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

查看:113
本文介绍了使用数据中提供的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天全站免登陆