[英]Remove outliers fully from multiple boxplots made with ggplot2 in R and display the boxplots in expanded format
我在這里 [在.txt文件中]有一些數據,我讀入數據幀df,
df <- read.table("data.txt", header=T,sep="\t")
我除去負值在列x
(因為我只需要正的值)的的df
使用以下代碼,
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
現在,我需要有一個情節,沒有任何異常,所以這樣做首先我計算上限和下限胡須我使用下面的代碼的建議在這里 ,
sts <- boxplot.stats(yp$x)$stats
為了消除異常值,我添加了上下晶須限制,如下所示,
p1 = plt_wool + coord_cartesian(ylim = c(sts*1.05,sts/1.05))
結果圖如下所示,而上面的代碼行正確地刪除了大部分頂部異常值,所有底部異常值仍然存在。 有人可以建議如何從這個情節中完全刪除所有異常值,謝謝。
一個可重復性最小的例子:
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的解決方案。)
根據@Sven Hohenstein,@ Roland和@lukeA的建議,我解決了以擴展形式顯示多個箱圖而沒有異常值的問題。
首先在geom_boxplot()
使用outlier.colour=NA
繪制沒有異常值的箱形圖
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的一半較低的晶須限制。
結果圖如下,
您可以使用參數outlier.colour = NA
使異常值不可見:
geom_boxplot(aes(color = factor(ID1)), outlier.colour = NA)
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))
排除異常值的另一種方法是計算它們,然后根據您認為的異常值設置y限制。
例如,如果您的上限和下限是Q3 + 1.5 IQR
和Q1 - 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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.