7

I'm new to ggplot2 and relatively new to R. I can make a picture appear on a plot, and I can make the y axis reverse scale, but I don't know how to do both at once. For example:

library(ggplot2)

y=c(1,2,3)
x=c(0,0,0)
d=data.frame(x=x, y=y)

#following http://stackoverflow.com/questions/9917049/inserting-an-image-to-ggplot2/9917684#9917684
library(png)
library(grid)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)

#these work fine - either reversing scale, or adding custom annotation
ggplot(d, aes(x, y)) + geom_point()
ggplot(d, aes(x, y)) + geom_point() + scale_y_reverse()
ggplot(d, aes(x, y)) + geom_point() + annotation_custom(g, xmin=.23, xmax=.27, ymin=1.8, ymax=2.2)

#these don't...combining both reverse scale and custom annotation
ggplot(d, aes(x, y)) + geom_point() + annotation_custom(g, xmin=.23, xmax=.27, ymin=1.8, ymax=2.2) + scale_y_reverse()
ggplot(d, aes(x, y)) + geom_point() + annotation_custom(g, xmin=.23, xmax=.27, ymin=2.2, ymax=1.8) + scale_y_reverse()

I'm sure I'm missing something pretty basic. Where should I start looking both to get my little graphic to display on a reverse scale plot, and also to understand things better under the hood?

CLARIFICATION IN RESPONSE TO COMMENTS: The example above is me trying to simplify the problem I'm having. I don't know if it matters, but I'm not merely trying to overlay some data on a static image. I actually want to place an image in a certain spot on a plot, based on the data in the plot. However, I can't seem to do that when the axis scale is reversed. And, as it turns out, I can't even put an image in an absolute position when the scale is reversed either, so that's the code example I posted.

jtolle
  • 7,023
  • 2
  • 28
  • 50
  • When I see a question that seems likely to have already been requested, I take key words from the question title and do a bit of SO searching. – IRTFM Nov 21 '15 at 23:32
  • @42, As do I. Anything on SO in particular that I might have missed without realizing it contained my answer? – jtolle Nov 22 '15 at 03:03
  • There are multiple questions and answers on SO showing the capacity to overlay data on png images: http://stackoverflow.com/questions/30152309/how-to-overlay-and-position-a-logo-over-any-r-plot-igraph-ggplot2-etc-so-i-c – IRTFM Nov 22 '15 at 03:38
  • 1
    @42, Indeed. That is in fact how I found out how to use `annotation_custom` in the first place. The thing I can't figure out is how to use it along with `scale_y_reverse`. In case it matters, My actual goal is to place an image based on the data I'm plotting, but I can't even do it in a fixed position, so I gave that example as it's similar. (See my edit.) Do you know how to do what I'm trying to do, or do you have a link to something that explains why what I'm trying isn't working? Maybe I'm just not recognizing the answer in my own search results. – jtolle Nov 22 '15 at 03:59
  • Hopefully now @Sandy's answer will show up high in Google or SO searches for "annotation_custom scale_y_reverse"! – jtolle Nov 22 '15 at 14:37

1 Answers1

8

With scale_y_reverse, you need to set the y coordinates inside annotation_custom to their negative.

library(ggplot2)
y=c(1,2,3)
x=c(0,0,0)
d=data.frame(x=x, y=y)


library(png)
library(grid)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)

ggplot(d, aes(x, y)) + geom_point() + 
   annotation_custom(g, xmin=.20, xmax=.30, ymin=-2.2, ymax=-1.7) + 
   scale_y_reverse()

enter image description here

Why negative? The y coordinates are the negative of the original. Check out this:

(p = ggplot(d, aes(x=x, y=y)) + geom_point() + scale_y_reverse())
y.axis.limits = ggplot_build(p)$layout$panel_params[[1]][["y.range"]]
y.axis.limits

OR, set the coordinates and size of the grob in relative units inside rasterGrob.

g <- rasterGrob(img, x = .75, y = .5, height = .1, width = .2, interpolate=TRUE)

ggplot(d, aes(x, y)) + geom_point() + 
   annotation_custom(g) +
   scale_y_reverse() 
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
  • 1
    Thanks! Negative coordinates did not dawn on me. Your extra info about relative units is also very helpful. – jtolle Nov 22 '15 at 14:30