告诉ggplot不要缩放alpha [英] Telling ggplot NOT to scale alpha

查看:160
本文介绍了告诉ggplot不要缩放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 neusance when creating the graph programatically.

是否有一种直接告诉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)


推荐答案

如果你有实体al alpha,color,...值,那么你应该使用 .. 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()

这篇关于告诉ggplot不要缩放alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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