我在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
!
你的數據是什麼樣的?你可以發佈'dput(head(mydata))'嗎? – Gregor
是的,可重現的數據會很好。 –
我建立了一個包含我的數據剪輯的鏈接。無論如何實際上傳數據集? – NJ13