简体   繁体   English

使用R和ggplot2的箱线图例图例键的透明度

[英]Transparency in boxplot legend keys using R and ggplot2

I am trying to make boxplots with a transparent fill and a corresponding legend. 我正在尝试使用透明填充和相应的图例制作箱形图。 The plotting with alpha values works fine except that the legend keys do not have transparent fills. 使用图例值的绘图工作正常,但图例键没有透明填充。 The following example illustrates the difficulty: 以下示例说明了这一困难:

dat <- data.frame(x=c('a', 'b', 'c'), y = runif(300), z = c('d', 'e', 'e'))
ggplot(dat) + geom_boxplot(aes(x, y, fill = z, colour = z), alpha = 0.2)

填充了透明胶片但未插入图例的箱线图

Is there a way to make the fill values in the legend keys transparent too? 有没有办法使图例键中的填充值也透明?

SessionInfo()
R version 2.14.0 (2011-10-31)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
ggplot2_0.8.9

Thanks in advance for any tips on this issue. 在此先感谢您提供有关此问题的任何提示。

An example where the alpha mapping works is the following 以下是Alpha映射起作用的示例

ggplot(dat) + geom_point(aes(x, y, fill = z, colour = z, shape = z), alpha = 0.2)

绘制Alpha映射效果良好的图

The solution I've used in the past is this one... its a huge hack but works. 我过去使用的解决方案是此解决方案。

dat <- data.frame(x=c('a', 'b', 'c'), y = runif(300), z = c('d', 'e', 'e'))
dummy <- dat
dummy$z <- factor(dummy$z)
dummy <- dummy[1,]
dummy[,2]<-NaN
ggplot() + 
geom_boxplot(data=dat,aes(x, y, fill = z, colour = z), alpha = 0.2,legend=FALSE) +
geom_density(data=dummy,aes(x, fill = z, colour = z), alpha = 0.2)

I usually use it when I'm making a geom_text plot, but want the legend to show as points or blocks. 我通常在制作geom_text图时使用它,但希望图例显示为点或块。 But it works for this instance too. 但它也适用于此实例。

In case Hadley is paying attention to this thread, I've found myself wanting a plot=T/F option that works like the legend option for this situation. 万一哈德利(Hadley)注意这个线程,我发现自己想要一个plot = T / F选项,该选项类似于这种情况下的图例选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM