我想在一张图表上绘制两条线,一条带有颜色渐变,另一条带有纯色。我可以添加单独的线条和渐变:
x <- seq(1, 100, 1)
y <- rnorm(100, 50, 15)
z <- rnorm(100, 30, 5)
df <- data.frame(x,y,z)
library(ggplot2)
ggplot(df, aes(x = x, y = y, color = x)) + geom_line() +
scale_color_gradient(low = "blue", high = "red") +
geom_line(data = df, aes(x = x, y = z, color = x))
但是更改颜色会引发错误:
> ggplot(df, aes(x = x, y = y, color = ..y..)) + geom_line() +
+ scale_color_gradient(low = "blue", high = "red") +
+ geom_line(aes(x = x, y = z, color = "yellow"))
Error: Discrete value supplied to continuous scale
我希望添加标签,但这可能会很复杂...