用ggplot模拟filled.contour

时间:2018-11-27 09:14:19

标签: r ggplot2 contour

我一直在尝试用ggplot的filled.contourgeom_tile来模仿geom_raster的输出。 filled.contour的幕后似乎有一些插值或某些东西可以填补空白,尤其是在y轴上。知道如何使用ggplot制作相似,美观的图吗?

例如,可以here(ShareCSV链接)下载数据。

例如代码:

library("ggplot2")
library("reshape2")


file <- file.choose()
data <- read.csv(file, stringsAsFactors=FALSE, header=TRUE,
                 colClasses=c("POSIXct", rep("numeric", 12)),
                 check.names=FALSE)

palette <- colorRampPalette(c("darkblue", "blue", "lightblue1",
                              "green", "yellow", "red", "darkred"))

# GGplot version
df <- melt(data, id.vars="Time", measure.vars=names(data)[-1])
names(df)[2:3] <- c("Channel", "Value")
df$Channel <- log10(as.numeric(as.character(df$Channel)))
df$Value <- log10(df$Value)


p <- ggplot(data=df, aes(x=Time, y=Channel, fill=Value)) +
  # geom_tile() +
  geom_raster() +
  scale_y_continuous(expand=c(0, 0)) +
  scale_x_datetime(expand=c(0, 0)) +
  scale_fill_gradientn(colours=palette(100)) +
  theme_bw()

p

# Base version
Channel <- log10(as.numeric(names(data[-1])))
Value <- log10(data.matrix(data[, -1]))

filled.contour(x=data$Time, y=Channel, z=Value,
               color.palette=palette, nlevels=100)

我在df上尝试了以下方法,该方法已经很接近了,但是它的计算量很大。

library("akima")

interpolation <- with(df, interp(x=Time, y=Channel, z=Value,
                      xo=seq(min(Time), max(Time), length.out=nrow(data)),
                      duplicate="mean"))

df<- interp2xyz(interpolation, data.frame=TRUE)
names(df) <- c("Time", "Channel", "Value")
df$Time <- as.POSIXct(df$Time, origin="1970-01-01")

结果:

enter image description here

0 个答案:

没有答案