gpt4 book ai didi

r - 用纯色绘制一条线,用渐变绘制第二条线

转载 作者:行者123 更新时间:2023-12-02 08:03:57 24 4
gpt4 key购买 nike

我想在一张图表上画两条线,一条是渐变色的,另一条是纯色的。我可以添加单独的线条和渐变:

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

我希望添加标签,但这可能太复杂了......

最佳答案

最简单的方法是在aes 之外为实线指定颜色:

library(ggplot2)
ggplot(df, aes(x, y, color = x)) +
geom_line() +
# No need to respecify data or x at it's defined in main ggplot call
geom_line(aes(y = z), color = "yellow") +
scale_color_gradient(low = "blue", high = "red")

enter image description here

关于r - 用纯色绘制一条线,用渐变绘制第二条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53787576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com