2014-02-11 41 views
3

我在ggplot中有一個問題,我想要在同一個繪圖中分離數據集。第一個數據集創建一個地理熱圖,並根據狀態顯示機會值。在ggplot中彼此繪製兩個圖形

setwd("~/R Work Example") 
library(ggplot2) 
library(maps) 
states <- map_data("state") 
tf=read.csv("Geographic Opprotunity.csv") 
mydata<-read.csv("top 200 geographic heatmap.csv") 
tfmerged <- merge(states, tf, sort = FALSE, by = "region") 
tfmerged <- tfmerged[order(tfmerged$order), ] 
Map<- ggplot(tfmerged, aes(long,lat, group=group, 
       fill=Total.Annual.Opportunity.In.Millions))+ geom_polygon()+ coord_equal() 
Map + scale_fill_gradient(low="white", high="red") 
Map<-Map + scale_fill_gradient(low="white", high="red") 

這很好,我非常興奮。我的下一個步驟,繪製了此圖,但這次進一步分解的機會,而不是由國家,我打破了下來,由國家內城市:

p<-ggplot() 
p <- Map + geom_point(data=mydata, aes(x=long, y=lat, size = Opportunity 
), color="gray6") + scale_size(name="Opportunity") 

當我跑我得到這個錯誤的腳本:

Error in eval(expr, envir, enclos) : object 'group' not found.

我GOOGLE了它,並嘗試一些上市這一頁,仍然一無所獲上的解決方案。我也爲上面的腳本創建了另一個ggplot,並將其正確繪製,我只是希望能夠將第二個圖中的實際數據點放置在第一個腳本中創建的熱圖上。

您可以在這裏找到設置我的數據的剪:

這裏是我的數據是這樣的:

dput(head(mydata)) 
structure(list(label = structure(c(79L, 51L, 138L, 161L, 45L, 
125L), .Label = c("ABILENE", "AIKEN", "ALBANY", "AMARILLO", "ANN ARBOR", 
"ANNAPOLIS", "APPLE VALLEY", "ARLINGTON HEIGHTS", "ATHENS", "ATLANTA", 
"AUGUSTA", "BAKERSFIELD", "BALTIMORE", "BANGOR", "BELLEVILLE", 
"BETHLEHEM", "BEVERLY", "BIRMINGHAM", "BOCA RATON", "BOISE", 
"BOSTON", "BOYNTON BEACH", "BRADENTON", "BRISTOL", "BRONX", "BROOKLYN", 
"BRYAN", "BUFFALO", "BURLINGTON", "CAMDEN", "CHARLESTON", "CHARLOTTE", 
"CHARLOTTESVILLE", "CHELSEA", "CHICAGO", "CHICAGO HEIGHTS", "CHICO", 
"CLINTON", "CLYDE", "COLUMBUS", "COMMERCE TOWNSHIP", "CORBIN", 
"CORONA", "CROWN POINT", "DALLAS", "DARBY", "DARIEN", "DECATUR", 
"DENISON", "DENVER", "DETROIT", "DOVER", "DUARTE", "DUBUQUE", 
"DURHAM", "EAU CLAIRE", "EDINBURG", "EFFINGHAM", "ELMHURST", 
"ENID", "EUREKA", "EVANSTON", "FLUSHING", "FORT LAUDERDALE", 
"FORT WORTH", "FREDERICKSBURG", "FRESNO", "FULLERTON", "GENEVA", 
"GLENDALE", "GRAND RAPIDS", "GREELEY", "GREEN BAY", "GREENVILLE", 
"HACKENSACK", "HAMMOND", "HANFORD", "HERSHEY", "HOUSTON", "INDEPENDENCE", 
"INDIANAPOLIS", "JACKSON", "JACKSONVILLE", "JAMAICA", "JONESBORO", 
"JUPITER", "KANSAS CITY", "LA CROSSE", "LAFAYETTE", "LAKEWOOD", 
"LANGHORNE", "LANSING", "LAREDO", "LAS VEGAS", "LAURINBURG", 
"LEONARDTOWN", "LEXINGTON", "LIVINGSTON", "LIVONIA", "LOMA LINDA", 
"LONG BRANCH", "LONGVIEW", "LOS ANGELES", "LOUISVILLE", "LOVELAND", 
"LUBBOCK", "LYNCHBURG", "MADISON", "MADISONVILLE", "MANCHESTER", 
"MANHASSET", "MANKATO", "MASON CITY", "MAYWOOD", "MELBOURNE", 
"MEMPHIS", "MESA", "MIAMI", "MIAMI BEACH", "MIAMISBURG", "MICHIGAN CITY", 
"MIDDLETON", "MIDLAND", "MILWAUKEE", "MINNEAPOLIS", "MISHAWAKA", 
"MISSION VIEJO", "MOBILE", "MONROEVILLE", "MONTCLAIR", "MORRISTOWN", 
"MUSKEGON", "NAPA", "NATRONA HEIGHTS", "NEEDHAM", "NEW ALBANY", 
"NEW HYDE PARK", "NEW YORK", "NEWARK", "NORFOLK", "NORRISTOWN", 
"NORTH WILKESBORO", "ODESSA", "OMAHA", "ORANGE", "OXFORD", "PADUCAH", 
"PALMDALE", "PANAMA CITY", "PARADISE", "PHILADELPHIA", "PHOENIX", 
"PITTSBURGH", "PLANO", "POCATELLO", "PONTIAC", "PORTLAND", "POUGHKEEPSIE", 
"PRESQUE ISLE", "RALEIGH", "ROCHESTER", "SAINT HELENA", "SAINT LOUIS", 
"SAN ANGELO", "SAN FRANCISCO", "SANTA ROSA", "SHELBY", "SHERMAN", 
"SILVER SPRING", "SIMI VALLEY", "SONORA", "SOUTHAVEN", "SPARTANBURG", 
"SPRINGFIELD", "STATEN ISLAND", "STUART", "SYLVA", "SYRACUSE", 
"TEMPLE", "TOMS RIVER", "TRENTON", "TUCSON", "TUSCALOOSA", "TYLER", 
"VERO BEACH", "VISALIA", "WACO", "WASHINGTON", "WAYCROSS", "WEST PALM BEACH", 
"WHITEVILLE", "WICHITA FALLS", "WILDOMAR", "WILMINGTON", "WINFIELD", 
"WINSTON SALEM", "YONKERS", "YORK"), class = "factor"), region = structure(c(34L, 
19L, 28L, 20L, 34L, 20L), .Label = c("al", "ar", "az", "ca", 
"co", "dc", "de", "fl", "ga", "ia", "id", "il", "in", "ks", "ky", 
"ma", "md", "me", "mi", "mn", "mo", "ms", "nc", "ne", "nh", "nj", 
"nv", "ny", "oh", "ok", "pa", "sc", "tn", "tx", "va", "wi", "wv" 
), class = "factor"), lat = c(29.9519265, 42.3486635, 40.7305991, 
44.0226213, 32.7801052, 44.9772995), long = c(-95.54091698, -83.0567375, 
-73.9865812, -92.4630094, -96.8000082, -93.2654692), Opportunity = c(20.4723937, 
14.15191147, 14.06937574, 13.5368484, 11.46484222, 11.16776426 
), Group = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("label", "region", 
"lat", "long", "Opportunity", "Group"), row.names = c(NA, 6L), class = "data.frame") 

mydata第一圖像對應,第二個與tfmerged

+2

你的數據是什麼樣的?你可以發佈'dput(head(mydata))'嗎? – Gregor

+0

是的,可重現的數據會很好。 –

+0

我建立了一個包含我的數據剪輯的鏈接。無論如何實際上傳數據集? – NJ13

回答

3

現在你的問題是,你是在頂層(即內ggplot(...))指定aes(group=group),這樣當您添加使用不同的數據集geom_pointggplot嘗試應用頂層審美到新層即使你沒有指定一個group審美。三種解決方案:

  • ggplot(aes(...))刪除有問題的美感,並將其移動到層,他們直接向
  • 對於不使用那些全球美觀層,迫使他們爲固定值(如geom_point(data=mydata, aes(group=1, fill=1, ...)
  • (可能)虛擬group變量添加到mydata

編輯:既然你提供的數據,這裏是一個可重複的解決方案(注意,您不包括tf,所以我只是ü從state.x77 SED人口填充的多邊形:

library(ggplot2) 
library(maps) 
states <- map_data("state") 
states$pop <- state.x77[match(states$region, tolower(rownames(state.x77)))] 
tfmerged <- states 
Map <- ggplot(
    tfmerged, 
    aes(long,lat, fill=pop, group=region)) + 
    geom_polygon() + coord_equal() + 
    scale_fill_gradient(low="white", high="red") 

Map + geom_point(
    data=mydata, aes(x=long, y=lat, fill=1, group=1, size=Opportunity), 
    color="gray6") + 
    scale_size(name="Opportunity")       

注意這只是增加了,這是你的dput數據集的一部分數據。

enter image description here

+0

我改變了一些代碼:Map < - ggplot(tfmerged,aes(long,lat,group = group, fill = Total.Annual.Opportunity.In.Millions )) + geom_polygon()+ coord_equal() 地圖+ scale_fill_gradient(低=「白」,高=「紅」) 地圖<-Map + scale_fill_gradient(低=「白」,高=「紅」) p < ggplot() p < - Map + geom_point(data = mydata,aes(x = long,y = lat,group = 1,size = Opportunity ),color =「gray6」)+ scale_size(name =「Opportunity」)我在我的數據中添加了一個新列,並將其標爲組,將默認值設置爲1 ...我沒有正確跟蹤您的情況嗎? – NJ13

+0

@ user3285069,查看更新。 – BrodieG

+0

我可以問你跟進爲什麼你做填充和組= 1? – NJ13