创建geom_line或类似内容,并在其下方使用“淡入” alpha

时间:2019-04-19 15:44:03

标签: r ggplot2 visualization

我想创建一个geom_line,或者可能创建geom_ribbongeom_area,并在类似于汤森路透Eikon终端所使用的线下方创建“淡出”效果

我尝试了一种基于geom_ribbon的非常简单的方法,即根据y值设置alpha,以为可以基于基础值对其进行归一化,但收到了以下错误:{{1} }

(非工作)代码示例:

Error in f(...) : Aesthetics can not vary with a ribbon

我希望输出类似于此博客文章中的第一幅图(讽刺的是ggplot2的library( tidyverse ) library( ggplot2 ) x <- seq( 0, 99 ) y <- rnorm( 100 ) + 4 tib <- tibble( x, y ) gp <- ggplot( tib ) + geom_ribbon( aes( x=x, ymax=y, ymin=y-1, alpha=y) ) + geom_line( aes( x=x, y=y ) ) ):A Detailed Guide to Plotting Line Graphs

enter image description here

或者如上所述,汤姆森·路透社Eikon终端的输出显示在此页第二张图片的两张图的顶部:Thomson Reuters Eikon Review

enter image description here

1 个答案:

答案 0 :(得分:0)

这里有个骗子。如果您需要多条重叠的线,看起来就不太好了,但是无论如何我都不认为这是用例。在绘图中添加不必要的褪色Alpha非常接近chartjunk,并且如果需要在同一张图表中比较多条线,我建议避免使用这样的修饰,以使外观更清晰,分散注意力。

ggplot(tib) +

  # rectangular gradient layer as background
  annotation_raster(raster = c("blue",      # top colour
                               "white") %>% # bottom colour
                      alpha(0.5),           # alpha
                    xmin = min(tib$x), xmax = max(tib$x),
                    ymin = 0, ymax = max(tib$y),
                    interpolate = TRUE) +

  # whiteout layer (replace white with relevant panel.background's
  # fill value, if a non-white background theme is used)
  geom_ribbon(aes(x = x, ymin = y, ymax = Inf), 
              fill = "white") +

  # actual line
  geom_line(aes(x = x, y = y), 
            color = "blue", size = 1) +
  theme_classic()

plot

数据:

set.seed(4321)
x <- seq(0, 99)
y <- rnorm(100) + 4

tib <- tibble(x, y)