首页 > 解决方案 > 用 ggplot 模拟 fill.contour

问题描述

我一直在尝试模仿filled.contourggplotgeom_tilegeom_raster. 似乎有一些插值或幕后的东西filled.contour来填补空白,尤其是在 y 轴上。知道如何用 ggplot 制作类似的、漂亮的情节吗?

例如,可以在此处下载数据(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")

结果:

在此处输入图像描述

标签: rggplot2contour

解决方案


推荐阅读