在ggplot中绘制两个图形

时间:2014-02-11 21:19:14

标签: r plot ggplot2

我在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")

当我运行脚本时,我收到了这个错误:

  

eval(expr,envir,enclos)出错:找不到对象'group'。

我用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

1 个答案:

答案 0 :(得分:3)

您现在的问题是您在顶层指定aes(group=group)(即在ggplot(...)内),以便在添加使用其他数据集的geom_point时{ {1}}尝试将顶级美学应用于该新图层,即使您没有为该图层指定ggplot美学。三种解决方案:

  • group中移除有问题的美学,并将它们移到他们直接应用的图层
  • 对于不使用这些全局美学的图层,强制它们为固定值(例如ggplot(aes(...))
  • 将一个(可能的)虚拟geom_point(data=mydata, aes(group=1, fill=1, ...)变量添加到group
编辑:既然您提供了数据,这里是一个可重现的解决方案(注意您没有包含mydata,所以我只使用tf中的人口来填充多边形:

state.x77

请注意,这只会添加属于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") 数据集的数据。

enter image description here