【问题标题】:Ggplot2 alpha not working as expectedGgplot2 alpha 未按预期工作
【发布时间】:2017-05-26 16:23:14
【问题描述】:

根据 ggplot2,尝试使用 ggplot2 绘制时间序列图并使用 alpha 值使线条更暗/更亮。让它在 1 个函数中工作,但是当我尝试使用另一个数据集时,alpha 不起作用。猜猜我叫错了,因为我将 alpha 变量设置为 0.2,但这条线仍然变暗

这是代码和一些示例数据

tsplot <- ggplot(xall, aes(x=Var1, y=value)) + 
  geom_line(size=.01) + guides(colour=FALSE) + xlab(x.lab) +ylab("Time Series")
tsplot <- tsplot + aes(alpha=alpha, group= factor(Var2)) +guides(alpha=F)

xall 的样本数据

    Var1    Var2    value   alpha   row
1   1   657 0   0.2 Other Rows
2   2   657 -0.006748957    0.2 Other Rows
3   3   657 -0.00088561 0.2 Other Rows
4   4   657 0.009399679 0.2 Other Rows
5   5   657 0.020216333 0.2 Other Rows
6   6   657 0.035222838 0.2 Other Rows
7   7   657 0.038869107 0.2 Other Rows
8   8   657 0.034068491 0.2 Other Rows
9   9   657 0.044237734 0.2 Other Rows
81  1   553 0   0.2 Other Rows
82  2   553 -0.006172511    0.2 Other Rows
83  3   553 -0.004779576    0.2 Other Rows
84  4   553 0.000116964 0.2 Other Rows
85  5   553 -0.013408332    0.2 Other Rows
86  6   553 -0.003200561    0.2 Other Rows
87  7   553 0.000574187 0.2 Other Rows
88  8   553 0.025227017 0.2 Other Rows
89  9   553 0.019984901 0.2 Other Rows
241 1   876 0   0.2 Other Rows
242 2   876 0.006348487 0.2 Other Rows
243 3   876 0.020292484 0.2 Other Rows
244 4   876 0.030155311 0.2 Other Rows
245 5   876 0.02664097  0.2 Other Rows
246 6   876 0.021992971 0.2 Other Rows
247 7   876 0.015871216 0.2 Other Rows
248 8   876 0.020519216 0.2 Other Rows
249 9   876 0.017004875 0.2 Other Rows
250 10  876 0.029588482 0.2 Other Rows

任何帮助将不胜感激。

【问题讨论】:

  • aes 通常不会直接添加到情节中(我认为可以,但无法预测其行为);它通常被传递给ggplotmapping 参数或geom。
  • 并且您应该将 alpha 作为参数传递,除非您将 alpha 映射到变量,所以类似于 ggplot(xall, aes(x = Var1, y = value, group = Var2)) + geom_path(alpha = 0.2)
  • 感谢阿利斯泰尔。奇迹般有效!唯一的问题是,如果我添加 + geom_line(size=.01) 它不会再次工作。嗯
  • 一次性添加所有内容:ggplot(xall, aes(x = Var1, y = value, group = Var2)) + geom_path(alpha = 0.2, size = 0.01),或者您要绘制两次线条。 0.01 太小以至于在大多数情况下是不可见的,但我会假设它对你来说是合适的值。
  • 优秀。非常感谢!

标签: r ggplot2


【解决方案1】:

您需要将alpha 添加到全球审美。您还应该添加group 映射:

ggplot(xall, aes(x=Var1, y=value, alpha=alpha, group= factor(Var2))) + 
  geom_line(size=.01) + guides(colour=FALSE) + xlab(x.lab) +ylab("Time Series")

【讨论】:

  • 谢谢,但这仍然对我不起作用。我想知道它是否在某些数据中有 NA,因为我收到一条消息保存删除了一些行,如果这会弄乱 ggplot 指令?情节虽然有效
  • 你想让组的每一行的 alpha 不同吗(Var2)?
猜你喜欢
  • 2022-01-12
  • 2012-06-26
  • 2021-06-04
  • 2022-01-24
  • 2015-05-11
  • 2020-05-15
  • 2014-10-31
  • 2018-02-12
  • 2014-01-20
相关资源
最近更新 更多