[英]geom_boxplot: map alpha levels to whiskers & outliers
Is there a way to make geom_boxplot
lines, whiskers, and outlier points inherit the same alpha
that is assigned to the boxplot fill
in the following plot? 有没有办法让geom_boxplot
线条,胡须和异常点继承相同的alpha
分配给下图中的boxplot fill
?
library(ggplot2)
ggplot(iris, aes(x = Species, y = Sepal.Length, alpha = Species == "setosa")) +
geom_boxplot() +
scale_alpha_discrete(range = c(0.5, 1))
I'm not sure if there's a natural way to do it within ggplot2
, but here's a hacky way: The colour aesthetic determines the colour of the lines, whiskers, and outlier points. 我不确定在ggplot2
是否有一种自然的方法可以做到这ggplot2
,但这里有一种黑客的方式:颜色审美决定了线条,胡须和异常点的颜色。 So we'll add a colour aesthetic to the plot. 因此,我们将为情节增添色彩美感。 However, we'll set the colors manually so that they include the sequence of alpha values in the color definitions. 但是,我们将手动设置颜色,以便它们在颜色定义中包含alpha值序列。 As a result, we effectively add alpha values to the color aesthetic. 因此,我们有效地将alpha值添加到颜色美学中。 I've mapped the aesthetics to Species
so that I can have three different alpha levels for illustration and added theme_bw()
for better contrast of the alpha values. 我已将美学映射到Species
以便我可以有三个不同的alpha级别用于说明,并添加了theme_bw()
以更好地对比alpha值。
First, set the alpha values we want in the final graph: 首先,在最终图中设置我们想要的alpha值:
numVals = length(unique(iris$Species))
avals = seq(0.9, 0.3, length.out=numVals)
Here's the hack: Set the all the colors to black, but add the alpha-level sequence to the color definition. 这是hack:将所有颜色设置为黑色,但将alpha级序列添加到颜色定义中。 For this, we need to convert the the alpha levels to hexadecimal on a 0 - 255 scale. 为此,我们需要将alpha级别转换为0到255的十六进制。
avalsHex = paste0("#000000", toupper(as.hexmode(round(avals*255))))
In the code above, avals*255
are the alpha levels on a 0 - 255 scale. 在上面的代码中, avals*255
是0到255比例的alpha级别。 as.hexmode
converts those values to hexadecimal. as.hexmode
将这些值转换为十六进制。 toupper
isn't strictly necessary, but I'm used to seeing hexadecimal color definitions in upper case. toupper
并不是绝对必要的,但我习惯于以大写形式看到十六进制颜色定义。 Then we paste the two-digit hexadecimal alpha value onto the end of the color, in this case black ( #000000
), which gives a vector of three colors that include alpha values. 然后我们将两位十六进制alpha值粘贴到颜色的末尾,在本例中为黑色( #000000
),它给出了包含alpha值的三种颜色的矢量。
No we plot with both the color and alpha aesthetics and the manual values for each aesthetic: 不,我们用每种美学的颜色和阿尔法美学以及手工值来绘制:
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot(aes(color=Species, alpha = Species)) +
scale_alpha_manual(values = avals) +
scale_colour_manual(values = avalsHex)
UPDATE: To address your comment, here's an example with different colors in addition to alpha values: 更新:为了解决您的评论,这里是一个除alpha值以外的不同颜色的示例:
avalsHex = paste0(c("#FF0000","#00FF00","#0000FF"), toupper(as.hexmode(round(avals*255))))
Then just run the same plotting code to get: 然后只需运行相同的绘图代码即可:
UPDATE 2: Thinking about colors made me realize that you don't even need the conversion to hexadecimal to add on the alpha values. 更新2:考虑颜色让我意识到你甚至不需要转换为十六进制来添加alpha值。 For example, with hcl
colors you can do this: 例如,使用hcl
颜色,您可以这样做:
# Black boxplots with 3 different alpha levels
scale_colour_manual(values = hcl(0,0,0, alpha=avals))
# Colored boxplots with 3 different alpha levels
scale_colour_manual(values=hcl(seq(15,375,length.out=4)[1:3], 100, 65, alpha=avals))
The hcl
function returns the hexdecimal code for each color, in effect taking care of the conversion for you. hcl
函数返回每种颜色的十六进制代码,实际上是为您处理转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.