R ggplot2 0.9.3和;注释的行为是自定义的

R ggplot2 0.9.3和;注释的行为是自定义的,r,ggplot2,R,Ggplot2,继最近的一个问题之后,这个问题有点不同,并用更简单的例子更充分地说明了这个问题。下面是两个数据集和三个函数。第一个按预期绘制一些点和一个圆: library("ggplot2") library("grid") td1 <- data.frame(x = rnorm(10), y = rnorm(10)) tf1 <- function(df) { # works as expected p <- ggplot(aes(x = x, y = y), data = d

继最近的一个问题之后,这个问题有点不同,并用更简单的例子更充分地说明了这个问题。下面是两个数据集和三个函数。第一个按预期绘制一些点和一个圆:

library("ggplot2")
library("grid")

td1 <- data.frame(x = rnorm(10), y = rnorm(10))

tf1 <- function(df) { # works as expected
    p <- ggplot(aes(x = x, y = y), data = df)
    p <- p + geom_point(color = "red")
    p <- p + annotation_custom(circleGrob())
    print(p)
}

tf1(td1)
库(“ggplot2”)
图书馆(“网格”)
td1要回答这个问题:

“我不明白为什么这个功能需要任何美学”

事实上,
annotation\u custom
需要x和y aes来缩放其grob,并在
本机
单元之后使用。 基本上是这样的:

  x_rng <- range(df$x, na.rm = TRUE)                            ## ranges of x :aes x
  y_rng <- range(df$y, na.rm = TRUE)                            ## ranges of y :aes y
  vp <- viewport(x = mean(x_rng), y = mean(y_rng),              ##  create a viewport
                 width = diff(x_rng), height = diff(y_rng),
                 just = c("center","center"))
  dd <- editGrob(grod =circleGrob(), vp = vp)                  ##plot the grob in this vp 

谢谢你的回答。我理解你写的代码以及它为什么工作,但我不确定它与我的问题有什么关系。我评论不需要美学的原因是函数没有使用美学(从文档和代码中可以清楚地看到)。当然,它必须缩放以适应vp,但这不是一种美学(问题可能是我没有正确使用这些术语)<代码>注释\u custom
确实使用了
inherit.aes
(内部,不是参数),但我不清楚它从何处继承。
x
y
具有默认值,这就是为什么不必传递它们的原因。您写道:“现在查看(base.big+annot)和(base.big+annot)之间的比例差异(小点/大圆)(base.big+annot)。“我猜你是想写”(base.small+annot)。另外,“Bascially”是拼错的(第三行)。为什么不在github存储库中作为新版本提交?@baptiste我正在考虑这个问题,但我想先把它放在这里,看看这是否是我的误解/错误期望,因为我有这方面的历史记录。除非有人提出解释,否则我将在明天提交(由于您对这些事情非常了解,我很怀疑是否有人会这样做)。谢谢。我将此发布到ggplot2问题跟踪程序。问题是@stevec已多年未涉及此问题,对不起。请确保您使用的是最新的
ggplot2
版本。
td3 <- data.frame(r = c(rnorm(5, 5, 1.5), rnorm(5, 8, 2)),
    f1 = c(rep("L", 5), rep("H", 5)), f2 = rep(c("A", "B"), 5))

tf3 <- function(df) {
    p <- ggplot()
    p <- p + geom_point(data = df, 
        aes(x = f1, y = r, color = f2, group = f2))     
#   p <- p + annotation_custom(circleGrob()) # comment out and it works
    print(p)
    }

tf3(td3)
  x_rng <- range(df$x, na.rm = TRUE)                            ## ranges of x :aes x
  y_rng <- range(df$y, na.rm = TRUE)                            ## ranges of y :aes y
  vp <- viewport(x = mean(x_rng), y = mean(y_rng),              ##  create a viewport
                 width = diff(x_rng), height = diff(y_rng),
                 just = c("center","center"))
  dd <- editGrob(grod =circleGrob(), vp = vp)                  ##plot the grob in this vp 
base.big   <- ggplot(aes(x = x1, y = y1), data = data.frame(x1=1:100,y1=1:100))
base.small <- ggplot(aes(x = x1, y = y1), data = data.frame(x1=1:20,y1=1:1))
annot <- annotation_custom(grob = circleGrob(),  xmin = 0, 
                                                 xmax = 20, 
                                                 ymin = 0, 
                                                 ymax = 1)
library(gridExtra)
grid.arrange(base.big+annot,
             base.small+annot)