1

2 つのデータセットが使用されます。

  1. 3 列の空間データ (x、y、データ)

  2. 2 列 (x, y) のグリッド データ

automap パッケージの autoKrige はクリギングの計算を行い、x と y の目盛りとラベルなしでプロットできます。

plot(kriging_result)
automapPlot(kriging_result$krige_output, "var1.pred", sp.layout = list("sp.points", shimadata), main="OK without grids", xlab="x", ylab="y")

ggplot2 パッケージを使用すると、エラーが表示されますが、クリギングが計算されます。

mydata<-read.table("D:/.../mydata.txt",header=T,sep=",")
#Renaming desired columns:
x<-mydata[,1]
y<-mydata[,2]
waterelev<-mydata[,3]
library(gstat)
coordinates(mydata)=~x+y
library(ggplot2)
theme_set(theme_bw())
library(scales)
library(automap)
grids<-read.table("D:/.../grids.txt",header=T,sep=",")
gridded(grids)=~x+y
kriging_result = autoKrige(log(waterelev)~1, mydata)
#This line turns the log(data) back to the original data:
kriging_result$krige_output$var1.pred<-exp(kriging_result$krige_output$var1.pred)
library(reshape2)
ggplot_data = as.data.frame(kriging_result$krige_output)
ggplot(ggplot_data, aes(x = x, y = y, fill = var1.pred)) + 
   geom_raster() + coord_fixed() + 
   scale_fill_gradient(low = 'white', high = muted('blue'))

エラー:

エラー: 美学は長さ 1 であるか、dataProblems:x, y と同じ長さでなければなりません

4

2 に答える 2

3

このautomapPlot関数は のラッパーspplotであるため、 で機能する修正spplotは にも適用できautomapPlotます。開始spplotする場所のドキュメントから開始できます。

ただし、通常、ggplot2空間データを含むプロット作業にはパッケージを使用します。以下は、automapPlot の結果を再現する例です。

library(ggplot2)
theme_set(theme_bw())
library(scales)
library(automap)
data(meuse)
coordinates(meuse) =~ x+y
data(meuse.grid)
gridded(meuse.grid) =~ x+y
kriging_result = autoKrige(zinc~1, meuse, meuse.grid)

# Cast the Spatial object to a data.frame
library(reshape2)
ggplot_data = as.data.frame(kriging_result$krige_output)

ggplot(ggplot_data, aes(x = x, y = y, fill = var1.pred)) + 
    geom_raster() + coord_fixed() + 
    scale_fill_gradient(low = 'white', high = muted('blue'))

ここに画像の説明を入力

于 2014-08-15T14:33:56.333 に答える
1

あなたが提供したデータセットを使用し、結果を画像のように投稿します。

ggplot結果

spPointsDataFrame (グリッド) から data.frame (reshape2 を使用) にキャストして ggplot_data が生成されるまで、コードを実行するだけです。次に、ggplot オブジェクトを段階的に作成しましたが、エラーは見つかりませんでした。

g=ggplot(data=ggplot_data,aes(x=x1,y=x2,fill=var1.pred))
g=g+geom_raster()
g=g+coord_fixed()
g=g+scale_fill_gradient(low = 'white', high = muted('blue'))
print(g)

これはあなたが期待したものですか?

于 2014-08-17T09:33:25.610 に答える