how to insert x and y label and tick marks in autoKrige?

ε祈祈猫儿з 提交于 2019-12-24 17:14:14

问题


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


回答1:


I used the dataset you provided, and, I post the result like an image:

I just run your code until ggplot_data is produced casting from an spPointsDataFrame (gridded), to a data.frame (with reshape2). Then I built the ggplot object step by step but I found no errors:

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)

Is this what you expected?




回答2:


The automapPlot function is a wrapper around spplot, so any fix that works for spplot would also be applicable to automapPlot. You can start with the spplot documentation for a place to start.

However, I normally use the ggplot2 package for any plotting work, including spatial data. The following is an example which reproduces the result of 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'))



来源:https://stackoverflow.com/questions/25324648/how-to-insert-x-and-y-label-and-tick-marks-in-autokrige

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!