Hi everyone,
I have been experimenting with ggplot2 to draw data (such as population, GDP, etc.) on a world map. A classical application I believe.
The chart I want to create should combine:
- one data frame (worldmap.f) containing the polygons of a high-res world map (polygons derived from a SpatialPolygosnDataFrame converted through fortify)
- another different data frame (worldmap.df) containing the name for each country and the country centroid as Long, Lat pair
It seems to me that ggplot2 should be able to combine and overlap in one single chart data coming from these two different data frames, properly aligned through latitude and longitude, however the two plotting sequences that work perfectly independently, fail miserably when combined.
Here the non-working code:
p <- ggplot(data=worldmap.f, aes(x=long, y=lat, group=group)) + geom_polygon(fill="darkseagreen") + geom_path(colour = "grey40")
p <- p + geom_text(data=worldmap.df, aes(x=LON, y=LAT, label=NAME), alpha=I(1/3))
p
If I split it like this:
p <- ggplot(data=worldmap.f, aes(x=long, y=lat, group=group)) + geom_polygon(fill="darkseagreen") + geom_path(colour = "grey40")
p
q <- ggplot() + geom_text(data=worldmap.df, aes(x=LON, y=LAT, label=NAME), alpha=I(1/3))
q
I obtain the two charts as expected, but when combining them as in the first code snippet I get the following error about "group" being missing!
Error in eval(expr, envir, enclos) : object 'group' not found
There is indeed no group in the 2nd data frame, but why should it bother since it is not referenced there. Anyone can help to solve the mistery?
Sure, I could try to combine the two data frames so that all geometries use the same data, but I would expect the code above to work anyway.
Thanks in advance,
Marco.