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

时间:2015-12-07 11:57:53

标签: r plot r-raster

我需要绘制光栅文件。

在我的输出图像中,图例上的数字有时不可见 - 尤其是当我将图形窗口拆分为两列或更多列时(例如:par(mfrow = c(1,2)))。

我考虑过将图例移到底部(光栅图像下方)以解决此问题。 但是,我发现的大多数示例都建议使用全新的颜色和项目定义创建全新的图例。

我想使用默认图例。我只需将它移到底部。我试着这样做:

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]'))

不幸的是,我的代码无效(似乎没有使用“bottom”参数)。

What I need to achieve

3 个答案:

答案 0 :(得分:3)

TL;博士

尝试将SELECT cast(scantime as date) as [Date] ,d.deviceid as [DeviceId] ,CASE WHEN AVG([cpu_usage]) IS NULL THEN 0 ELSE AVG([cpu_usage]) END as [CPU] FROM [ods_10_ds1].[dbo].[datacpu_detailed] [data] join task t on [data].taskid = t.taskid join device d on t.deviceid = d.deviceid join customer c on d.customerid = c.customerid where cast(scantime as date) >= @startdate and cast(scantime as date) <= @enddate and deviceclassid = 106 and enabled = 1 and d.deviceid in (1433949,9667189) group by cast(scantime as date), d.deviceid order by cast(scantime as date), d.deviceid 传递给horizontal = TRUE函数调用。

plot()

enter image description here

调整菜谱方案

您现在可以将library('raster') data(volcano) r <- raster(volcano) plot(r, legend.only=TRUE, horizontal = TRUE, legend.args = list(text='My values [m^3]')) side=个参数传递到line=列表,以指定图例标签的位置(默认为legend.argsside = 3) 。例如,您可以使用line = 0将“我的值[m ^ 3]”文本放在图例下方,并使用side = 1将其与图例中的位置相隔一些(相对于它的顶部!)

line = 2

高级调整

如果你想在图的右边或底部以外的地方绘制图例,你必须首先用plot(r, legend.only=TRUE, horizontal = TRUE, legend.args = list(text='My values [m^3]', side = 1, line = 2)) 绘制你的光栅,然后重新绘制它,但作为参数传递{{1 }和axes = FALSE指定绘图区域中绘制彩色框的位置。

背景

诀窍在于legend.only = TRUE列表会传递给smallplot= c(xleft, xright, ybottom, ytop)函数,因此在legend.args基础mtext()中使用R函数定义图例位置的标准方法使用legend()参数(例如,“bottom”,“bottomright”)不可用。

您可能会认为x=列表会有所帮助,因为这也可能(可能违反直觉)控制图例的绘制方式。 axis.args列表将传递给axis.args函数以绘制图例的某些功能,axis()函数具有axis()参数,用于设置图的哪一侧为轴(在我们的例子中,传说)将被绘制!但不,在绘制栅格图时,side=参数是通过其他方式设置的。

您可能会问的是什么意思?这是side=参数!

horizontal=参数可以传递给horizontal=调用,以表明您是否希望栅格图例位于图的右侧(默认情况下,plot()设置horizontal = FALSE 1}}绘制图例的side=函数调用中的4的参数)​​或绘图的底部(当axis()在{轴中将horizontal = TRUE参数设置为1时( )绘制图例的函数调用

答案 1 :(得分:0)

Here是一种使用rasterVis的方式:

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

enter image description here

答案 2 :(得分:0)

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

spplot(r, scales = list(draw = TRUE), colorkey = list(space = "bottom"))
相关问题