首页 > 解决方案 > r ggplot 非线性渐变阿尔法

问题描述

取以下数据

df <- data.frame(ID = c(1, 1, 2, 2, 3, 3),
                 cond = c(2, 2, 2, 2, 2, 2),
                 Time = c(1,2,1,2,1,2),
                 State  = c("Empty", "Empty", "Empty", "Full", "Full", "Empty"),
                 eye = c(2, -3, -2, 1, 0, -2),
                 combination = c(1, 1,2, 2, 3, 3))

和情节

ggplot(df, aes(x = Time, y = eye)) +
  geom_point(aes(shape=State), size = 3.5) +
  scale_shape_manual(values=c(13,15)) +  
  geom_line(aes(group=ID, color = Time, alpha = State), size = 5) +
    
  theme(legend.title=element_blank(),
        axis.title.x=element_text(size = 12),
        axis.title.y=element_text(size = 12),
        plot.title = element_text(hjust = 0.5, size = 15),
        axis.text.x= element_text(color = "black"),
        axis.text.y= element_text(color = "black"),
        axis.line = element_line(colour = "black"),
        plot.background = element_rect(fill = "white"),
        panel.background = element_rect(fill = "white"))

产生

在此处输入图像描述

我的问题是

  1. 这为线条提供了不同的 alpha,但我希望线条具有渐变 alpha。具体来说,我需要在“空”和“满”状态之间的线以低 alpha 开始并以高 alpha 结束,从“满”到“空”的线以高 alpha 开始并以低结束α。(从“低”到“低”的线是可以的,就像在示例中一样)。

  2. 在 x 轴上,我希望“空”到“满”线从 1.25 开始,然后“淡入”到 2 结束,而不是从 x = 1 开始并在 x = 2 结束,我想要'full' 到 'empty' 行从 1.00 开始,'fade out' 到 1.75 结束。

我一直在为这个问题烦恼一段时间,非常感谢任何帮助。

标签: rggplot2

解决方案


推荐阅读