你如何使geom_points透明化

时间:2018-02-28 21:28:33

标签: r ggplot2

我有这个情节,当group2 = 0时,我将点的大小设置为0,将alpha设置为.000000000001,因为我想使这些点不可见。但你仍然可以看到图表上的圆圈。知道如何让那个圆圈完全不可见吗?

enter image description here

library(ggplot2)
dat = data.frame(label = c("A","B","C","D"), group1 = c(1,2,3,.02), group2 = c(3,4,5,0), color1 = c("lightblue","lightblue","lightblue","lightblue") ,
                 color12 = c("blue","blue","blue","blue"))

dat$sizze = ifelse(dat$group2 ==0 ,0, 2 )
dat$alpha = ifelse(dat$group2 ==0 ,0.0000000000001, 1 )


p1 <- ggplot(dat, aes(x = label, y = group1, fill = color1))+
  geom_bar(stat="identity")  +
  geom_point(data = dat, aes(x = label, y = group2, color = color12, size = sizze, alpha = alpha))

p1 + 
  scale_size(guide='none', range=c(2) ) +
  scale_fill_manual(values = c('lightblue' = 'lightblue'),
                    name = 'bar legend')+
  scale_colour_manual(values = c('blue' = 'blue'),
                      name = 'point legend')

1 个答案:

答案 0 :(得分:4)

如果您试图不绘制您的观点,最好只将geom_point中的数据进行子集化。您可以尝试将geom_point更改为:

geom_point(data = dat[!(dat$group2==0),], aes(x = label, y = group2, 
           color = color12, size = sizze, alpha = alpha))

这给出了这个情节:

enter image description here