太多的图例项目无法阅读

时间:2017-11-27 04:24:40

标签: r plot ggplot2 legend spatial

我有一个带有213个Ecoregions的SpatialPolygonsDataFrame来绘制。 我的问题是,我无法以一种我确实可以阅读传奇的方式组织传奇。我是新来的,我现在已经尝试了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()

现在,这是这个情节的样子: Ecoregions with messy legend

我已设法将此图例放在地图的右侧,但也过于重叠...我已尝试colorkey = FALSE并设置单独的legend ... < / p>

#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,但我还不知道使用这么难的包。 提前谢谢,非常感谢任何提示。

0 个答案:

没有答案