2017-11-27 10 views
0

私はプロットする213のEcoregionsを持つSpatialPolygonsDataFrameを持っています。 私の問題は、私が実際に伝説を読むことができる方法で伝説を編成することができないということです。私はrの新人だし、2日間これを試してきた、私は本当にばかげている...誰かが私にこの目標を達成する方法のヒントを与えることができるかと思います。 Ecoregions with messy legendあまりにも多くの伝説のitensは読むことが不可能になります

私は、地図の右側にこの伝説を置くことに成功しましたが、あまりにもオーバーレイします...私がしようと試みてきた:さて、これはこのプロットはどのように見えるかさ

#### 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() 

colorkey = FALSEを行うと、別のlegend ...

#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, 

のNcoI = 3)

しかし、動作しません。..を設定するために、私はそのメッセージを取得しますplot.new has not been called yet 私は伝説で多くのことをすることができましたが、私はそれを良くすることはできません...伝説のitensは、長い図形の2つまたは3つの列にマップを鳴らしているように...実際に形式を問わず、良い姿勢を作りたいと思っていました。誰かが私をある方向に向けることができますか?私はggplot2を学ぼうとしていますが、このような難しいパッケージを使用するにはまだ十分にわかりません。 ありがとうございます、どんなヒントも高く評価されています。

+2

、あなたを私はあなたのデータセットを持っていないが、あなたは直接スクリプトを使用できるように、私はあなたと同じ名前を使用するように私には、再現性の例を使用しました213の異なる色彩/カテゴリの賢明なプロットを作ることはできません。おそらく、プロットの中でどんな種類のもっきりした感覚を生み出すためにはっきりした6色または7色しかありません。通常、各地域の成果(人口密度、降水量など)のマップをプロットすると、離散カテゴリではなくスケールを持つことができます。 – thelatemail

+0

一度に大きな領域をプロットする必要がありますか?ビューの細かさを決める必要があります。より小さな領域の複数のプロットを検討するか、またはそれらをより大きなグループに集約します。 –

+0

はい、それは213の本当に異なる色を持つことはできません...しかし、私は全体的な外観のためにこの地図を提示する必要があります、誰も本当に場所と伝説を確認することはありませんが、そこにする必要があります...私はマップと伝説を使用してPhotoshopを使って、それほど恐ろしい方法ではないかもしれません。たぶん地図だけで、伝説だけの次のページがあります。 – 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

セバスチャン、ありがとう!私はあなたから今までに学んだこのグリッドのものを今使っています!マップが間違っていることがわかりました(ポリゴンは色の列に応じて色を取得していません...)。しかし、伝説のコードは完全に機能します。ありがとうございました – Thai

関連する問題