Plotting letters as shapes in ggplot2

[This article was first published on is.R(), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This post is a little more esoteric than most, but I found myself needing to solve this problem, so I’m just passing the solution on to you. The plot above shows the distribution of DW-NOMINATE scores for the 18th Congress, with party indicated by both color and shape. You will notice that there are more parties in 1824 than there are today — so many, in fact, that ggplot2 will resist plotting the seven shapes needed to account for each party. Note that I am confident that there is a good, peer-reviewed reason for this, so caveat emptor.

One work-around is to plot the initial letter of each party as a text geom, but in this case, the legend indicates the use of geom_text with an “a,” rather than an indicator for each shape. This is non-optimal, particularly if it’s not perfectly clear how the plotted letter symbols align with the party names:

The solution (or possibly, hack) is to use geom_shape, but use a custom scale that passes the numeric referents to each of the letters you want to use. To do so, we just need to choose the initial to use for each party name, and pass them to scale_shape_manual with utf8ToInt(). Incidentally, to manually look up the shape-to-numeric indicator correspondence, just run example(points).

To leave a comment for the author, please follow the link and comment on their blog: is.R().

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)