在“ geom_boxplot”中仅针对一个因子水平更改晶须定义 [英] Change whisker definition for only one level of a factor in `geom_boxplot`

查看:92
本文介绍了在“ geom_boxplot”中仅针对一个因子水平更改晶须定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将晶须定义更改为最小和最大(,而不是任何异常值),如这个问题,但仅针对映射到x轴的因子的单个级别。答案中的代码将更改整个图的晶须定义。

I'm trying to change the whisker definition to extend to the minimum and maximum (i.e., not to consider anything as an outlier), as in this question, but only for a single level of the factor that is mapped to the x-axis. The code in that answer will change the whisker definition for the entire plot.

解决这个问题的正确方法是什么?

What's the proper way, if any, to go about this?

推荐答案

扩展问题中链接的示例,您可以执行以下操作:

Extending the example linked in the question, you could do something like:

f <- function(x) {
  r <- quantile(x, probs = c(0.05, 0.25, 0.5, 0.75, 0.95))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}

# sample data
d <- data.frame(x = gl(2,50), y = rnorm(100))

# do it
ggplot(d, aes(x, y)) + 
  stat_summary(data = subset(d, x == 1), fun.data = f, geom = "boxplot") +
  geom_boxplot(data = subset(d, x == 2))

在这种情况下,因子 x == 2 得到常规 geom_boxplot ,但因数 x == 1 是扩展。

In this case, factor x == 2 gets the "regular" geom_boxplot, but factor x == 1 is the "extended".

以您的情况为例,您可能想做一些抽象的工作像这样的事情:

In your case, and being a little more abstract, you probably want to do something like this:

ggplot(d, aes(x, y)) + 
  stat_summary(data = subset(d, x == "special_factor"), fun.data = f, geom = "boxplot") +
  geom_boxplot(data = subset(d, x != "special_factor"))

这篇关于在“ geom_boxplot”中仅针对一个因子水平更改晶须定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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