使用数据中提供的 alpha 值 [英] Use alpha values provided in data

查看:24
本文介绍了使用数据中提供的 alpha 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 alpha 级别的显式值.

I would like to use the explicit values for the alpha level.

head(D)

    x  y group  alpha
  1 1 18     A   0.40   <~~~~
  2 2 18     A   0.44
  3 3 18     A   0.47
  4 1 18     A   0.51
  5 2 21     B   0.55
  6 3 21     B   0.58
  ...

然而,ggplot 正在缩放 alpha 级别.我可以使用 scale_alpha_continuous(range = range(D$alpha)) 覆盖它,但这在以编程方式创建图形时会变得很麻烦.

However, ggplot is scaling the alpha levels. I can override this using scale_alpha_continuous(range = range(D$alpha)), but this becomes a nuisance when creating the graph programmatically.

有没有直接的方法告诉 ggplot 不要缩放 alpha?(而不是告诉它缩放到什么范围)

Is there a direct way to tell ggplot NOT to scale alpha? (instead of telling it what range to scale to)

library(ggplot)
library(gridExtra)
(D <- data.frame(x=rep(1:3, 4), y=rep((6:8)*3, each=4), group=rep(c("A","B", "C"), each=4),  alpha=round(seq(.4, .8, length.out=12), 2)))

P <- ggplot(data=D, aes(x=x, y=y, alpha=alpha)) + geom_bar(stat="identity", fill="blue") + theme(legend.position="bottom") + facet_grid(group ~. )

### Adding  scale_alpha_continuous
P.manually_scaled <- P + scale_alpha_continuous(range=range(D$alpha))

grid.arrange( P + ggtitle("INCORRECT")
             , P.manually_scaled + ggtitle("CORRECT")
             , ncol=2)

推荐答案

如果您有实际的 alpha、颜色、...、值,那么您应该使用 ..identity() 标度.这将告诉 ggplot() 分配 alpha 值,因为它们在您的数据框中,而不是缩放它们.

If you have actual alpha, color, ..., values then you should use ..identity() scales. This will tell ggplot() to assign alpha values as they are in your data frame and not to scale them.

ggplot(data=D, aes(x=x, y=y, alpha=alpha)) + 
         geom_bar(stat="identity", fill="blue") + 
         facet_grid(group ~. ) +
         scale_alpha_identity()

这篇关于使用数据中提供的 alpha 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆