我有一些使用色标绘制的图像数据.我想从图像中选出一条线并在ggplot2中绘制曲线,在曲线上使用相同的色标,就像在图像中一样.这可能吗?
假设我按如下方式绘制图像
require(ggplot2)
n <- 100 # number of observations
cols <- topo.colors(256) # color scheme
lim <- c(-10, 10) # limits corresponding to color scheme
x <- seq(0, 1, length = n) # x-axis
y <- cumsum(rnorm(n)) # Brownian motion
dat <- data.frame(x, y) # data
# Plot
ggplot(dat, aes(x, y)) + geom_line() + scale_y_continuous(limits = lim)
Run Code Online (Sandbox Code Playgroud)

我想要与下面的图类似地着色线

使用以下代码创建
colscale <- function(y, cols, ylim) {
k <- length(cols)
steps <- seq(ylim[1], ylim[2], length = k)
result <- sapply(y, function(x) …Run Code Online (Sandbox Code Playgroud)