Using Inkscape to Post-edit Labels in R Graphs

Posted on September 26th, 2011

I like to label points in my graphs. For example, in my previous exchange with Seth Masket, we both labeled every point. Often, default labeling schemes in R will produce text that overlaps substantially, requiring complicated code to prevent overlap or only label a few of the points. I have recently been experimenting with post-editing graphs produced in R with Inkscape, an open-source version of Adobe Illustrator. I have found post-editing in Inkscape especially useful for correcting overlap when labeling points. Below I discuss how I go about this and give an example.

Start with a basic plot of the data, but save the plot using the devSVG() device found in the RSvgDevice library. (Although see the P.S. at the bottom and Jay Kerns' remark in the comments section.) The code to do this is given below.

To view the graph, simply open it in a web browser or Inkscape. While the names are plotted on the graph, several overlap substantially and are thus unreadable.

Unfortunately, the devSVG() driver does not contain font metric information, so the font needs to be selected in Inkscape. To do this, simply click "Select All" in the "Edit" tab and then change the fonts as desired.

After changing the fonts (if necessary), simply click the selector tool (or press F1). Now the text labels can be dragged around or deleted as necessary. In the new figure below, which only requires dragging and dropping several of the labels, there is no overlap.

There is no need to write code in R to position the labels properly. Of course, if the figure needs to be recreated several times, having a program to position the labels might be worth a little extra work. But in other cases, the figure can simply be saved as a  .svg file and then the labels can be easily repositioned with some simple post-editing in Inkscape.


P.S. Jay Kerns correctly points out in the comments section that the svg() device has better font support. For some reason, I was having a little trouble with it yesterday, but it might have been my own error. It seems to work now. I plotted the graph in R with my favorite serif font and then post-edited in Inkscape to remove the overlap. The plot is posted below and looks fantastic. Thanks for the feedback, Jay.