Plotting letters as shapes in ggplot2

November 5, 2012
By

(This article was first published on is.R(), and kindly contributed to R-bloggers)

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 his blog: is.R().

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , ,

Comments are closed.