8

spplot()凡例と比較した場合、ggplotを使用して空間ラスターマッププロットの凡例を改善するにはどうすればよいですか?

ssplot()の代わりにggplot()を使用して空間マップをプロットしたいのですが、spplotと比較して改善したいことがいくつかあります。

  1. 小さい値(下)から大きい値(上)に変化するggplot凡例を作成します
  2. ssplot()の凡例と同様に、ggplotの凡例に区切りを入れて、各色の境界が何であるかを確認します。

## load packages
require(raster)
require(ggplot2)
require(rgdal)
require(RColorBrewer)
set.seed(1)

r <- raster(xmn=-110, xmx=-90, ymn=40, ymx=60, ncols=40, nrows=40,
          crs="+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100
+ellps=WGS84")
r <- setValues(r,matrix(rnorm(1600, mean=0.4,sd=0.2))) 

## 1. spatial map with spplot
cuts <-seq(minValue(r),maxValue(r),length.out=8)
cuts = round(cuts,digits=2)
col.regions = brewer.pal(length(cuts)+3-1, "RdYlGn")
print( 
spplot(as(r, 'SpatialGridDataFrame'),at=cuts,
col.regions=col.regions,
colorkey=list(labels=list(at=cuts),at=cuts), pretty=TRUE,
scales=list(draw=T)
) 
)

## 2. spatial map with ggplot
p = rasterToPoints(r); df = data.frame(p)
colnames(df) = c("x", "y", "NDVI")

p  <- ggplot(data=df) + geom_tile(aes(x, y, fill=NDVI)) +
coord_equal() + labs(x=NULL, y=NULL) + 
scale_fill_gradient2(low="red", mid="yellow",high="green",
limits=c(minValue(r),maxValue(r)), midpoint = 0.4) + theme_bw() +
scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))
print(p)

ssplot()の結果 ssplot

ggplot()の結果 ggplot

4

3 に答える 3

5

それへのポインタをありがとう@joran。

dev バージョンを使用したコードと出力の例を次に示します。

br <- seq(min(df$NDVI), max(df$NDVI), len=8)

ggplot(data=df) + 
  geom_tile(aes(x, y, fill=NDVI)) + 
  scale_fill_gradient(low="red", high="green", 
    breaks=br, labels=sprintf("%.02f", br), 
    guide=guide_colorbar(title=NULL, nbin=100, barheight=unit(0.75, "npc"), label.hjust=1)) + 
  scale_x_continuous(expand=c(0,0)) + 
  scale_y_continuous(expand=c(0,0))

ここに画像の説明を入力

おそらく次の方法でこれを試すことができます:

# from Hadley's instruction
install.packages("devtools")
library(devtools)
dev_mode() # to avoid interfering with your existing install
install_github("ggplot2", username="kohske", branch = "feature/new-guides-with-gtable")
library(ggplot2)

更新しました:

最初からインストールする手順は次のとおりです。

install.packages(
  c('devtools', 'digest', 'memoise', 'plyr', 'reshape2', 'RColorBrewer', 'stringr', 'dichromat', 'munsell', 'plyr', 'colorspace'), 
  dep=TRUE)

library(devtools)
dev_mode()

install_github("scales")
install_github("ggplot2", username="kohske", branch = "feature/new-guides-with-gtable")
于 2011-07-29T16:56:08.600 に答える
4

頭のてっぺんから(1)に対処する方法がわかりません。ただし、(2) と (3) に対するいくつかの可能な解決策を次に示します。

ggplot2現在、そのような方法で伝説にラベルを付けることができるとは思いません。ただし、Koske はggplot2、この方法でスタイル設定された凡例を作成する、将来的に組み込まれる可能性があるいくつかのコードに取り組んでいます。ここにリンクがありますが、いくつかの追加パッケージをインストールする必要があり、アルファ版にすぎません.

探している特定の休憩を取得するには、次のことを試してください。

br <- c(-0.25,-0.05,0.15,0.35,0.56,0.76,0.96,1.16)
p  <- ggplot(data=df) + geom_tile(aes(x, y, fill=NDVI)) +
coord_equal() + labs(x=NULL, y=NULL) + 
scale_fill_gradient(low="red", mid="yellow",high="green",
breaks = br) + theme_bw() +
scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))
print(p)

私のマシンでこのグラフを作成しました:

ここに画像の説明を入力

上記の実験的なコードを使用して、これがどのように見えるかをテストしていません。

于 2011-07-28T20:06:08.613 に答える
0

Re: (2) まずcut()データをビニングされたデータセットにします。breaksで とlabelsオプションを使用cut()して、正しいラベルを取得します。次に例を示します。

dat$col <- cut(
  df$NDVI, 
  breaks=c(-Inf, -0.25, 0.05, ...whatever..., Inf), 
  labels=c(-0.25, 0.05, ...whatever..., "")
)

次に、ggplot を使用してプロットし、ラベルをシフトして、次を使用して色の境界に正しく配置できます。

 scale_fill_manual (your options...) + guides(fill=guide_legend(label.vjust = 1.2)) #1.2= what fits your legend best. Use label.hjust if using an horizontal color bar

参照: ggplot2 を使用してラベルをシフトした色の凡例を生成する

別の方法として、外部スクリプトを使用して色の凡例を作成し (たとえば、GrADS は適切なスクリプトを使用して適切な色の凡例を作成します)、同じ色を scale_fill_manual で手動で指定することもできます。

于 2014-11-20T11:32:54.420 に答える