简体   繁体   English

geom_boxplot:将alpha级别映射到胡须和异常值

[英]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.

相关问题 geom_boxplot 给出了错误的胡须 - geom_boxplot gave wrong whiskers geom_boxplot:用第二个矩形替换 IQR 和胡须之间的线 - geom_boxplot: replace the lines between the IQR and the whiskers by a second rectangle 为什么geom_boxplot比基本boxplot可以识别更多离群值? - Why geom_boxplot identify more outliers than base boxplot? 如何使用 geom_boxplot(stat = “identity”) 模拟带有异常值的 geom_boxplot() - How to mimic geom_boxplot() with outliers using geom_boxplot(stat = “identity”) 使用geom_boxplot时如何更改异常值的颜色 - How to change color of outliers when using geom_boxplot ggplot2,带有自定义分位数和异常值的geom_boxplot - ggplot2, geom_boxplot with custom quantiles and outliers ggplot2 - 使用 geom_boxplot 生成带有自定义晶须的水平箱线图时出错 - ggplot2 - Error while generating a horizontal boxplot with custom whiskers using geom_boxplot 对于ggplot2 geom_boxplot,只有两个样本时,是否可以删除盒子和晶须? - For ggplot2 geom_boxplot, is there a way to remove the box and whiskers when only 2 samples? 从 ggplotly() function 调用时如何从 geom_boxplot() 中删除胡须 - How to remove whiskers from geom_boxplot() when called from a ggplotly() function 在 R 中使用 geom_boxplot() + geom_jitter() 时如何排除异常值 - How to exclude outliers when using geom_boxplot() + geom_jitter() in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM