Amm*_*Amm 12 r outliers ggplot2 boxplot
我在这里 [在.txt文件中] 有一些数据,我读入数据帧df,
df <- read.table("data.txt", header=T,sep="\t")
Run Code Online (Sandbox Code Playgroud)
我使用以下代码删除列中的负值x
(因为我只需要正值)df
,
yp <- subset(df, x>0)
Run Code Online (Sandbox Code Playgroud)
现在我想在同一层中绘制多个箱形图.我首先融化数据框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
Run Code Online (Sandbox Code Playgroud)
现在,我需要有一个情节,没有任何异常,所以这样做首先我计算上限和下限胡须我使用下面的代码的建议在这里,
sts <- boxplot.stats(yp$x)$stats
Run Code Online (Sandbox Code Playgroud)
为了消除异常值,我添加了上下晶须限制,如下所示,
p1 = plt_wool + coord_cartesian(ylim = c(sts*1.05,sts/1.05))
Run Code Online (Sandbox Code Playgroud)
结果图如下所示,而上面的代码行正确地删除了大多数顶部异常值,所有底部异常值仍然存在.有人可以建议如何从这个情节中完全删除所有异常值,谢谢.
Rol*_*and 19
一个可重复性最小的例子:
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot()
Run Code Online (Sandbox Code Playgroud)
没有绘制异常值:
p + geom_boxplot(outlier.shape=NA)
#Warning message:
#Removed 3 rows containing missing values (geom_point).
Run Code Online (Sandbox Code Playgroud)
(我更喜欢得到这个警告,因为一年后我会用长脚本提醒我,我在那里做了一些特别的事情.如果你想避免使用Sven的解决方案.)
Amm*_*Amm 14
根据@Sven Hohenstein,@ Roland和@lukeA的建议,我解决了以扩展形式显示多个箱图而没有异常值的问题.
首先使用outlier.colour=NA
in 绘制没有异常值的箱形图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"))
Run Code Online (Sandbox Code Playgroud)
然后使用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
Run Code Online (Sandbox Code Playgroud)
现在要实现多个箱图的完全展开视图,修改coord_cartesian()
函数内部的y轴极限是很有用的,如下所示,
p1 = plt_wool + coord_cartesian(ylim = c(sts[2]/2,max(sts)*1.05))
Run Code Online (Sandbox Code Playgroud)
注意: y的限制应根据具体情况进行调整.在这种情况下,我选择ymin的一半较低的晶须限制.
结果图如下,
归档时间: |
|
查看次数: |
37822 次 |
最近记录: |