为 rasterImage 添加边框

Pat*_*ick 7 graphics r raster r-raster

这是我使用创建的渐变颜色图例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)
Run Code Online (Sandbox Code Playgroud)

我希望在颜色图例周围添加一个细黑色边框。我尝试添加lty = 1rasterImage但没有成功。我的问题是如何向生成的图像添加黑色边框并调整其颜色和宽度。

jay*_*.sf 2

您可以稍微增加边距并使用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)
Run Code Online (Sandbox Code Playgroud)

你也可以玩s par

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请注意,在使用绘图预览窗口时,这相当不稳定,一定要使用png()pdf()代替,如图所示。