Question

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.

Was it helpful?

Solution 3

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

OTHER TIPS

Have you considered giving mar a small non-zero value:

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)

I admit I haven't quite figured out what the end-game might be, but when I do an interactive "stretch" of that very small screen-object, it does result in an all-around border.

enter image description here

I do recognize that I am on a Mac and saving this to a pdf file and converting it to a png file for SO-inclusion may not be precisely reproducible on a Linux or Windows device.

Another solution base in grid and gridBase package. The idea is to replace the box by grid.rect.

  1. Use gridBase to get the base viewport
  2. Introduce some offset (viewport y ) to show the bottom line
  3. Reduce the width of the viewport to show the right line.

Here my code:

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)

enter image description here

EDIT thanks to @baptiste, you can simply get the same result using only grid.rect:

library(grid)
grid.rect(width = unit(0.999,'npc'),
          y = unit(0.5001, "npc"),
          gp=gpar(fill=NA))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top