简体   繁体   English

R - 绘制栅格时移动图例的最简单方法

[英]R - the simplest way to move legend when plotting raster

I need to plot raster files. 我需要绘制光栅文件。

In my output image, numbers on the legend are sometimes not visible - especially when I split graph window to two or more columns (for example: par(mfrow=c(1,2))). 在我的输出图像中,图例上的数字有时不可见 - 尤其是当我将图形窗口拆分为两列或更多列时(例如:par(mfrow = c(1,2)))。

I thought about moving legend to the bottom (below raster image) to resolve this issue. 我想过将图例移到底部(光栅图像下方)以解决此问题。 However, most examples which I found suggest creating completely new legend with completely new colors and items definitions. 但是,我发现的大多数示例都建议使用全新的颜色和项目定义创建全新的图例。

I would like to use the default legend. 我想使用默认图例。 I just need to move it to the bottom. 我只需将它移到底部。 I've tried to do this as follows: 我试过这样做如下:

library('raster')
data(volcano)
r <- raster(volcano)
# Trying to draw default legend below raster plot
plot(r, legend=F)
# Now trying to draw legend. Default is okay for me, I want to move it below only:
plot(r, legend.only=TRUE, legend.args=list("bottom", text='My values [m^3]'))

Unfortunately, my code is not working (seems "bottom" parameter is not used). 不幸的是,我的代码不起作用(似乎没有使用“底部”参数)。

我需要实现的目标

tl;dr TL;博士

Try passing horizontal = TRUE to the plot() function call. 尝试将horizontal = TRUE传递给plot()函数调用。

library('raster')

data(volcano)
r <- raster(volcano)

plot(r, legend.only=TRUE, horizontal = TRUE, legend.args = list(text='My values [m^3]'))

在此输入图像描述

Tweaks 调整

You can now pass side= or line= arguments to the legend.args list to specify where you want the legend label (default is side = 3 and line = 0 ). 您现在可以将side=line=参数传递给legend.args列表,以指定图例标签的位置(默认为side = 3line = 0 )。 For instance, you could put the "My values [m^3]" text below the legend by using side = 1 and space it a bit further from the legend (versus on top of it!) using line = 2 . 例如,您可以使用side = 1将“我的值[m ^ 3]”文本放在图例下方,并使用line = 2将其与图例中的距离相对较远(相对于它的顶部!)。

plot(r, legend.only=TRUE, horizontal = TRUE, 
        legend.args = list(text='My values [m^3]', side = 1, line = 2))

Advanced tweaks 高级调整

If you want to plot the legend somewhere other than the right or bottom of the plot, you'll have to look into first plotting your raster with axes = FALSE and then plotting it again but passing as arguments legend.only = TRUE and smallplot= c(xleft, xright, ybottom, ytop) to specify where in the plotting region to draw the colored box. 如果你想在图的右边或底部以外的某个地方绘制图例,你必须首先用axes = FALSE绘制你的栅格,然后重新绘制它,但传递参数legend.only = TRUEsmallplot= c(xleft, xright, ybottom, ytop)指定绘图区域中绘制彩色框的位置。

Background 背景

The trick is that the legend.args list gets passed to the mtext() function, so the standard way of defining legend locations in base R plotting with the legend() function and using the x= argument (eg, "bottom", "bottomright") isn't available. 诀窍在于legend.args列表被传递给mtext()函数,因此使用legend()函数和使用x=参数(例如,“bottom”,“ legend.args mtext()来定义基本R绘图中的图例位置的标准方法无法获得。

You might think that the axis.args list would help, since that also (counterintuitively perhaps) controls how the legend is drawn. 您可能认为axis.args列表会有所帮助,因为这也可能(可能违反直觉)控制图例的绘制方式。 The axis.args list is passed to the axis() function to draw some features of the legend, and the axis() function has a side= argument that sets which side of the plot the axis (in our case the legend) will be drawn! axis.args列表传递给axis()函数以绘制图例的一些特征,而axis()函数有一个side=参数,用于设置图的哪一侧(在我们的例子中是图例)将是画! But no, the side= argument is set by other means during the drawing of the raster plot. 但不,在绘制光栅图时, side=参数是通过其他方式设置的。

What means are those you might ask? 你可能会问的是什么意思? It's the horizontal= argument! 这是horizontal=参数!

The horizontal= argument can be passed to the plot() call to say whether you want the raster legend to be on the right of the plot (the default, horizontal = FALSE sets the side= argument to 4 in the axis() function call that draws the legend) or on the bottom of the plot (when horizontal = TRUE which sets the side= argument to 1 in the `axis() function call that draws the legend) horizontal=参数可以传递给plot()调用,以表示您是否希望光栅图例位于图的右侧(默认情况下, horizontal = FALSEaxis() side=参数设置为axis()函数调用中的4绘制图例的底部或绘图的底部(当horizontal = TRUE ,在绘制图例的`axis()函数调用中将side=参数设置为1)

Here is a way using rasterVis : 以下是使用rasterVis的方法:

library(rasterVis)
levelplot(r, margin=FALSE, colorkey=list(space="bottom"), par.settings = RdBuTheme())

在此输入图像描述

或者尝试spplot ,它也使用包lattice

spplot(r, scales = list(draw = TRUE), colorkey = list(space = "bottom"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM