我有一个大的data.frame
有3个变量Longitude
,Latitude
and Temp
。
数据的排列使其在1/4度的“网格”上有规律地间隔 - 以便dput(head(dat))
给出:
structure(list(Longitude = c(0.125, 0.375, 0.625, 0.875, 1.125,
1.375), Latitude = c(0.125, 0.125, 0.125, 0.125, 0.125, 0.125
), Temp = c(25.2163, 25.1917, 25.1593, 25.125, 25.0908, 25.0612
)), .Names = c("Longitude", "Latitude", "Temp"), row.names = c(NA,
6L), class = "data.frame").
我在重新安排所需格式时遇到问题。
我想创建一个常规曲面对象(通常是一个列表),其中x和y是网格值,z是曲面的对应矩阵。这是persp
,contour
,image
etc使用的通常格式。
使用这个表面对象,我将能够使用interp.surf
from fields
package轻松插入到位置矩阵。
任何建议都会很棒。
假设你的数据是这样的
set.seed(123)
d <- data.frame(lon=rep(seq(0,1,0.25), times=5),
lat=rep(seq(0,1,0.25), each=5),
temp=sample(1:25, 25, replace=TRUE))
head(d, 8)
# lon lat temp
# 1 0.00 0.00 8
# 2 0.25 0.00 20
# 3 0.50 0.00 11
# 4 0.75 0.00 23
# 5 1.00 0.00 24
# 6 0.00 0.25 2
# 7 0.25 0.25 14
# 8 0.50 0.25 23
我们创建一个z
矩阵,表示网格中每个点的值。然后我们将网格线(x
和y
)的位置与z
一起放入列表中。
library(reshape2)
z <- acast(d, lat~lon, value.var="temp")
X <- list(x=sort(unique(d$lon)),
y=sort(unique(d$lat)),
z=z)
image(X, col=gray.colors(25))
with(d, text(lon, lat, labels=temp))