首页 > 解决方案 > 忽略 ggplot 中 geom_bar 和 geom_histogram 中的异常值

问题描述

我有数据框

DF <- data.frame(respondent = factor(c(1, 2, 3, 4, 5, 6)), choice = as.numeric(c(1, 3, 3, 5, 10, 1000)))

当我绘制它时,“1000”值使情节难以解释:

library(ggplot2)
ggplot(DF, aes(choice, 1,  ymax=10))+
  geom_bar(stat = "identity")+
  scale_y_continuous (limits = quantile(DF$choice, c(0, 10)
  theme(axis.text.x = element_text(angle = 0, hjust = 1),
        text = element_text(size=20)) 

如何从图中删除异常值?是否有一个相当于ylim我可以简单地设置为“10”的ggplot?有一篇关于处理异常值的帖子geom_boxplot,但我无法获得在geom_bar或中工作的解决方案geom_histogram

标签: rggplot2

解决方案


我想xlim这就是你要找的吗?

ggplot(DF, aes(choice, 1)) +
  geom_bar(stat = "identity") + 
  xlim(c(0, 11))

您将收到警告Warning message: Removed 1 rows containing missing values (position_stack).,指出由于施加的限制,数据点已被删除。

我删除了其他代码以使其成为“最小示例”-如果这对您的问题很重要,请随时跟进。


推荐阅读