[英]R - the simplest way to move legend when plotting 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]'))
不幸的是,我的代碼不起作用(似乎沒有使用“底部”參數)。
嘗試將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]'))
您現在可以將side=
或line=
參數傳遞給legend.args
列表,以指定圖例標簽的位置(默認為side = 3
和line = 0
)。 例如,您可以使用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))
如果你想在圖的右邊或底部以外的某個地方繪制圖例,你必須首先用axes = FALSE
繪制你的柵格,然后重新繪制它,但傳遞參數legend.only = TRUE
和smallplot= c(xleft, xright, ybottom, ytop)
指定繪圖區域中繪制彩色框的位置。
訣竅在於legend.args
列表被傳遞給mtext()
函數,因此使用legend()
函數和使用x=
參數(例如,“bottom”,“ legend.args
mtext()
來定義基本R
繪圖中的圖例位置的標准方法無法獲得。
您可能認為axis.args
列表會有所幫助,因為這也可能(可能違反直覺)控制圖例的繪制方式。 axis.args
列表傳遞給axis()
函數以繪制圖例的一些特征,而axis()
函數有一個side=
參數,用於設置圖的哪一側(在我們的例子中是圖例)將是畫! 但不,在繪制光柵圖時, side=
參數是通過其他方式設置的。
你可能會問的是什么意思? 這是horizontal=
參數!
horizontal=
參數可以傳遞給plot()
調用,以表示您是否希望光柵圖例位於圖的右側(默認情況下, horizontal = FALSE
將axis()
side=
參數設置為axis()
函數調用中的4繪制圖例的底部或繪圖的底部(當horizontal = TRUE
,在繪制圖例的`axis()函數調用中將side=
參數設置為1)
以下是使用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.