Plotting two graphs over each other in ggplot

10,918

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

Share:
10,918
NJ13
Author by

NJ13

New to R, but learning more everyday.

Updated on June 04, 2022

Comments

  • NJ13
    NJ13 almost 2 years

    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!