在同一绘图中为栅格指定不同的图例断点

时间:2018-04-02 00:57:17

标签: r plot legend raster r-raster

如果我想在绘制光栅时提供我自己的图例分类,我可以这样做:

library(raster)
data(volcano)
volcanoR <- raster(volcano)

breakpoints <- c(94,100,120,140,160,180,195)
colors <- c("red","green4","blue","orange","pink","black")
plot(volcanoR,breaks=breakpoints,col=colors)

enter image description here

但是,我的数据是这样的:

set.seed(123)

df <- data.frame(X=seq(1,30,by = 1), Y=seq(1,30,by=1),var1=sample(1:20,30,replace = T), 
             var2=sample(40:80,30,replace = T),var3=sample(110:120,30,replace = T), 
             var4 = sample(200:210,30,replace = T))
ras <- rasterFromXYZ(df)
plot(ras) # bad example but gets my point across

enter image description here

如何指定每个图的断点?对于例如对于var1我想指定断点2,4,8,10,15,20。对于var2,45,60,80等。 我这样做了:

plot(ras, breaks = c(2,4,8,10,15,20))

enter image description here

但这为所有四个光栅提供了相同的断点。

1 个答案:

答案 0 :(得分:0)

你可以这样做。

library(raster)
library(viridis) # For color schemes

# Create the raster brick
ras <- rasterFromXYZ(df)

# Set 4 figures arranged in 2 rows and 2 columns
par(mfrow = c(2, 2))

# Plot var1
plot(ras$var1, breaks = c(2, 4, 8, 10, 15, 20), col = viridis(5))

# Plot var2
plot(ras$var2, breaks = c(45, 60, 80), col = viridis(2))

# Plot var3
plot(ras$var3, breaks = c(110, 113, 116, 120), col = viridis(3))

# Plot var4
plot(ras$var4, breaks = c(200, 201, 202, 204, 206, 208, 210), col = viridis(6))

enter image description here