在R图中轴= F时,par(xpd = NA)不起作用 [英] par(xpd=NA) does not work when axes=F in R plot

查看:701
本文介绍了在R图中轴= F时,par(xpd = NA)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个设备上的两个图之间绘制图像。重要的是,图像必须位于精确的中心,因此两个图都重叠。请参阅示例,其中我使用



保持外观,即没有轴,解决方法是添加一个轴col =white:

  par(mfrow = c(1,2),xpd = N A)
barplot( - (1:10),horiz = T,border = NA,axes = FALSE)
轴(1,labels = F,col =white)
# axis(1,labels = F,tick = F)#没有帮助
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的边距/尺寸有什么关系?



您是否有其他想法或解决方案如何在没有轴的中心实现徽标(未切割)在情节中?

解决方案

似乎传统和网格图形相互争斗以控制裁剪区域, 来自<​​a href=\"https://stat.ethz.ch/pipermail/r-help/2006-June/108358.html\" rel=\"nofollow noreferrer\">列出来自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 =单位(0.5,npc),
宽度=单位(0.1,npc),高度=单位(0.1,npc)))
barplot(1:10) ,horiz = T,border = FALSE,axes = F)

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




I want to plot an image in between two plots on one device. Importantly, the image has to be in the exact center, thus overlapping both plots. Please see the example, where I used the R logo as image:

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

results in a cut R logo:

maintaining the looks, i.e. without axes, a workaround is to add an axis with 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)

which returns what I want

but doesn't seem like a good solution to me.

Why is the logo cut in the first plot? What do axes have to do with margins / size of device / xpd?

Do you have other ideas or solutions how to achieve the logo (uncut) in the center without axes in the plots?

解决方案

It seems likely that the "traditional and grid graphics are fighting each other for control of the clipping region," from a list email from Paul Murrell. He suggests adding grid.clip() before the grid command, as follows. The xpd is no longer needed.

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)

I can't really explain how adding the axis changes the behavior though.

这篇关于在R图中轴= F时,par(xpd = NA)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆