geom_ribbon颜色错误?

时间:2018-08-09 12:57:12

标签: r ggplot2 dplyr

考虑这个简单的例子

library(dplyr)
library(ggplot2)
library(lubridate)

testdf <- data_frame(time = c(ymd('2015-01-01'), ymd('2015-02-01'), ymd('2015-03-01')),
                     coef = c(1, 0, -1),
                     low_ci = c(-0.5, -0.25, -2),
                     high_ci = c(1.5, 0.5, -.5))

> testdf
# A tibble: 3 x 4
  time        coef low_ci high_ci
  <date>     <dbl>  <dbl>   <dbl>
1 2015-01-01     1  -0.5      1.5
2 2015-02-01     0  -0.25     0.5
3 2015-03-01    -1  -2       -0.5

在这里,我想使用coeflow_ci作为置信区间带来绘制high_ci的时间序列。

但是,使用以下代码会产生令人惊讶的结果

testdf %>% 
  ggplot(., aes(x = time)) + 
  geom_line(aes(y = coef)) +
  geom_ribbon(aes(ymin = low_ci, ymax = high_ci , alpha = 0.3, fill = 'blue'))

enter image description here

因为蓝色是红色?这是什么问题?

谢谢!

1 个答案:

答案 0 :(得分:2)

正如AntoniosK所说,您可以使用函数scale_fill_manual(),但是第二种选择是将参数fill = 'blue'放在函数aes()之外(与参数alpha相同)。 像这样:

testdf %>% 
  ggplot(., aes(x = time)) + 
  geom_line(aes(y = coef)) +
  geom_ribbon(aes(ymin = low_ci, ymax = high_ci), alpha = 0.3, fill = 'blue')

enter image description here

相关问题