ggplot2: Thresholds for scale_alpha()

白昼怎懂夜的黑 提交于 2019-12-04 08:10:59

The threshold is working and the values outside that threshold are set to NA; the problem is that an alpha of NA is getting rendered as full opacity. Setting the na.value on the scale to 0 gets the results you want.

ggplot(xyd, aes(x=x, y=y, fill=z, alpha=a)) + 
       geom_tile() + 
       scale_alpha(range=c(0,1), limits=c(0.5,1), na.value = 0)

None of my attempts to use the scales to control alpha were completely successful. My best attempt was to use ifelse to control the value of a:

ggplot(xyd, aes(x=x, y=y, fill=z)) + 
  geom_tile(aes(alpha=ifelse(a<=0.5, 0, a))) +
  scale_alpha(range=c(0,1))


So, a different approach is required: remove the values that you don't want to plot from the data:

xyd <- with(xyd, xyd[a>0.5, ])

ggplot(xyd, aes(x=x, y=y, fill=z)) + 
  geom_tile(aes(alpha=a))

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