8
votes

I am trying to figure out how to use grconvertX/grconvertX in ggplot. My ultimate goal is to to add annotation to a ggplot2 figure (and possibly lattice) with grid.text and grid.lines by going from user coordinates to device coordinates. I know it can be done with grobs but I am wondering if there is an easier way.

The following code allows me to pass values from user coordinates to ndc coordinates and use those values to annotate the plot with grid.text.

graphics.off()      # close graphics windows   

library(grid)
library(gridBase)


test= data.frame(
  x = c(1,2,3),
  y = c(12,10,3),
  n = c(75,76,73)
  )

par(mar = c(13,5,2,3))

plot(test$y ~ test$x,type="b", ann=F)

for (i in 1:nrow(test))

{
  X=grconvertX(i , from="user", to="ndc")
  grid.text(x=X, y =0.2, label=paste("GRID.text at\nuser.x=", i, "\n", "ndc.x=", (signif( X, 5))   ) ) 
  grid.lines(x=c(X, X), y = c(0.28, 0.33) )
}
#add some code to save as PDF ...

enter image description here

The code is based on the solution from one of my previous posts: Mixing X and Y coordinate systems . You can see how x coordinates from the original plot were converted to ndc. The advantage of this approach is that I can use device coordinates for Y.

I assumed I could easily do the same in ggplot2 (and possibly in lattice).

library(ggplot2)
graphics.off()      # close graphics windows   

qplot(x=x, y=y, data=test)+geom_line()+  opts(plot.margin = unit(c(1,3,8,1), "lines"))

for (i in 1:nrow(test))

{
  X=grconvertX(i , from="user", to="ndc")
  grid.text(x=X, y =0.2, label=paste("GRID.text at\nuser.x=", i, "\n", "ndc.x=", (signif( X, 5))   ) ) 
  grid.lines(x=c(X, X), y = c(0.28, 0.33) )
}

#add some code to save as PDF...

However, it does not work correctly. The coordinates seem to be a bit off. The vertical lines and text don't correspond to the tick labels on the plot. Can anybody tell me how to fix it? Thanks a lot in advance.

enter image description here

1
Just a heads up, I've been editing some formatting in your questions so that they fit better with the "standard" use of markup for code highlighting. Also, it's best not to include lines that remove all objects from a user's workspace in code that people are likely to quickly copy+paste. Leave it to the reader to make sure they are in a clean R session.joran
Thanks! I thought it's be easier to work in a clean session. I won't add this code anymore.Max C

1 Answers

11
votes

The grconvertX and grconvertY functions work with base graphics while ggplot2 uses grid graphics. In general the 2 different graphics engines don't play nicely together (though you have demonstrated using gridBase to help). Your first example works because you started with a base graphic so the user coordinate system exists with the base graph and grconvertX converts from it. In the second case the user coordinate system was never set in the base graphics, so it looks like it might use the default coordinates of 0,1 which are similar but not identical to the top viewport coordinates so you get something similar but not exactly correct (I am actually surprised that you did not get an error or warning

Generally for grid graphics the equivalent for converting between coordinates is to just create a new viewport with the coordinate system of interest (or push/pop to an existing viewport with the correct coordinate system), then add your annotations in that viewport.

Here is an example that creates your plot, then moves down to the viewport containing the main plot, creates a new viewport with the same dimensions but with clipping turned off, the x scale is based on the data and the y scale is 0,1, then adds some text accordingly:

library(ggplot2)
library(grid)

test= data.frame(   x = c(1,2,3),   y = c(12,10,3),   n = c(75,76,73)   )  

qplot(x=x, y=y, data=test)+geom_line()+  opts(plot.margin = unit(c(1,3,8,1), "lines"))  

current.vpTree()
downViewport('panel-3-4')
pushViewport(dataViewport( test$x, clip='off',yscale=c(0,1)))

for (i in 1:nrow(test))  {
    grid.text(x=i, y = -0.2, default.units='native',
        label=paste("GRID.text at\nuser.x=", i, "\n"   ) )
        grid.lines(x=c(i, i), y = c(-0.1, 0), default.units='native' )
 } 

One of the tricky things here is that ggplot2 does not set the viewport scales to match the data being plotted, but does the conversions itself. In this case setting the scale based on the x data worked, but if ggplot2 does something fancier then this might not work. What we would need is some way to get the back tranformed coordinates from ggplot2 to use in the call to grid.text.