从data.frame绘制等高线图

从data.frame绘制等高线图,r,contour,R,Contour,考虑以下几点: temp <- array(sample(1:100,12), dim=c(365,12)) Depth <- as.vector(seq(1,12,by=1)) time <- seq(from=as.Date("2011-01-01"), to=as.Date("2011-12-31"), by=1) Data <- data.frame(Time = time, Temp = as.matrix(temp)) colnames(Data) <

考虑以下几点:

temp <- array(sample(1:100,12), dim=c(365,12))
Depth <- as.vector(seq(1,12,by=1))
time <- seq(from=as.Date("2011-01-01"), to=as.Date("2011-12-31"), by=1)

Data <- data.frame(Time = time, Temp = as.matrix(temp))
colnames(Data) <- c("Datetime", paste(Depth,"m",sep = "")) 

tempLattice使这变得非常简单,因为您拥有正确结构的数据(长而薄,而不是宽而短-您目前拥有的是后者)

首先,使用一些简单的数据操作将数据转换成所需的格式

## make the colnames numeric-ish
names(Data)[-1] <- sub("m", "", names(Data)[-1])
## stack the data
Data2 <- data.frame(Time = rep(Data$Datetime, times = ncol(Data)-1),
                    stack(Data[, -1]))
names(Data2)[-1] <- c("Temperature", "Depth")
## make Depth numeric
Data2 <- transform(Data2, Depth = as.numeric(as.character(Depth)))
加载
lattice
并使用
contourplot()
函数绘制数据:

require(lattice)
contourplot(Temperature ~ Time * Depth, data = Data2)
对于这个示例数据集,使用

contourplot(Temperature ~ Time * Depth, data = Data2, labels = FALSE,
            region = TRUE)
因为轮廓基本上是围绕着小块数据形成的


有关各种选项的更多信息,请参见
?轮廓图
页面。

Lattice使这变得非常简单,因为您拥有正确结构的数据(长而薄,而不是宽而短-您目前拥有后者)

首先,使用一些简单的数据操作将数据转换成所需的格式

## make the colnames numeric-ish
names(Data)[-1] <- sub("m", "", names(Data)[-1])
## stack the data
Data2 <- data.frame(Time = rep(Data$Datetime, times = ncol(Data)-1),
                    stack(Data[, -1]))
names(Data2)[-1] <- c("Temperature", "Depth")
## make Depth numeric
Data2 <- transform(Data2, Depth = as.numeric(as.character(Depth)))
加载
lattice
并使用
contourplot()
函数绘制数据:

require(lattice)
contourplot(Temperature ~ Time * Depth, data = Data2)
对于这个示例数据集,使用

contourplot(Temperature ~ Time * Depth, data = Data2, labels = FALSE,
            region = TRUE)
因为轮廓基本上是围绕着小块数据形成的

有关各种选项的更多信息,请参见
?轮廓图
页面