我如何使用R和ggplot2使annotation_custom()grob与scale_y_reverse()一起显示? [英] How do I get annotation_custom() grob to display along with scale_y_reverse() using R and ggplot2?

查看:39
本文介绍了我如何使用R和ggplot2使annotation_custom()grob与scale_y_reverse()一起显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ggplot2 的新手,而对R则是新手.我可以使图片显示在图上,并且可以使y轴反向缩放,但是我不知道如何一次做两个.例如:

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.

推荐答案

使用 scale_y_reverse ,您需要将 annotation_custom 中的y坐标设置为负数.

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()

为什么要消极?y坐标是原始坐标的负数.看看这个:

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

或,在 rasterGrob 内以相对单位设置grob的坐标和大小.

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() 

这篇关于我如何使用R和ggplot2使annotation_custom()grob与scale_y_reverse()一起显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆