I have some image data plotted using the color bar. I want to select a line from the image and plot the graph in ggplot2 using the same color scale on the curve as on the image. Is it possible?
Suppose I draw my image as follows
require(ggplot2) n <- 100

I want to color the line as in the following image

Created using the following code
colscale <- function(y, cols, ylim) { k <- length(cols) steps <- seq(ylim[1], ylim[2], length = k) result <- sapply(y, function(x) {cols[which.min(abs(x - steps))]}) return(result) } plot(x, y, ylim = lim, col = colscale(y, cols, lim))
source share