2

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.

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.

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.

You can find snips of my data set here:

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

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

4
  • 2
    What does your data look like? Could you post dput(head(mydata))? Commented Feb 11, 2014 at 21:29
  • Yes, reproducible data would be nice. Commented Feb 11, 2014 at 22:16
  • I put up a link with snips of my data. Is there anyway to actually upload a dataset? Commented Feb 12, 2014 at 13:33
  • I also just added teh dput as shujaa asked Commented Feb 12, 2014 at 19:11

1 Answer 1

5

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:

  • Remove the problematic aesthetics from the ggplot(aes(...)) and move them to the layers they directly apply to
  • 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, ...))
  • Add a (possibly) dummy group variable to 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:

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.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I changed some of the code: 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,group=1,size = Opportunity ), color="gray6") + scale_size(name="Opportunity") I added a new column in my data and titled it group, set default values to 1...Did I not follow you correctly?
Can I ask you a follow up as to why you made the fill and group = 1?
@user3285069, see bullet #2. Basically, you have to override the aesthetics you specified in the actual ggplot call so that geom_point doesn't try to look for them in mydata, where they don't exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.