Overlaying two ggplot() - map and density

Hello :slight_smile: I'm just wondering whether anyone would be able to help me.
I created a density plot using ggplot's stat_density_2d and I am trying to overlay this on top of a map which is a shapefile read and loaded to function in ggplot. Both codes shown in the image and the code attached at the end. I tried adding the density and the map together but I get an error message:
Error: Don't know how to add e2 to a plot

I'm guessing it cannot add to ggplot() commands together?
Any advice and feedback would be greatly appreciated.

Thank you!

R Code:

# DENSITY
kernel <- ggplot(AucklandCTPC, aes(x = EASTING, y = NORTHING) ) +
                stat_density_2d(aes(fill = ..level..), geom = "polygon") +
kernel

#############################

# DENSITY WITH SCATTERPLOT
plot <- ggplot(AucklandCTPC, aes(EASTING, NORTHING)) 
densityplot <- plot + stat_density2d(geom="polygon", aes(fill = ..level..), contour = TRUE) + geom_point(colour = "green")
densityplot

#############################

# READING SHAPEFILE AND CREATING THE MAP
readOGR(dsn = "C:/Users/janni/Documents/University of Auckland/ENVSCI705/RData/Project/CrashData/Crash_Analysis_System_CAS_data", 
        layer = "cau_clip")
plot(akl)
akl_df <- fortify(akl)
map <- ggplot() +
  geom_path(data = akl_df, 
            aes(x = long, y = lat, group = group),
            color = "black", fill = "yellow", size = .5)
print(map) 

#############################

# ADDING THE DENSITY MAPS AND AUCKLAND MAP TOGETHER
map + kernel

I don't have your data, so I can't test the code below, but the typical way to do this would be add a new "layer" to an existing plot, rather than to "add" together two separate ggplot objects.* EASTING and NORTHING need to have the same units as long and lat (or one needs to be transformed so that the units are equivalent) for this to work. I've put geom_path first, on the assumption that you want the map first and the contours overlayed on the map.

ggplot(AucklandCTPC, aes(EASTING, NORTHING)) + 
  geom_path(data = akl_df, 
            aes(x = long, y = lat, group = group),
            color = "black", fill = "yellow", size = .5) +
  stat_density2d(geom="polygon", aes(fill = ..level..), contour = TRUE) + 
  geom_point(colour = "green")

* There is, however, an R package called patchwork that allows you to lay out arrays of plots in a grid using the + operator, but that's a different thing than what you're trying to do here.

1 Like

That definitely solved my problem! I now understand how ggplot works in terms of layering. Thank you so much! :smiley:

My suspicion - far from certainty, given that we can not access your shapefiles - is that the coordinate reference systems of your two objects are not the same.

It is possible that one describes latitude and longitude in angular units = decimal degrees (as used by GPS and Google Maps) and the other in distance units = meters or what not (this is what easting and northing often means). This is not compatible for a number of reasons.

To verify this check sf::st_crs() for both objects - you should get the same result. If not you need to sf:st_transform() one of the objects to the CRS of the other.

Also consider reading the shapefile in as a sf object and visualizing it via the geom_sf() function from recent ggplot2; the approach via fortify() is by now rather obsolete (though commonly referenced in older online manuals).

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.