r - 当 R 图中axes=F 时,par(xpd=NA) 不起作用

标签 r image plot

我想在一台设备上的两个绘图之间绘制图像。重要的是,图像必须位于正中心,从而使两个图重叠。请参阅示例,其中我使用了 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 :

R logo gets cut

保持外观,即没有轴,解决方法是添加带有 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)

返回我想要的内容

plot with logo

但对我来说这似乎不是一个好的解决方案。

为什么第一幅图中的标志被剪掉了?轴与设备的边距/大小/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)

我无法真正解释添加轴如何改变行为。

enter image description here

关于r - 当 R 图中axes=F 时,par(xpd=NA) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37658895/

相关文章:

javascript - 如何在 HTML5 Canvas 上缩放图像时保持宽高比?

r - 绘图标签中的表达式和新行

matlab - 等值面函数 MATLAB 用法

r - 在 R 中绘制表达式树

r - sapply - 保留列名称

r - 如何将包含 NULL 值的列表列表转换为数据框

r - 如何用spss文件中的data.frames中的标签替换值?

r - 如何根据变量是否出现在另一个数据框中在 R 中创建另一列

html - 将图像放在 css 文件而不是 html 文件中

image - Okami sumie风格渲染算法