alpha in geom_segment not working [duplicate]

戏子无情 提交于 2020-08-07 07:15:30

问题


I was trying few refinements in my plot and encounter that alpha in geom_segment is not working properly. For minimum working example check this:

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")

However, if you change the alpha to really low value such as 0.005, 0.001 appears working. You can only see some effect from 0.05 to 0.001.

Aren't the alpha values supposed to change in a linear manner between 0 and 1 or have I understood incorrectly?


回答1:


Something like this,

# 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")




回答2:


ggplot2 is drawing many segments, one on top of each other making the segment opaque. You can solve it by removing the data from the ggplot function and add it to the required layers. Similar problem with other geoms here and here.

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")

Another option is to use annotate as Eric did:

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


来源:https://stackoverflow.com/questions/48397603/alpha-in-geom-segment-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!