R 用多个彩色部分绘制不同的彩色线

R 用多个彩色部分绘制不同的彩色线,r,ggplot2,R,Ggplot2,我想有一个线条图,其中我有两条线,两种不同的颜色,每一条线都有不同颜色的部分。就像每条线的渐变阴影。e、 g.沿x轴不同区域的蓝色阴影线 library(ggplot2) df <- data.frame(a = c(rep(0,10), rep(1,10)), b = c(rep(c(rep(0,3),rep(1,4),rep(2,3)), 2)), c = sample(1:20),

我想有一个线条图,其中我有两条线,两种不同的颜色,每一条线都有不同颜色的部分。就像每条线的渐变阴影。e、 g.沿x轴不同区域的蓝色阴影线

library(ggplot2)
df <- data.frame(a = c(rep(0,10), rep(1,10)),
                 b = c(rep(c(rep(0,3),rep(1,4),rep(2,3)), 2)),
                 c = sample(1:20),
                 d = c(1:20))
df

ggplot(data = df) + geom_line(aes(x = d, y = c, color = factor(a),
linetype = factor(b)))
库(ggplot2)

df您可以尝试使用alpha和线条大小而不是线型

代码:

结果:


数据(
df
):


这种方法怎么样:

创建一个从蓝色到红色的渐变,共8种颜色,前3种颜色用于映射蓝色,后3种颜色用于映射红色。基于a和b相互作用的颜色:

colfunc <- colorRampPalette(c("blue", "red"))

ggplot(data = df)+
  geom_line(aes(x = d, y = c, color = interaction(b, a), group = a)) +
  scale_color_manual(values = colfunc(8)[c(1:3,6:8)])

colfunc很好的解决方案,伙计!我仍然会优先选择不同的色调,因为这里的层数是5。因此,较粗的线条也会使情节变得复杂dense@joel.wilson阿尔法没有给你不同的色调吗?是的。但是非常非常轻。我们能把它调整得更暗一些吗?把颜色调得更暗,然后给它更暗的阴影汉克斯兄弟!我玩了这个,它成功了!!
structure(list(a = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1), b = c(0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 0, 0, 
0, 1, 1, 1, 1, 2, 2, 2), c = c(6L, 13L, 10L, 20L, 2L, 11L, 15L, 
5L, 3L, 7L, 17L, 4L, 12L, 14L, 1L, 19L, 9L, 8L, 18L, 16L), d = 1:20), .Names = c("a", 
"b", "c", "d"), row.names = c(NA, -20L), class = "data.frame")
colfunc <- colorRampPalette(c("blue", "red"))

ggplot(data = df)+
  geom_line(aes(x = d, y = c, color = interaction(b, a), group = a)) +
  scale_color_manual(values = colfunc(8)[c(1:3,6:8)])
bfunc <- colorRampPalette(c("blue3", "dodgerblue", "firebrick1", "darkred"))

ggplot(data = df)+
  geom_line(aes(x = d, y = c, color = interaction(b, a), group = a)) +
  scale_color_manual(values = bfunc(6))