r - 用 ggplot 模拟 fill.contour
问题描述
我一直在尝试模仿filled.contour
ggplotgeom_tile
和geom_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")
结果:
解决方案
推荐阅读
- c++ - 构造函数初始化列表中的分段错误
- python - Python list.insert() 多索引/列表列表
- php - 如何在 Cake PHP Form->select 中将第一个选择器值设置为 1
- python - 检查来自不同列表的值是否小于 x
- java - 使用spring webClient完成所有http请求后如何等待?
- c++ - 使用 cudaMemcpy3D 进行高效的零填充
- python - 使用 Python 从加载的 XML 文件中获取 XML 文件名
- java - 杰克逊反序列化数组内的数组
- google-colaboratory - Google Colab 用户活动
- javascript - 如何仅在 textarea 完全加载后运行控制器功能?