代码之家  ›  专栏  ›  技术社区  ›  Doug Fir

使用alpha或scale_alpha_manual向图表添加不同的覆盖名称

  •  0
  • Doug Fir  · 技术社区  · 6 年前

    我有一个情节,并添加了两个几何覆盖。这个情节是一个时间轴,两个覆盖物应该表示发生的事件,以提供时间轴的上下文。

    我的数据如下:

    > glimpse(pdata_world)
    Observations: 108
    Variables: 2
    $ date             <date> 2019-04-01, 2019-04-02, 2019-04-03, 2019-04-04, 2019-04-05, 2019-04-06, 2019-04-07, 2019-04-08, 2019-04-09…
    $ organic_installs <dbl> 1572, 1593, 2391, 2857, 2015, 2677, 3800, 3308, 855, 2122, 4420, 2979, 4324, 4532, 3775, 3490, 3487, 3605, …
    > glimpse(usa_launch_overlay)
    Observations: 1
    Variables: 2
    $ start <date> 2019-05-28
    $ end   <date> 2019-05-30
    > glimpse(marketing_campaign_overlay)
    Observations: 1
    Variables: 2
    $ start <date> 2019-06-24
    $ end   <date> 2019-07-17
    

    pdata_world %>%
      ggplot(aes(x = date, y = organic_installs)) +
      geom_line() +
      geom_rect(data = marketing_campaign_overlay, inherit.aes = F,
                aes(xmin = start, xmax = end,
                    ymin = -Inf, ymax = Inf,
                    alpha = "Marketing Campaign"),
                fill = "black") +
      geom_rect(data = usa_launch_overlay, inherit.aes = F,
                aes(xmin = start, xmax = end,
                    ymin = -Inf, ymax = Inf,
                    alpha = "USA Launch"),
                fill = "blue") +
        scale_alpha_manual(name = '', values = c("USA Launch" = 0.1, "Marketing Campaign" = 0.1))
    

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  0
  •   the-mad-statter    6 年前

    我不会那样用α标度的。事实上,你应该得到一个警告信息。

    取而代之,我会用一个合适的比例尺来填充外部的比例和贴图。更像这样:

    pdata_world %>%
      ggplot(aes(x = date, y = organic_installs)) +
      geom_line() +
      geom_rect(data = marketing_campaign_overlay, inherit.aes = F,
                aes(xmin = start, xmax = end,
                    ymin = -Inf, ymax = Inf,
                    fill = "Marketing Campaign"),
                alpha = 0.1) +
      geom_rect(data = usa_launch_overlay, inherit.aes = F,
                aes(xmin = start, xmax = end,
                    ymin = -Inf, ymax = Inf,
                    fill = "USA Launch"),
                alpha = 0.1) +
        scale_fill_manual(name = element_blank(), values = c("USA Launch" = "yellow", "Marketing Campaign" = "purple"))