2017-08-06 16 views
3

ラスタの束をプロットしたいと思います。それぞれの罫線を調整し、forループをプロットするコードを作成しました。しかし、私は問題のあるカラースケールバーを手に入れています。私の努力はそれを解決するのに有効ではありません。例:プロットの問題 - 凡例の尺度、凡例、小数点の尺度

私は降水量が0から11.000の範囲ですが、データの大半は0から5.000まで、そしてほとんどは11.000までです。だから私はこのバリエーションをキャプチャするために休憩を変更する必要があります...私はより多くのデータを持っています。

次に、そのためのブレークオブジェクトを作成しました。
しかし、私は、ラスタをプロットすると、スケールのカラーバーは非常に厄介な、ひどいます...

#get predictors (These are a way lighter version of mine) 
predictors_full<-getData('worldclim', var='bio', res=10) 

predic_legends<-c(
"Annual Mean Temperature [°C*10]", 
"Mean Diurnal Range [°C]", 
"Isothermality", 
"Temperature Seasonality [standard deviation]", 
"Max Temperature of Warmest Month [°C*10]", 
"Min Temperature of Coldest Month [°C*10]", 
"Temperature Annual Range [°C*10]", 
"Mean Temperature of Wettest Quarter [°C*10]", 
"Mean Temperature of Driest Quarter [°C*10]", 
"Mean Temperature of Warmest Quarter [°C*10]", 
"Mean Temperature of Coldest Quarter [°C*10]", 
"Annual Precipitation [mm/year]", 
"Precipitation of Wettest Month [mm/month]", 
"Precipitation of Driest Month [mm/month]", 
"Precipitation Seasonality [coefficient of variation]", 
"Precipitation of Wettest Quarter [mm/quarter]", 
"Precipitation of Driest Quarter [mm/quarter]", 
"Precipitation of Warmest Quarter [mm/quarter]", 
"Precipitation of Coldest Quarter [mm/quarter]", 
) 

# Crop rasters and rename 
xmin=-120; xmax=-35; ymin=-60; ymax=35 
limits <- c(xmin, xmax, ymin, ymax) 
predictors <- crop(predictors_full,limits) 

predictor_names<-c("mT_annual","mT_dayn_rg","Isotherm","T_season", 
"maxT_warm_M","minT_cold_M","rT_annual","mT_wet_Q","mT_dry_Q", 
"mT_warm_Q","mT_cold_Q","P_annual","P_wet_M","P_dry_M","P_season", 
"P_wet_Q","P_dry_Q","P_warm_Q","P_cold_Q") 

names(predictors)<-predictor_names 

#Set a palette 
Blues_up<-c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858','#233159') 
colfunc_blues<-colorRampPalette(Blues_up) 

#Create a loop to plot all my Predictor rasters 
for (i in 1:19) { 
#save a figure 
png(file=paste0(predictor_names[[i]],".png"),units="in", width=12, height=8.5, res=300) 

#Define a plot area 
par(mar = c(2,2, 3, 3), mfrow = c(1,1)) 

#extract values from rasters 
vmax<- maxValue(predictors[[i]]) 
vmin<-minValue(predictors[[i]]) 
vmedn=(maxValue(predictors[[i]])-minValue(predictors[[i]]))/2 

#breaks 
break1<-c((seq(from=vmin,to= vmedn, length.out = 40)),(seq(from=(vmedn+(vmedn/5)),to=vmax,length.out = 5))) 

#plot without the legend because the legend would come out with really messy, with too many marks and uneven spaces 
plot(predictors[[i]], col =colfunc_blues(45) , breaks=break1, margin=FALSE, 
      main =predic_legends[i],legend.shrink=1) 
dev.off() 
} 

Precipitation raster according to the above code は、この図は、私はその後、ループ

内のすべてのラスタからのI = 12書いていますカラーバーに

#Plot the raster with no color scale bar  
plot(predictors[[i]], col =colfunc_blues(45) , breaks=break1, margin=FALSE, 
     main =predic_legends[i],legend=FALSE) 

#breaks for the color scale 
def_breaks = seq(vmax,vmin,length.out=(10)) 

#plot only the legend 
image.plot(predictors_full[[i]], zlim = c(vmin,vmax), 
      legend.only = TRUE, col = colfunc_greys(30), 
      axis.args = list(at = def_breaks, labels =def_breaks,cex.axis=0.5)) 

を別のブレークを設定するしかし、それは動作しません、色は本当にマップ内の番号と一致しないので...各マップに6.000の色を見て、異なるコード...それはdですifferent。

Different breaks for plot and scale

その上で続行する方法上の任意のヒント? 私はRに新しいので、私の目標に到達するために多くの努力をしています... また、私は数字に小数点以下の桁をたくさん入れています...小数点以下2桁をどのように変更するのですか?

EDIT:@jbaumsは...ログインを使用するように私に教えて、私は言っていますが、それは私が

levelplot(predictors[[12]]+1, col.regions=colorRampPalette(brewer.pal(9, 'Blues')), zscaleLog=TRUE, at=seq(1, 4, len=100), margin=FALSE) 

Using log

+0

実際にはどのように見えますか? – jbaums

+0

私は色に均等に配分された色を必要としません。なぜなら、高い数値と低い数値のデータが非常に少なく、途中であまりにも多くのデータがあるからです。それらが均等に分散されていれば、青変化の少ない「中間青」となる。ある極度の場合は薄い青色、もう一方の極限の場合は濃い青色、そして実際にデータがある場合は青色の残りの部分がデータに沿って必要です。私が投稿したコードはそれをしようとしたものでした...しかし、私はプログラミングとRに先月紹介されました...私は多くを読んでいましたが、基盤がありません – Thai

+0

@jbaums ...この説明を理解しましたか?私が十分にはっきりしていないかどうか教えてください。私はもっとうまくやろうとします!あなたのattentioのために事前にありがとう! – Thai

答えて

1

は、あなたが使用して(一部のユーザーがあなたに言ったように)対数スケールを避けることができる求めるものをまだありませんclassIntervals()の機能はclassIntパッケージです。 levelplot()(私の意見では、結果がraster::plot()機能よりも優れている)を使用して

:また

# Normal breaks 
break1 <- classIntervals(predictors[[12]][!is.na(predictors[[12]])], n = 50, style = "equal") 

levelplot(predictors[[12]], col.regions=colorRampPalette(brewer.pal(9, 'Blues')), at=break1$brks, margin=FALSE,main =predic_legends[12]) 

enter image description here

# Using quantiles 
break1 <- classIntervals(predictors[[12]][!is.na(predictors[[12]])], n = 50, style = "quantile") 

levelplot(predictors[[12]], col.regions=colorRampPalette(brewer.pal(9, 'Blues')), at=break1$brks, margin=FALSE,main =predic_legends[12]) 

enter image description here

は、あなたが選択するより多くのオプションを持っています、例えばsd,pretty,kmeans,hclustなどが挙げられる。プロット

まずにポリゴンとポイントを追加する


、私はpに上記のプロットを救う、ラインは、この例のために長すぎる:私が使用します

p <- levelplot(predictors[[12]], col.regions=colorRampPalette(brewer.pal(9, 'Blues')), at=break1$brks, margin=FALSE,main =predic_legends[12]) 

あなたのデータと同じデータ、wrld_simplのデータをプロットに追加するポリゴンとして追加し、プロットに追加するポイントも作成します。

library(maptools) 
library(rgeos) 

data(wrld_simpl) 
pts <- gCentroid(wrld_simpl, byid = T) 

行を追加するには、ポリゴン、ポイント、あるいはテキスト、あなたはlayer()機能とpanel.spplotオブジェクトを使用することができます

p + layer(sp.polygons(wrld_simpl)) + layer(sp.points(pts)) 

enter image description here

最後に、あなたはまた、塗りつぶし、色を変更することができますが、記号、記号など:

p + layer(sp.polygons(wrld_simpl,col='firebrick')) + layer(sp.points(pts,pch = 12,col='red')) 

enter image description here

詳細については、?panel.spplotを確認してください。

+0

oh my!良いダーウィン!正確に私が必要なもの! @aldo_tapia、muito obrigada !!! – Thai

+0

levelplotは実際にプロットした方が良い結果を出すのですが、私は先月Rに紹介されましたので、まだ学習しています。レベルマップを使って1つのプロットを追加しています。 wrld_simpl、spatialPoints、ポリゴンからの国の行... reguarプロットでは簡単です。プロットしたいものすべてに対して 'plot(x、add = TRUE)'を実行します...レベルプロットとは異なります。しかし、できるだけ早くレベルプロットとggplotを学ぶために時間を割いています!ありがとう、@アルドタイピア – Thai

+0

@タイあなたを助けてあげましょう。私は今は私のマシンよりは遠いですが、私はすぐにあなたの素敵な –

関連する問題