How to plot raster layers in R in with no legend?

问题描述 投票:0回答:1

我正在尝试绘制一个侧面没有图例或比例尺的栅格图层。我不想使用 ggplot,只想使用 base R 或光栅包。这是因为我还想在没有图例的初始层上叠加额外的栅格层,这在 ggplot 中似乎很难做到。

这是我正在使用的栅格图层,想要在没有图例/比例尺的情况下绘制:

library(raster)
library(RColorBrewer)

# Define common variables
extent.x <- 100
extent.y <- 100
resol    <- 100

# Half-forest terrain layer
env <- raster(nrow=extent.x,ncol=extent.y,xmn=0,xmx=(extent.x*resol),ymn=0,ymx=(extent.y*resol),resolution=resol)
env[1:50 ,] <- 1 # Plains
env[51:100 ,] <- 2 # Forest
plot(env)

# An example layer that I would like to plot over the env layer
popLayer <- env
popLayer[,] <- 0
popLayer[50,50] <- 1000

# Define a new color gradient for the pop raster layer
rasterColors <- colorRampPalette(c("white", "red"))

# Set breaks of the gradient
cuts <- c(0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)

# Plot the env layer, then plot the pop layer on top of it and use the color gradient above
plot(env)
popLayer[popLayer == 0] <- NA
plot(popLayer, add = TRUE, col = rasterColors(2))

如您所见,两个比例尺也相互重叠并且不可读。我如何绘制没有图例的第一层,以便第二层的比例清晰易读?

我试过使用 par() 来操纵图例绘图,但我不确定如何将它应用到我正在绘制两个栅格图层的情况。如前所述,我也尝试过使用 ggplot,但这也被证明很难应用。

这是我的第一个问题,如果某些格式不正确,请提前致谢。

r plot raster r-raster
1个回答
0
投票

在第一个

legend = FALSE
调用中使用
plot

plot(env, legend = FALSE)
popLayer[popLayer == 0] <- NA
plot(popLayer, add = TRUE, col = rasterColors(2))

© www.soinside.com 2019 - 2024. All rights reserved.