从ggplot2中的stat_summary中删除异常值

发布于 2025-01-29 14:14:57 字数 740 浏览 3 评论 0原文

我有这个代码的一部分来生产我的数据:

p <- ggplot(meltData, aes(x=variable, y=value)) + 
  geom_boxplot()+  geom_boxplot(outlier.colour="red", outlier.shape=1,outlier.size=2)+
stat_summary(geom="text", fun=quantile,
             aes(label=sprintf("%1.1f", ..y..), color=factor(variable)),
             position=position_nudge(x=0.0), size=3.5,show_guide = FALSE)+
  ggtitle("Species measurements")+
  ggeasy::easy_center_title()
p

而且我有此输出:

我希望能够在我的框图上看到 uppper和lower Whisker编号作为最大值和最小值(而不是异常值)。例如,在第五个框图上,我们可以看到最大数字为72,但这是一个离群值,最大值应大约为56。

I have this part of code to produce boxplot with my data:

p <- ggplot(meltData, aes(x=variable, y=value)) + 
  geom_boxplot()+  geom_boxplot(outlier.colour="red", outlier.shape=1,outlier.size=2)+
stat_summary(geom="text", fun=quantile,
             aes(label=sprintf("%1.1f", ..y..), color=factor(variable)),
             position=position_nudge(x=0.0), size=3.5,show_guide = FALSE)+
  ggtitle("Species measurements")+
  ggeasy::easy_center_title()
p

and I have this output:
enter image description here

I want to be able to see uppper and lower whisker numbers on my boxplot as maximum and minimum values (and not the outliers numbers). For example, on the 5th boxplot we can see that the max number is 72, but this is an outlier and the max should be at 56 approximately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2025-02-05 14:14:57

如果我正确理解您的目的,则需要创建盒子图以及显示上下晶须数量的文本,并且图中不应显示异常值。如果这是真的,那么我同意您可能要过滤每个类别的异常值的@Death Metal。

但是,由于您没有提供可再现的数据,因此以下是类似于数据的虚拟数据。

dat <- data.frame(var.A = c(iris$Sepal.Length, c(20,21,22)), 
                  var.B = c(iris$Petal.Length, c(20,21,22)))
meltData <- dat %>% pivot_longer(cols = c(var.A, var.B), 
                                 values_to = "value", 
                                 names_to = "variable")

ggplot(meltData, aes(x=variable, y=value)) + geom_boxplot()

清楚地显示异常值

这是在应用框图之前过滤异常值的方法:

meltData %>% group_by(variable) %>%
     filter(value != (boxplot(value))$out) %>% 
     ggplot(aes(x = variable, y = value)) + 
     geom_boxplot() + stat_summary(geom="text", 
                                   fun=quantile,aes(label=sprintf("%1.1f", ..y..), 
                                                    color=factor(variable)),
                                   position=position_nudge(x=0.0), 
                                   size=3.5,show_guide = FALSE)+
     ggtitle("Species measurements")+
     ggeasy::easy_center_title()
#Warning message:
#`show_guide` has been deprecated. Please use `show.legend` instead. 

结果:

“在此处输入图像描述”

If I understand your purpose correctly, you want to create boxplots along with texts that show the upper and lower whisker numbers and no outliers should be shown in the plots. If that's true, then I agree with @Death Metal that you might want to filter the outliers per category.

However, because you don't provide a reproducible data, here is a dummy data similar to your data.

dat <- data.frame(var.A = c(iris$Sepal.Length, c(20,21,22)), 
                  var.B = c(iris$Petal.Length, c(20,21,22)))
meltData <- dat %>% pivot_longer(cols = c(var.A, var.B), 
                                 values_to = "value", 
                                 names_to = "variable")

ggplot(meltData, aes(x=variable, y=value)) + geom_boxplot()

which clearly shows outliers

enter image description here

Here is on of the ways to filter the outliers before applying boxplots:

meltData %>% group_by(variable) %>%
     filter(value != (boxplot(value))$out) %>% 
     ggplot(aes(x = variable, y = value)) + 
     geom_boxplot() + stat_summary(geom="text", 
                                   fun=quantile,aes(label=sprintf("%1.1f", ..y..), 
                                                    color=factor(variable)),
                                   position=position_nudge(x=0.0), 
                                   size=3.5,show_guide = FALSE)+
     ggtitle("Species measurements")+
     ggeasy::easy_center_title()
#Warning message:
#`show_guide` has been deprecated. Please use `show.legend` instead. 

The result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文