在ggplot中互相画两个图。

时间:2022-02-02 08:58:59

I have a problem in ggplot where I want to graph to separate data sets within the same plot. The first data set creates a geographic heat map and shows opportunity values based on the state.

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

This worked great, I was super excited. My next step to plot over this graph, but this time break down the opportunity further, instead of by state, I broke it down by cities within the state:

这个工作很成功,我非常兴奋。我下一步要画这个图,但是这次我把机会进一步分解,不是按州来,而是按州内的城市来分解:

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

When I ran the script I got this error:

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

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

eval(expr, envir, enclos)中的错误:未找到对象'group'。

I googled it, and attempted a few of the solutions that were listed on this page and still nothing. I did also create another ggplot for the script above and got it to plot correctly, I just want to be able to put the actual data points in the second plot over the heatmap I created in the first script.

我在谷歌上搜索了一下,尝试了一些列在这个页面上的解决方案,但仍然一无所获。我还为上面的脚本创建了另一个ggplot,并让它正确地绘制,我只是希望能够将实际的数据点放在我在第一个脚本中创建的热图上的第二个图中。

You can find snips of my data set here:

你可以在这里找到我数据集的片段:

在ggplot中互相画两个图。

在ggplot中互相画两个图。

Here is what my data looks like:

以下是我的数据:

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", "*LYN", 
"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")

The first image corresponds with mydata, the second with tfmerged!

第一个图像与mydata相对应,第二个图像与tfmerge相对应!

1 个解决方案

#1


3  

Your problem right now is that you are specifying aes(group=group) at the top level (i.e. inside ggplot(...)), so that when you add a geom_point that uses a different data set, ggplot attempts to apply the top level aesthetic to that new layer even though you're not specifying a group aesthetic for that one. Three solutions:

现在你的问题是你指定aes(组=组)在顶层(即内部ggplot(…)),所以当你添加一个使用不同的数据集,geom_point ggplot试图运用*的审美新图层即使你不指定一个组的审美。三个解决方案:

  • Remove the problematic aesthetics from the ggplot(aes(...)) and move them to the layers they directly apply to
  • 从ggplot(aes(…))中删除有问题的美学,并将它们移动到它们直接应用的层中
  • For the layers that don't use those global aesthetics, force them to fixed values (e.g. geom_point(data=mydata, aes(group=1, fill=1, ...))
  • 对于不使用全局美学的层,将它们强制到固定值(例如geom_point(data=mydata, aes(group=1, fill=1,…))
  • Add a (possibly) dummy group variable to mydata
  • 向mydata添加一个(可能的)哑组变量

EDIT: Now that you provided data, here is a reproducible solution (note you did not include tf, so I just used population from state.x77 as the fill for the polygons:

编辑:既然您提供了数据,这里有一个可复制的解决方案(注意,您不包括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")                        

Note this only adds data that was part of your dput data set.

注意,这只添加了dput数据集的一部分。

在ggplot中互相画两个图。

#1


3  

Your problem right now is that you are specifying aes(group=group) at the top level (i.e. inside ggplot(...)), so that when you add a geom_point that uses a different data set, ggplot attempts to apply the top level aesthetic to that new layer even though you're not specifying a group aesthetic for that one. Three solutions:

现在你的问题是你指定aes(组=组)在顶层(即内部ggplot(…)),所以当你添加一个使用不同的数据集,geom_point ggplot试图运用*的审美新图层即使你不指定一个组的审美。三个解决方案:

  • Remove the problematic aesthetics from the ggplot(aes(...)) and move them to the layers they directly apply to
  • 从ggplot(aes(…))中删除有问题的美学,并将它们移动到它们直接应用的层中
  • For the layers that don't use those global aesthetics, force them to fixed values (e.g. geom_point(data=mydata, aes(group=1, fill=1, ...))
  • 对于不使用全局美学的层,将它们强制到固定值(例如geom_point(data=mydata, aes(group=1, fill=1,…))
  • Add a (possibly) dummy group variable to mydata
  • 向mydata添加一个(可能的)哑组变量

EDIT: Now that you provided data, here is a reproducible solution (note you did not include tf, so I just used population from state.x77 as the fill for the polygons:

编辑:既然您提供了数据,这里有一个可复制的解决方案(注意,您不包括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")                        

Note this only adds data that was part of your dput data set.

注意,这只添加了dput数据集的一部分。

在ggplot中互相画两个图。