从R中使用ggplot2制作的多个箱图中完全删除异常值,并以展开格式显示箱图

时间:2014-02-03 17:01:32

标签: r ggplot2 boxplot outliers

我有一些数据here [在.txt文件中]我读入数据框df,

df <- read.table("data.txt", header=T,sep="\t")

我使用以下代码删除xdf中的负值(因为我只需要正值),

yp <- subset(df, x>0)

现在我想在同一层中绘制多个箱形图。我首先融合数据框df,结果图包含几个异常值,如下所示。

# Melting data frame df    
df_mlt <-melt(df, id=names(df)[1])
    # plotting the boxplots
    plt_wool <- ggplot(subset(df_mlt, value > 0), aes(x=ID1,y=value)) + 
      geom_boxplot(aes(color=factor(ID1))) +
      scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +    
      theme_bw() +
      theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+
      theme(axis.text=element_text(size=20)) +
      theme(axis.title=element_text(size=20,face="bold")) +
      labs(x = "x", y = "y",colour="legend" ) +
      annotation_logticks(sides = "rl") +
      theme(panel.grid.minor = element_blank()) +
      guides(title.hjust=0.5) +
      theme(plot.margin=unit(c(0,1,0,0),"mm")) 
    plt_wool

Boxplot with outliers

现在我需要一个没有任何异常值的情节,所以为了做到这一点,我首先计算下限和上限胡须我使用下面的代码建议here

sts <- boxplot.stats(yp$x)$stats

要删除异常值,我会添加上面和下面的晶须限制,如下所示

p1 = plt_wool + coord_cartesian(ylim = c(sts*1.05,sts/1.05))

结果图如下所示,而上面的代码行正确地删除了大多数顶部异常值,所有底部异常值仍然存在。有人可以建议如何完全删除所有异常值,谢谢。

enter image description here

5 个答案:

答案 0 :(得分:19)

最小可重复的例子:

library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot()

不绘制异常值:

p + geom_boxplot(outlier.shape=NA)
#Warning message:
#Removed 3 rows containing missing values (geom_point).

(我更喜欢得到这个警告,因为一年后我会用长脚本提醒我,我在那里做了一些特别的事情。如果你想避免使用Sven的解决方案。)

答案 1 :(得分:14)

基于@Sven Hohenstein,@ Roland和@lukeA的建议,我解决了以扩展形式显示多个箱图而没有异常值的问题。

首先使用outlier.colour=NA

中的geom_boxplot()绘制没有异常值的箱形图
plt_wool <- ggplot(subset(df_mlt, value > 0), aes(x=ID1,y=value)) + 
  geom_boxplot(aes(color=factor(ID1)),outlier.colour = NA) +
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +
  theme_bw() +
  theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+
  theme(axis.text=element_text(size=20)) +
  theme(axis.title=element_text(size=20,face="bold")) +
  labs(x = "x", y = "y",colour="legend" ) +
  annotation_logticks(sides = "rl") +
  theme(panel.grid.minor = element_blank()) +
  guides(title.hjust=0.5) +
  theme(plot.margin=unit(c(0,1,0,0),"mm"))

然后使用boxplot.stats()作为下面的代码计算较低的上胡须。由于我只考虑正值,因此我使用subset()中的条件选择它们。

yp <- subset(df, x>0)             # Choosing only +ve values in col x
sts <- boxplot.stats(yp$x)$stats  # Compute lower and upper whisker limits

现在要实现多个箱图的完全展开视图,修改coord_cartesian()函数内的图的y轴限制非常有用,如下所示,

p1 = plt_wool + coord_cartesian(ylim = c(sts[2]/2,max(sts)*1.05))

注意:应根据具体情况调整y的限制。在这种情况下,我选择ymin的一半较低的晶须限制。

结果图如下:

答案 2 :(得分:4)

您可以使用参数outlier.colour = NA隐藏异常值:

geom_boxplot(aes(color = factor(ID1)), outlier.colour = NA)

答案 3 :(得分:3)

ggplot(df_mlt, aes(x = ID1, y = value)) + 
  geom_boxplot(outlier.size = NA) + 
  coord_cartesian(ylim = range(boxplot(df_mlt$value, plot=FALSE)$stats)*c(.9, 1.1))

答案 4 :(得分:3)

排除异常值的另一种方法是计算它们,然后根据你认为的异常值设置y限制。

例如,如果您的上限和下限是Q3 + 1.5 IQRQ1 - 1.5 IQR,那么您可以使用:

upper.limit <- quantile(x)[4] + 1.5*IQR(x)
lower.limit <- quantile(x)[2] - 1.5*IQR(x)

然后对y轴范围设置限制:

ggplot + coord_cartesian(ylim=c(lower.limit, upper.limit))