geom_polygon and geom_text fail when combined (but work as expected independently)

270 views
Skip to first unread message

Marco Ghislanzoni

unread,
Oct 6, 2013, 12:38:32 PM10/6/13
to ggp...@googlegroups.com
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.

Ista Zahn

unread,
Oct 6, 2013, 2:54:57 PM10/6/13
to Marco Ghislanzoni, ggplot2
Hi Marco,
Aesthetic mappings established in the ggplot() call apply to all the
layers, unless they are over-ridden. One way to solve your problem is
to move the group mapping out of the ggplot() call and into the
specific geom calls, e.g.,

ggplot(data=worldmap.f, aes(x=long, y=lat)) +
geom_polygon(aes(group=group), fill="darkseagreen") +
geom_path(aes(group=group), colour = "grey40") +
geom_text(data=worldmap.df,
aes(x=LON, y=LAT, label=NAME),
alpha=I(1/3))

Best,
Ista


>
> 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.
>
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Marco Ghislanzoni

unread,
Oct 6, 2013, 3:11:19 PM10/6/13
to ggp...@googlegroups.com, Marco Ghislanzoni
Hi Ista,
many thanks, that worked flawlessly! Lesson learned I guess. 
Kind Regards,
Marco.
Reply all
Reply to author
Forward
0 new messages