2017-11-27 16 views
0

213 개의 Ecoregions가있는 SpatialPolygonsDataFrame을 플롯합니다. 제 문제는 전설을 실제로 읽을 수있는 방식으로 전설을 구성 할 수 없다는 것입니다. 나는 2 일 동안 이것을 시도해 보았고, 정말 어리 석다. 누군가이 목표를 달성하는 방법에 대한 힌트를 줄 수 있는지 궁금하다. 내가지도의 오른쪽에있는이 전설을 넣어 관리했습니다 Ecoregions with messy legend읽을 수 없게하는 범례 itens가 너무 많음

, 또한 너무 오버레이됩니다 ... 내가 tryed했습니다 : 이제

#### 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) 
[email protected]$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 = [email protected]$COLOR, 
     colorkey = list(space = "bottom", height = 1)) 
dev.off() 

이이 음모 같은 모습입니다

#Plot the map with no legend 
spplot(ecoreg_shp, zcol = "ECO_NAME", col.regions = [email protected]$COLOR, 
     colorkey = FALSE) 
#Now, just the legend 
legend("bottom",[email protected]$ECO_NAME,[email protected]$COLOR, 

는 = 3)

그러나 작동하지 않습니다 .. 내가 메시지를 받게에는 NcoI ... colorkey = FALSE을하고 별도의 legend 설정이 plot.new has not been called yet 나는 전설에 많은 것을 할 수 있었다. 그러나 나는 그것을 좋게 만들 수 없다. .. 전설 itens가 긴 그림의 2 또는 3 개의 열에서지도를 뒤덮는 것처럼 ... 실제로 전혀 형식에 문제가 있습니다. 나는 단지 좋은 인물을 만들고 싶었습니다. 누구든지 어떤 방향으로 나를 가리킬 수 있습니까? 나는 ggplot2를 배우려고 노력하고 있지만, 어려운 패키지를 사용하기에는 아직 충분히 알지 못합니다. 미리 감사드립니다. 팁을 주시면 감사하겠습니다.

+2

, 당신을 나는 데이터 집합을 가지고 있지 않지만 직접 스크립트를 사용할 수 있도록 내가 당신과 같은 이름을 사용할 때 나는 재현 예를 사용 213 가지 색상/카테고리를 가진 합리적인 음모를 만들 수 없을 것입니다. 줄거리에서 어떤 종류의 명확한 감각을 만들기에 충분할만큼 뚜렷한 6 ~ 7 색만있을 것입니다. 일반적으로 각 지역의 결과 (인구 밀도, 강우량 등)에 대한지도를 작성하면 개별 카테고리가 아닌 규모를 가질 수 있습니다. – thelatemail

+0

한 번에 큰 영역을 그릴 필요가 있습니까? 뷰의 세밀한 정도를 결정해야합니다. 더 작은 영역의 여러 플롯을 고려하거나 더 큰 그룹으로 집합하십시오. –

+0

그래, 정말 별개의 213 가지 색상을 가질 수는 없지만 ...이지도를 전반적인 모양으로 표현해야합니다. 아무도 장소와 전설을 실제로 확인할 수는 없지만 거기에 있어야합니다 ... 나는 지도 및 전설과 포토샵을 사용하여 덜 끔찍한 방법으로 ... 어쩌면 단지지도가있는 페이지와 전설이있는 다음 페이지로 이동합니다. – Thai

답변

1

주석에서 언급했듯이 색상을 구분할 수는 없습니다. 여러 생태계 분류를 정의하고 유사한 생태 지역에 대해 유사한 색상을 선택해야합니다.
그럼에도 불구하고 다음과 같이이 긴 범례에 대해서만 이미지를 만들 수 있습니다.

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) 
[email protected]$COLOR <- col_samp #put the colors in the polygons data frame 
[email protected]$ECO_NAME <- [email protected]$NAME 

# Define a grid to plot the legend 
grid.dim <- c(45, 5) 
[email protected]$ROW <- rep(rev(1:grid.dim[1]), by = grid.dim[2], length.out = n) 
[email protected]$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([email protected]$COL, 
    [email protected]$ROW, 
    pch = 22, cex = 2, 
    bg = [email protected]$COLOR, 
    xlim = c(0.8, grid.dim[2] + 1), 
    xaxs = "i") 
text([email protected]$COL, 
    [email protected]$ROW, 
    [email protected]$ECO_NAME, 
    pos = 4, cex = 0.75) 

dev.off() 

결과 : 일반적인 주석으로 colors and legends

+0

Sébastien, 대단히 감사합니다! 나는 너에게 지금까지 영원히 배운이 그리드를 사용할 것이다! ou에서 내지도가 잘못되었음을 발견했습니다 (폴리곤은 색상 열에 따라 색상이 표시되지 않습니다 ...). 그러나 범례 코드는 완벽하게 작동합니다. 고맙습니다 – Thai