gpt4 book ai didi

r - 在 tikzDevice 中使用 tikzAnnotate 注释 ggplot2 图

转载 作者:行者123 更新时间:2023-12-04 11:30:33 24 4
gpt4 key购买 nike

我想使用 tikzDevice包括注释 ggplot2 Latex 中的图表文档。
tikzAnnotate help 有一个如何将它与基本图形一起使用的示例,但是如何将它与基于网格的绘图包一起使用,如 ggplot2 ?挑战似乎是 tikz 节点的定位。
playwith包有一个功能convertToDevicePixels ( http://code.google.com/p/playwith/source/browse/trunk/R/gridwork.R ) 似乎类似于 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()

resulting image

最佳答案

目前,tikzAnnotate仅适用于基本图形。当tikzAnnotate第一次写,grid 的问题graphics 是我们需要一种方法来指定相对于设备 Canvas 的绝对左下角的 x,y 坐标。 grid考虑视口(viewport),在许多情况下,图形的最终坐标系似乎是未知的,直到它通过 print 前往设备。功能。

拥有这个功能会很棒,但我想不出一个好的方法来实现它,所以这个功能被搁置了。如果有人有关于良好实现的详细信息,请随时在邮件列表(现在在 Google Groups 上有 alternate portal)开始讨论,它会出现在 TODO 列表中。

更好的是,实现该功能并在 GitHub 上打开项目的拉取请求.与在我的 TODO 列表中放置数月相比,这可以保证让该功能以超过 9000 倍的速度发布。

更新

我有一些时间来解决这个问题,并且我想出了一个将当前视口(viewport)中的网格坐标转换为绝对设备坐标的函数:

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

}

使用这个缺失的部分,可以使用 tikzAnnotate标记 gridlattice阴谋:
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()

这给出了以下输出:

TikZ Annotation of Grid Graphics

还有一些工作要做,例如:
  • 创建可以添加到网格图形的“注释 grob”。
  • 确定如何将此类对象添加到 ggplot .

  • 这些功能是 scheduled to appear in release 0.7tikzDevice .

    关于r - 在 tikzDevice 中使用 tikzAnnotate 注释 ggplot2 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5016719/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com