1

Two datasets are used:

  1. spatial data in 3 columns (x, y, data)

  2. grids data in 2 columns (x, y)

automap package autoKrige does the calculation of kriging and can be plotted with no x and y tick marks and labels:

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

And when I use ggplot2 package, it shows error, however it calculates the kriging:

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'))

The error:

Error: Aesthetics must either be length one, or the same length as the dataProblems:x, y

4

2 回答 2

3

automapPlot函数是. spplot_ 您可以从文档开始。spplotautomapPlotspplot

但是,我通常将该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 结果

我只是运行您的代码,直到 ggplot_data 从 spPointsDataFrame(网格化)转换为 data.frame(使用 reshape2)。然后我一步一步构建了 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 回答