如何使用 grImport 导入的图形作为 ggplot2 中的轴刻度标签(使用网格函数)?
我希望能够将 grImport
出色的导入和绘图功能与 ggplot2
强大的绘图功能结合起来,但我根本不理解 grid< /code> 系统足够好,可以找到一种优雅的方式来实现我想要的。我想要的是将 ggplot2 图中的 x 轴刻度标签替换为使用 grImport 导入的图像。
由于这两个包都使用 grid 函数,我希望有一种方法可以在 ggplot2 框架内使用 grid.symbols() ,这将是理想的,或者至少在设备中的现有绘图上。对此类事情有更多了解的人是否知道一种可靠的方法来做到这一点?或者,有人可以向我指出更多信息来帮助我了解 grobs、视口等吗?我已通读 Paul Murrel 的 R 图形书的免费章节网格图形模型,但我对 ggplot2 的内部工作原理不够熟悉,无法建立链接。
我的问题与 有关使用图像作为绘图点,尽管我对轴标签而不是绘图点更感兴趣。但是,我很好奇这两个任务有多么相似,以及该解决方案是否可以适用于此目的。我自己无法弄清楚。
这是我的第一篇文章,所以我不允许发布图像,但这段代码实现了接近我想要的效果。注意:这种方法使用了一种非常丑陋的 hack,它不可移植且不能令人满意。我想要生成的最终图将具有面(facet_grid
),并且 x 轴用于一个因子每个轴刻度处具有不同的图片,而不是连续变量,这就是为什么我正在寻找一种更通用/更强大的解决方案,不需要大量的试验和测试。错误。
library(ggplot2)
## library(grImport) # not needed for this example, but would be for grid.symbols()
p <- ggplot(mtcars, aes(cyl, mpg)) + stat_summary(fun.data = "mean_cl_boot")
print(p)
## Replace (in this case, overlay) x-axis tick labels with a graphic / grob
iconSize <- 0.05
iconHt <- 0.2
padding <- 0.09 # horizontal padding around axis: I found this by trial & error
tickSp <- (1-padding)/(4*2)
downViewport("axis_h-5-3")
## I would use grid.symbols() with an imported Picture in place of grid.circle(),
## but the idea is the same: draw a shape at the ticks along the axis.
for (i in 0:(max(mtcars$cyl) - min(mtcars$cyl)) )
{
grid.circle(x = (padding/2 + tickSp*(i*2)), y = iconHt,
r = iconSize*(min(mtcars$cyl)+i), gp = gpar(fill="black"))
}
upViewport()
I'm hoping I can combine the spiffy importing and drawing powers of grImport
with the awesome graphing power of ggplot2
, but I simply don't understand the grid
system well enough to find an elegant way of achieving what I want. What I want is to replace the x-axis tick labels in a ggplot2
graph with images imported using grImport
.
Since both packages use grid
functions, I'm hoping there is a way to use grid.symbols()
either within the ggplot2
framework, which would be ideal, or at least on an existing plot in a device. Does anyone with more knowledge of such things know of a robust way to do this? Or, can anyone point me to more information to help me learn about grobs, viewports, etc.? I have read through the free Chapter of Paul Murrel's R graphics book on the Grid graphics model, but I'm not familiar enough with the inner workings of ggplot2 to make the link.
My question is very similar to an existing question about using an image as a plot point, although I'm more interested in the axis labels rather than the plot points. However, I would be curious as to how similar the two tasks are, and if that solution can be adapted for this purpose. I wasn't able to figure it out on my own.
This is my first post, so I'm not allowed to post an image, but this code achieves something close to what I want. Note: this approach uses an ugly, ugly hack that is not portable, and unsatifactory. The final plot I want to produce will have facets (facet_grid
), and the x-axis is for a factor with different pictures at each axis tick, not a continuous variable, which is why I'm looking for a more general / robust solution that doesn't require a lot of trial & error.
library(ggplot2)
## library(grImport) # not needed for this example, but would be for grid.symbols()
p <- ggplot(mtcars, aes(cyl, mpg)) + stat_summary(fun.data = "mean_cl_boot")
print(p)
## Replace (in this case, overlay) x-axis tick labels with a graphic / grob
iconSize <- 0.05
iconHt <- 0.2
padding <- 0.09 # horizontal padding around axis: I found this by trial & error
tickSp <- (1-padding)/(4*2)
downViewport("axis_h-5-3")
## I would use grid.symbols() with an imported Picture in place of grid.circle(),
## but the idea is the same: draw a shape at the ticks along the axis.
for (i in 0:(max(mtcars$cyl) - min(mtcars$cyl)) )
{
grid.circle(x = (padding/2 + tickSp*(i*2)), y = iconHt,
r = iconSize*(min(mtcars$cyl)+i), gp = gpar(fill="black"))
}
upViewport()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个示例:
here is an example:
对于单个面板,从 x 轴 grob 中提取信息并将其替换为您自己的信息是相当简单的。我不确定如何将其干净地扩展到自动多面板轴。
For a single panel, it is fairly straight-forward to extract information from the x axis grob, and replace it with your own. I'm not sure how this could be extended cleanly to automatic, multi-panel axes.
下面是使用自定义 grob 作为轴标签的技巧。
Below is a hack to use a custom grob for axis labels.