我想在一台设备上的两个绘图之间绘制图像。重要的是,图像必须位于正中心,从而使两个图重叠。请参阅示例,其中我使用了 R logo如图所示:
# png and grid are both for plotting the image
library("png")
library("grid")
Rlogo <- readPNG("Rlogo.png")
par(mfrow=c(1,2), xpd=NA) # two columns, xpd should permit plotting outside of margin
barplot(-(1:10), horiz=T, border=NA, axes=FALSE)
grid.draw(rasterGrob(Rlogo, x=unit(0.5, "npc"), y = unit(0.5, "npc"), width=unit(0.1, "npc"), height=unit(0.1, "npc")))
barplot(1:10, horiz=T, border=NA, axes=F)
导致切割 R Logo :
保持外观,即没有轴,解决方法是添加带有 col="white"的轴:
par(mfrow=c(1,2), xpd=NA)
barplot(-(1:10), horiz=T, border=NA, axes=FALSE)
axis(1, labels=F, col = "white")
# axis(1, labels=F, tick=F) # does not help
grid.draw(rasterGrob(Rlogo, x=unit(0.5, "npc"), y = unit(0.5, "npc"), width=unit(0.1, "npc"), height=unit(0.1, "npc")))
barplot(1:10, horiz=T, border=NA, axes=F)
返回我想要的内容
但对我来说这似乎不是一个好的解决方案。
为什么第一幅图中的标志被剪掉了?轴与设备的边距/大小/xpd 有什么关系?
您是否有其他想法或解决方案如何在图中实现中心的 Logo (未切割)而没有轴?
最佳答案
似乎“传统图形和网格图形正在相互争夺剪切区域的控制权”,来自 list email from Paul Murrell 。他建议在 grid 命令之前添加 grid.clip() ,如下所示。不再需要 xpd
。
library("png")
library("grid")
Rlogo <- readPNG("Rlogo.png")
par(mfrow=c(1,2))
barplot(-(1:10), horiz=T, border=NA, axes=FALSE,)
grid.clip()
grid.draw(rasterGrob(Rlogo, x=unit(0.5, "npc"), y = unit(0.5, "npc"),
width=unit(0.1, "npc"), height=unit(0.1, "npc")))
barplot(1:10, horiz=T, border=FALSE, axes=F)
我无法真正解释添加轴如何改变行为。
关于r - 当 R 图中axes=F 时,par(xpd=NA) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37658895/