geom_segment 中的 alpha 不起作用

The*_*aya 4 plot r opacity ggplot2

我想在我的情节和遇到的那几个改进alphageom_segment工作不正常。对于最小工作示例,请检查:

ggplot(mtcars, aes(hp, mpg)) + 
  geom_point() + 
  geom_segment(aes(x = 100, xend = 200, y = 20, yend = 20), 
  inherit.aes = FALSE, 
  size = 10, 
  alpha = 0.5, 
  color = "blue")
Run Code Online (Sandbox Code Playgroud)

但是,如果您将 alpha 更改为非常低的值,例如 0.005,则 0.001 似乎有效。您只能看到 0.05 到 0.001 之间的一些效果。

alpha 值不是应该在 0 和 1 之间以线性方式变化还是我理解不正确?

Eri*_*ail 6

像这样的事情,

# install.packages(c("tidyverse"), dependencies = TRUE)
library(tidyverse)
    ggplot(mtcars, aes(hp, mpg)) + 
      geom_point() + 
      annotate('segment', x = 100, xend = 200, y = 20, yend = 20,
    size = 10,
    alpha = 0.5,
    color = "blue")
Run Code Online (Sandbox Code Playgroud)

ggplt2 中的段


mpa*_*nco 5

ggplot2 正在绘制许多段,一个在彼此之上使段不透明。您可以通过data从 ggplot 函数中删除并将其添加到所需图层来解决它。此处此处与其他 geoms 的类似问题。

ggplot() + 
    geom_point(data=mtcars, aes(hp, mpg)) + 
    geom_segment(aes(x = 100, xend = 200, y = 20, yend = 20), 
                 inherit.aes = FALSE, 
                 size = 10, 
                 alpha = 0.5, 
                 color = "blue")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

另一种选择是像 Eric 一样使用 annotate:

ggplot(mtcars) +
    geom_point(aes(hp, mpg)) +
    annotate(
      'segment',
      x = 100,
      xend = 200,
      y = 20,
      yend = 20,
      size = 10,
      colour = "blue",
      alpha = 0.5
    ) 
Run Code Online (Sandbox Code Playgroud)