7

Here is a gradient color legend I created using rasterImage:

colfunc <- colorRampPalette(c("red", "blue"))
legend_image <- as.raster(matrix(colfunc(20), ncol=1))
plot.new()
rasterImage(legend_image, 0.9, 0, 1, 1)     
lbsq <- seq.int(0, 1, l=5)                                  
axis(4, at=lbsq, pos=1, labels=F, col=0, col.ticks=1, tck=-.05) 
mtext(lbsq, 4, -.2, at=lbsq, las=2, cex=.6)

I wish to add a thin black border surrounding the color legend. I tried to add lty = 1 in rasterImage, which did not work. My question is how to add a black border to the resultant image and adjust its color and width.

Patrick
  • 1,057
  • 9
  • 23

2 Answers2

2

You could slightly increase the margins and use box().

colfunc <- colorRampPalette(c("red", "blue"))
legend_image <- as.raster(matrix(colfunc(20), ncol=1))
plot.new()
rasterImage(legend_image, -.1, -.1, 1.1, 1.1)
box(lwd=3)

You could also play with the pars.

png('test.png', 600, 400)

plot.new()
par(new=TRUE, mar=c(5.8, 40, 4, 4))
rasterImage(legend_image, .9, 0, 1.1, 1.1)
box(lwd=1)
par(mar=c(5.4, 4, 3.9, 4.8)+.1, new=TRUE)
plot(1:10)

dev.off()

enter image description here

Note, that this is rather unstable while working with the plotting preview window, definitely use png() or pdf() instead as shown.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • If I set `rasterImage(legend_image, 0.9, 0, 1.1, 1.1)`, how could I adjust arguments for `box` so that the box encloses only the colored region, rather than the whole plotting region? – Patrick Jan 02 '22 at 13:19
  • @Patrick See update for a possible solution. I think you had in mind to add a main plot. You may also be interested in my [recent answer](https://stackoverflow.com/a/70522655/6574038). – jay.sf Jan 02 '22 at 13:41
  • Thank you. Yes, that is in fact a color legend. I have updated my question. In addition, I tried your edited code and I was returned "object op not found". – Patrick Jan 02 '22 at 13:50
  • @Patrick Last line (deleted) was just dirt, ignore it. – jay.sf Jan 02 '22 at 13:52
  • @Patrick See more reproducible update. – jay.sf Jan 02 '22 at 14:05
  • That works. Beyond the current code, I also wish to add legend ticks and tick labels as you did in you recent answer. I followed your code but was unnecessful. I also want to export the gradient color legend (with ticks and tick labels) to a PDF file. Only color legend is needed. The scatter plot is not a must. Thank you. – Patrick Jan 02 '22 at 14:23
  • @Patrick `pdf` works similar to `png`, see link in my answer. The ticks are discussed in [*@mnel'*s answer](https://stackoverflow.com/a/13355440/6574038) – jay.sf Jan 02 '22 at 14:27
  • 1
    @jay.sf Great solution! When I posted my answer, the OP had not yet updated the question. Now it makes more sense. – Dion Groothof Jan 02 '22 at 15:31
  • 1
    @Dion Thanks, your solution is also very clever! Take a look into the `?grconvertX` `?grconvertY`'s, might be interesting for you since you also seem to like low-level plots. – jay.sf Jan 02 '22 at 15:52
1

Using rect(), the following adds a black border.

colfunc <- colorRampPalette(c("red", "blue"))
legend_image <- as.raster(matrix(colfunc(20), ncol=1))
plot.new()
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col = "black")
rasterImage(legend_image, 0, 0, 1, 1)

enter image description here

Dion Groothof
  • 1,406
  • 5
  • 15
  • For my real analysis, I set `rasterImage(legend_image, 0.9, 0, 1.1, 1.1)`. Is it possible to adjust `rect` so that the borders encloses only the colored region AND the borders are of the same thickness on all four sides? – Patrick Jan 02 '22 at 13:22