R 在tikzDevice中使用tikzAnnotate注释ggplot2图形

R 在tikzDevice中使用tikzAnnotate注释ggplot2图形,r,ggplot2,R,Ggplot2,我想使用tikzDevice在Latex文档中包含带注释的ggplot2图形 tikzAnnotate帮助提供了一个示例,说明如何将其与基本图形一起使用,以及如何将其与基于网格的绘图软件包(如ggplot2)一起使用?挑战似乎在于tikz节点的定位 playwith软件包有一个函数convertToDevicePixels(),它似乎类似于grconvertX/grconvertY,但我也无法让它工作 如果您能就如何进行提供指导,我将不胜感激 tikzAnnotate使用基本图形的示例 libr

我想使用
tikzDevice
Latex
文档中包含带注释的
ggplot2
图形

tikzAnnotate
帮助提供了一个示例,说明如何将其与基本图形一起使用,以及如何将其与基于网格的绘图软件包(如
ggplot2
)一起使用?挑战似乎在于tikz节点的定位

playwith
软件包有一个函数
convertToDevicePixels
(),它似乎类似于grconvertX/grconvertY,但我也无法让它工作

如果您能就如何进行提供指导,我将不胜感激

tikzAnnotate使用基本图形的示例

library(tikzDevice)
library(ggplot2)
options(tikzLatexPackages = c(getOption('tikzLatexPackages'),
                "\\usetikzlibrary{shapes.arrows}"))
tikz(standAlone=TRUE)

print(plot(15:20, 5:10))
#print(qplot(15:20, 5:10))

x <- grconvertX(17,,'device')
y <- grconvertY(7,,'device')
#px <- playwith::convertToDevicePixels(17, 7)
#x <- px$x
#y <- px$y

tikzAnnotate(paste('\\node[single arrow,anchor=tip,draw,fill=green] at (',
                x,',',y,') {Look over here!};'))
dev.off()
库(tikzDevice)
图书馆(GG2)
选项(tikzLatexPackages=c(getOption('tikzLatexPackages'),
“\\usetikzlibrary{shapes.arrows}”))
tikz(独立=真)
打印(绘图(15:20,5:10))
#打印(qplot(15:20,5:10))

x我根据@Andrie的建议,用
geom_text
geom_polygon
制作了一个小例子:

正在初始化数据:

df <- structure(list(x = 15:20, y = 5:10), .Names = c("x", "y"), row.names = c(NA, -6L), class = "data.frame")

当然,这是一个相当粗糙的解决方案,但可以扩展:

  • 根据x和y范围计算箭头的大小
  • 根据文本的长度(或使用
    textGrob
    计算字符串的实际宽度)计算文本的位置
  • 定义不与点重叠的形状:)

祝你好运

目前,
tikzAnnotate
仅适用于基本图形。当首次编写
tikzAnnotate
时,
grid
图形的问题在于,我们需要一种方法来指定相对于设备画布绝对左下角的x,y坐标<代码>网格
从视口的角度考虑,在许多情况下,图形的最终坐标系似乎不知道,直到它通过
打印
功能指向设备

拥有这个功能会很好,但是我想不出一个好的方法来实现它,所以这个功能被搁置了。如果有人对一个好的实现有详细的了解,可以在邮件列表(现在有一个关于谷歌群组的邮件列表)上展开讨论,它将被列入待办事项列表

更好的是,实现该功能并在上打开对项目的拉取请求。这保证了该功能发布的速度比我的待办事项列表上的时间快9000倍以上


更新 我花了一些时间来做这件事,我想出了一个将当前视口中的栅格坐标转换为绝对设备坐标的函数:

gridToDevice <- function(x = 0, y = 0, units = 'native') {
  # Converts a coordinate pair from the current viewport to an "absolute
  # location" measured in device units from the lower left corner. This is done
  # by first casting to inches in the current viewport and then using the
  # current.transform() matrix to obtain inches in the device canvas.
  x <- convertX(unit(x, units), unitTo = 'inches', valueOnly = TRUE)
  y <- convertY(unit(y, units), unitTo = 'inches', valueOnly = TRUE)

  transCoords <- c(x,y,1) %*% current.transform()
  transCoords <- (transCoords / transCoords[3])

  return(
    # Finally, cast from inches to native device units
    c(
      grconvertX(transCoords[1], from = 'inches', to ='device'),
      grconvertY(transCoords[2], from = 'inches', to ='device')
    )
  )

}
这将提供以下输出:


还有一些工作要做,例如:

  • 创建可添加到栅格图形的“注释grob”

  • 确定如何将此类对象添加到
    ggplot


这些功能属于
tikzDevice

您是否考虑过直接在ggplot()中使用geom_text进行注释?这是一个备份选项。我更喜欢tikz,因为它允许使用各种形状。你觉得ggplot中结合geom_文本和geom_多边形的示例有用吗?嗯,这可能是解决这个问题的另一种方法。。。是的,举个例子会很有用。我在二月份根据@Andrie的建议编了一个例子——尽管我刚刚意识到我上次贴的图片从那时起不知怎么消失了。我又上传了一次,希望能有所帮助。
arrow <- data.frame(
    x = c(point$x-0.1, point$x-0.3, point$x-0.3, point$x-2, point$x-2, point$x-0.3, point$x-0.3, point$x-0.1),
    y = c(point$y, point$y+0.3, point$y+0.2, point$y+0.2, point$y-0.2, point$y-0.2, point$y-0.3, point$y)
)
ptext <- data.frame(label=ptext, x=point$x-1, y=point$y)
ggplot(df, aes(x,y)) + geom_point() + geom_polygon(aes(x,y), data=arrow, fill="green") + geom_text(aes(x, y, label=label), ptext) + theme_bw()
gridToDevice <- function(x = 0, y = 0, units = 'native') {
  # Converts a coordinate pair from the current viewport to an "absolute
  # location" measured in device units from the lower left corner. This is done
  # by first casting to inches in the current viewport and then using the
  # current.transform() matrix to obtain inches in the device canvas.
  x <- convertX(unit(x, units), unitTo = 'inches', valueOnly = TRUE)
  y <- convertY(unit(y, units), unitTo = 'inches', valueOnly = TRUE)

  transCoords <- c(x,y,1) %*% current.transform()
  transCoords <- (transCoords / transCoords[3])

  return(
    # Finally, cast from inches to native device units
    c(
      grconvertX(transCoords[1], from = 'inches', to ='device'),
      grconvertY(transCoords[2], from = 'inches', to ='device')
    )
  )

}
require(tikzDevice)
require(grid)
options(tikzLatexPackages = c(getOption('tikzLatexPackages'),
                "\\usetikzlibrary{shapes.arrows}"))

tikz(standAlone=TRUE)

xs <- 15:20
ys <- 5:10

pushViewport(plotViewport())
pushViewport(dataViewport(xs,ys))

grobs <- gList(grid.rect(),grid.xaxis(),grid.yaxis(),grid.points(xs, ys))

coords <- gridToDevice(17, 7)
tikzAnnotate(paste('\\node[single arrow,anchor=tip,draw,fill=green,left=1em]',
  'at (', coords[1],',',coords[2],') {Look over here!};'))

dev.off()