本文介绍了在tikzDevice中使用tikzAnnotate注释ggplot2图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我希望使用 playwith 包有一个函数 convertToDevicePixels (,并且它会显示在TODO列表中。 更好的是,实现这个功能并打开 GitHub 。这可以保证将这个功能发布到一个发布版本上,比起它在我的TODO列表上几个月的发布速度要快上9000倍。 $ b Update 我有一些时间来处理这个问题,我想出了一个函数来将当前视口中的网格坐标转换为绝对的设备坐标: p> gridToDevice< - function(x = 0,y = 0,units ='native'){#将坐标对从当前视口设置为从设备单位左下角测量的绝对#位置。这是通过首先在当前视口中铸造成英寸,然后使用#current.transform()矩阵来获得设备画布中的英寸来完成的#。 x y< - convertY(unit(y,units),unitTo ='inches',valueOnly = TRUE) $ b $ transCoords transCoords< - (transCoords / transCoords [3]) return(#)最后,从英寸投射到本地设备单元c( grconvertX(transCoords [1],从='英寸'到='设备'), grconvertY(transCoords [2],from ='inches',to ='device'))) } 使用这个缺失的部分,可以使用 tikzAnnotate 来标记 grid 或 lattice plot: \\ usetikzlibrary {shapes.arrows})) tikz(standAlone = TRUE) xs ys pushViewport(plotViewport()) pushViewport(dataViewport(xs,ys)) grobs< - gList(grid.rect(),grid .xaxis(),grid.yaxis(),grid.points(xs,ys)) coords< - gridToDevice(17,7) tikzAnnotate(paste('\\\ (',coords [1],',',coords [2],'){look look over(单箭头,锚点= tip,draw,fill = green,left = 1em]','这里!};')) dev.off() 给出以下输出: 还有一些工作要做,例如: 创建一个可以添加到网格图形中的注释grob。 确定如何将这样的对象添加到 ggplot 。 计划在rel中出现缓解0.7元的 tikzDevice 。 I would like to use tikzDevice to include annotated ggplot2 graphs in a Latex document.tikzAnnotate help has an example of how to use it with base graphics, but how to use it with a grid-based plotting package like ggplot2? The challenge seems to be the positioning of the tikz node.playwith package has a function convertToDevicePixels (http://code.google.com/p/playwith/source/browse/trunk/R/gridwork.R) that seems to be similar to grconvertX/grconvertY, but I am unable to get this to work either.Would appreciate any pointers on how to proceed.tikzAnnotate example using base graphics 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$ytikzAnnotate(paste('\\node[single arrow,anchor=tip,draw,fill=green] at (', x,',',y,') {Look over here!};'))dev.off() 解决方案 Currently, tikzAnnotate only works with base graphics. When tikzAnnotate was first written, the problem with grid graphics was that we needed a way of specifying the x,y coordinates relative to the absolute lower left corner of the device canvas. grid thinks in terms of viewports and for many cases it seems the final coordinate system of the graphic is not known until it is heading to the device by means of the print function.It would be great to have this functionality, but I could not figure out a way good way to implement it and so the feature got shelved. If anyone has details on a good implementation, feel free to start a discussion on the mailing list (which now has an alternate portal on Google Groups) and it will get on the TODO list.Even better, implement the functionality and open a pull request to the project on GitHub. This is guaranteed to get the feature into a release over 9000 times faster than if it sits on my TODO list for months.UpdateI have had some time to work on this, and I have come up with a function for converting grid coordinates in the current viewport to absolute device coordinates: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') ) )}Using this missing piece, one can use tikzAnnotate to mark up a grid or lattice plot:require(tikzDevice)require(grid)options(tikzLatexPackages = c(getOption('tikzLatexPackages'), "\\usetikzlibrary{shapes.arrows}"))tikz(standAlone=TRUE)xs <- 15:20ys <- 5:10pushViewport(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()This gives the following output:There is still some work to be done, such as:Creation of a "annotation grob" that can be added to grid graphics.Determine how to add such an object to a ggplot.These features are scheduled to appear in release 0.7 of the tikzDevice. 这篇关于在tikzDevice中使用tikzAnnotate注释ggplot2图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 10:50