Search code examples
rplotbordermargins

small plot with no margins - border with line width (lwd) equal to 1 not visible


I've been trying to make some very tiny line graphs using base plotting functions, but am coming unstuck when trying to add a thin border.

This is via RGui on Windows 7, saving a png from the plot window.

Here's my code:

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)

Everything is fine until I try to add a box with a line width of 1, giving:

box(lwd=1)

enter image description here

Now I can solve this by increasing the line width to 2, but this seems a bit of a hack.

box(lwd=2)

enter image description here

Using rect like rect(1,0,10,1) doesn't seem to give me an appropriate solution either, with the bottom and right borders not being visible.


Solution

  • To answer my own question thanks to @baptiste's tip-off, this is a device dependent issue due to RGui. If the image is saved directly out to file using png everything works as intended. E.g.:

    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)
    

    Saving out to png from the "R Graphics" window gives my original stuffed up image:

    enter image description here

    Going directly to file using png like:

    png("textbox_direct.png",width=116,height=27)
    # take code block from above
    dev.off()
    

    ...gives the correct result:

    enter image description here