无法指定图例展示位置

时间:2017-02-20 16:15:11

标签: r raster

我正在制作一张光栅文件中的地图。我希望我的传奇在默认情况下不在外面。我正在使用此代码:

library(raster)
library(rgdal)
library(classInt)

NDII = raster("G:\\Sheyenne\\image_differencing\\NDII\\differenced.tif")
value.vector = round(values(NDII),2)
breaks.qt = classIntervals(value.vector, n = 3, style = "quantile", intervalClosure = "right")
breaks.qt = breaks.qt$brks
breaks.qt[2] = 0
xlim = c(616768.4, 646426)
ylim = c(5130933, 5159682)
print (plot(NDII, main="NDII", breaks=breaks.qt, col = rainbow(3), axes=F, xlim=xlim, ylim=ylim))
legend("topright")

但这会返回:

enter image description here

1 个答案:

答案 0 :(得分:1)

这是一个可重现的例子

二进制数据的示例

r <- raster(nrows = 50, ncols = 50)
set.seed(123)
id <- sample(1:2500, 100, replace = FALSE)
r[id] <- 1
plot(r)

enter image description here

现在使用legend = FALSE并在

之后添加图例
plot(r, legend = FALSE, col = rev(terrain.colors(2)))
legend("topright", legend = c("Absence", "Presence"), fill = rev(terrain.colors(2)))

enter image description here

连续数据的示例

r <- raster(nrows=10, ncols=10)
r <- setValues(r, 1:ncell(r))


plot(r, legend = FALSE, col = rev(terrain.colors(5)))
legend("topright", fill = rev(terrain.colors(5)),  legend = c("0", "20", "40", "60", "80", "100"))

enter image description here