Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 无边距的小绘图-线宽(lwd)等于1的边框不可见_R_Plot_Border_Margins - Fatal编程技术网

R 无边距的小绘图-线宽(lwd)等于1的边框不可见

R 无边距的小绘图-线宽(lwd)等于1的边框不可见,r,plot,border,margins,R,Plot,Border,Margins,我一直在尝试使用基本绘图功能制作一些非常小的线条图,但在尝试添加薄边框时,我遇到了问题 这是通过Windows7上的RGui,从绘图窗口保存png 这是我的密码: dev.new(width=1.3,height=0.3) par(mar=c(0,0,0,0)) set.seed(13) x <- 1:10 y <- runif(10) plot(x,y,type="n",xaxs="i",yaxs="i",ylim=c(0,1)) polygon( c(1,x,max(x),0

我一直在尝试使用基本绘图功能制作一些非常小的线条图,但在尝试添加薄边框时,我遇到了问题

这是通过Windows7上的RGui,从绘图窗口保存png

这是我的密码:

dev.new(width=1.3,height=0.3)
par(mar=c(0,0,0,0))

set.seed(13)
x <- 1:10
y <- runif(10)

plot(x,y,type="n",xaxs="i",yaxs="i",ylim=c(0,1))
polygon( c(1,x,max(x),0), c(0,y,0,0), col="lightblue", border=NA)
lines(x,y,lwd=1)

现在我可以通过将线宽增加到2来解决这个问题,但这似乎有点困难

box(lwd=2)


使用
rect
比如
rect(1,0,10,1)
似乎也不能给我一个合适的解决方案,因为底部和右侧的边界都不可见。

您是否考虑过给mar一个小的非零值:

dev.new(width=0.3,height=0.3)
par(mar=c(0.01,0.01,0.01,0.01))

set.seed(13)
x <- 1:10
y <- runif(10)

plot(x,y,type="n",xaxs="i",yaxs="i",ylim=c(0,1))
polygon( c(1,x,max(x),0), c(0,y,0,0), col="lightblue", border=NA)
lines(x,y,lwd=1)

box(lwd=1)
dev.new(宽度=0.3,高度=0.3)
par(mar=c(0.01,0.01,0.01,0.01))
种子(13)

x另一个解决方案库位于
grid
gridBase
包中。我们的想法是将
替换为
grid.rect

  • 使用
    gridBase
    获取
    base
    视口
  • 引入一些偏移(视口y)以显示底线
  • 减小视口的宽度以显示右侧线条
  • 这是我的代码:

    library(gridBase)
    sp <- baseViewports()
    vp <- sp$plot
    vp$width <- unit(0.999,'npc')
    vp$y <- unit(0.001,'npc')
    pushViewport(vp)
    grid.rect(gp=gpar(fill=NA))
    upViewport(1)
    

    感谢@baptiste的提示,回答我自己的问题,这是由RGui引起的设备相关问题。如果使用
    png
    将图像直接保存到文件中,则一切正常。例如:

    dev.new(width=1.3,height=0.3)
    # repeat from here onwards only for png call below
    par(mar=c(0,0,0,0))
    set.seed(13)
    x <- 1:10
    y <- runif(10)
    plot(x,y,type="n",xaxs="i",yaxs="i",ylim=c(0,1),bty="n")
    polygon( c(1,x,max(x),0), c(0,y,0,0), col="lightblue", border=NA)
    lines(x,y,lwd=1)
    box(lwd=1)
    
    …给出了正确的结果:


    Hmmh。。。这似乎确实适用于
    lwd=1
    ,以便在Win7/RGui上提供预期的结果。在我接受之前,我将拭目以待,看看是否有其他人对这背后的原因有任何智慧。我怀疑你已经“拉伸”了这幅图像,因为它的规格是一个小的“正方形”>@DWin-yep,RGui正在强制更大的宽度以适应窗口控件。我已将宽度调整为
    1.3
    ,这与上述图像的比例大致相同。但是,这不会影响根问题。如果绘图使用的是完整的设备窗口,为什么还要使用gridBase呢?单独使用grid.rect()也可以,不是吗?我无法复制原始版本,可能与设备有关。@baptiste你说得对!也许我太喜欢操纵
    视口了
    。。。或者也受到OP评论的影响,“使用类似rect的rect(1,0,10,1)似乎并没有给我一个合适的解决方案”…@baptiste-我没有考虑设备特定的问题。如果我直接进入
    png
    一切正常。如果我试图通过RGui保存图像,我会遇到问题。啊!
    library(grid)
    grid.rect(width = unit(0.999,'npc'),
              y = unit(0.5001, "npc"),
              gp=gpar(fill=NA))
    
    dev.new(width=1.3,height=0.3)
    # repeat from here onwards only for png call below
    par(mar=c(0,0,0,0))
    set.seed(13)
    x <- 1:10
    y <- runif(10)
    plot(x,y,type="n",xaxs="i",yaxs="i",ylim=c(0,1),bty="n")
    polygon( c(1,x,max(x),0), c(0,y,0,0), col="lightblue", border=NA)
    lines(x,y,lwd=1)
    box(lwd=1)
    
    png("textbox_direct.png",width=116,height=27)
    # take code block from above
    dev.off()