图例项目太多,无法阅读

Too many legend items making it impossible to read

我有一个包含 213 个生态区的 SpatialPolygonsDataFrame 要绘制。 我的问题是我无法以我确实可以阅读图例的方式组织图例。我是 r 的新手,我已经尝试了 2 天了,我觉得真的很愚蠢......我想知道是否有人可以给我一些关于如何实现这个目标的提示。

#### Download and unzip ecoregions ####
#the reference for this ecoregions data: https://doi.org/10.1093/biosci/bix014

#Don't forget to change the path to a path of your own
dir_eco<-"C:/Users/thai/Desktop/Ecologicos/w2"


download.file("https://storage.googleapis.com/teow2016/Ecoregions2017.zip", 
              file.path(paste0(dir_eco,"/","Ecoregions2017.zip",sep=""))) 


unzip("Ecoregions2017.zip")

#Read this shapefile
#install.packages("rgdal")
library(rgdal)
ecoreg_shp<- readOGR("Ecoregions2017.shp")

#Crop to a smaller extent
xmin=-120; xmax=-35; ymin=-60; ymin2=-40; ymax=35
limits2 <- c(xmin, xmax, ymin2, ymax) # Just from mexico to Uruguay.
ecoreg_shp<-crop(ecoreg_shp,limits2)

# Color palette - one color for each attribute level
n <- 213
color = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)] 
# pie(rep(1,n), col=sample(color, n)) #just to take a look at the colors
col_samp<-sample(color, n)
ecoreg_shp@data$COLOR<-col_samp #put the colors in the polygons data frame

#Plot
png(file="29_ecoreg2.png", width=3000, height=3000, units="px", res=300)
par(mar=c(50,0.3,1.5,0),pty="s")
spplot(ecoreg_shp, zcol = "ECO_NAME", col.regions = ecoreg_shp@data$COLOR, 
       colorkey = list(space = "bottom", height = 1))
dev.off()

现在,这个情节是这样的:

我已经设法将这个图例放在地图的右侧,但也被过度覆盖了...我已经尝试 colorkey = FALSE 并设置一个单独的 legend.. .

#Plot the map with no legend
spplot(ecoreg_shp, zcol = "ECO_NAME", col.regions = ecoreg_shp@data$COLOR, 
       colorkey = FALSE)
#Now, just the legend
legend("bottom",legend=ecoreg_shp@data$ECO_NAME,fill=ecoreg_shp@data$COLOR, ncol=3)

但不起作用..我收到一条消息 plot.new has not been called yet 我已经设法用图例做了很多事情,但我不能做得很好......就像地图下方的图例项目在一个长图中的 2 或 3 列......实际上无关紧要根本没有格式,我只是想能够做出一个好图。谁能指出我的方向?我正在尝试学习 ggplot2,但我对使用如此困难的包还不够了解。 提前谢谢你,非常感谢任何提示。

正如评论中所说,您将无法真正区分颜色。您应该定义一个具有多个级别的分类,并为相似的生态区选择相似的颜色。
不过,您可以只为这个长图例创建一个图像,如下所示。我使用了一个可重现的示例,因为我没有您的数据集,但我使用与您相同的名称,以便您可以直接使用脚本:

library(sp)
library(rgdal)

n <- 213

dsn <- system.file("vectors", package = "rgdal")[1]
ecoreg_shp <- readOGR(dsn = dsn, layer = "cities")
ecoreg_shp <- ecoreg_shp[1:n,]

# Color palette - one color for each attribute level
color <- grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)] 
col_samp <- sample(color, n)
ecoreg_shp@data$COLOR <- col_samp #put the colors in the polygons data frame
ecoreg_shp@data$ECO_NAME <- ecoreg_shp@data$NAME

# Define a grid to plot the legend
grid.dim <- c(45, 5)
ecoreg_shp@data$ROW <- rep(rev(1:grid.dim[1]), by = grid.dim[2], length.out = n)
ecoreg_shp@data$COL <- rep(1:grid.dim[2], each = grid.dim[1], length.out = n)

# Plot the legend
png(file = "legend.png",
    width = 21, height = 29.7,
    units = "cm", res = 300)
par(mai = c(0, 0, 0, 0))

plot(ecoreg_shp@data$COL,
     ecoreg_shp@data$ROW,
     pch = 22, cex = 2, 
     bg = ecoreg_shp@data$COLOR,
     xlim = c(0.8, grid.dim[2] + 1),
     xaxs = "i")
text(ecoreg_shp@data$COL,
     ecoreg_shp@data$ROW,
     ecoreg_shp@data$ECO_NAME, 
     pos = 4, cex = 0.75)

dev.off()

结果: