运行此代码时,红线应与方框图中的中线匹配。它只对一种树胶起作用,而对另一种则不起作用。知道为什么吗?
还发出警告消息
"Warning message:
Removed 7 rows containing non-finite values (stat_boxplot). "
那是什么?
set.seed(123)
d = data.frame(group=c(rep("A",10),rep("B",10)),v = rnorm(20))
summary_stats = d %>% dplyr::filter( is.na(v) ==FALSE) %>% dplyr::group_by(group) %>%
dplyr::summarise(
Q1 = quantile(v,.25,na.rm =TRUE),
MEDIAN = quantile(v,.5,na.rm =TRUE),
Q3 = quantile(v,.75,na.rm =TRUE)
) %>% dplyr::mutate(IQR = Q3-Q1) %>% dplyr::arrange(MEDIAN)
boxplot.stats(d[d$group=="A",]$v )
boxplot.stats(d[d$group=="B",]$v )
d$group = factor( d$group ,levels=summary_stats$group, ordered = TRUE)
ggplot(d, aes(x=group, y=v)) +
geom_boxplot(outlier.shape = NA,outlier.size =0,coef = 0)+
theme(axis.text.x=element_text(angle=90))+
geom_hline(yintercept = -0.07983455,color= "red") +
geom_hline(yintercept = 0.3802926 ,color= "red") +
scale_y_continuous(limits = c( min(summary_stats$Q1)-.1, max(summary_stats$Q3)+.1 ))
答案 0 :(得分:1)
如果您忽略了scale_y_continuous
部分,那么一切似乎都很好。似乎该线以某种方式与晶须的计算混乱。一种更安全的方法是使用coord_cartesian
。例如
ggplot(d, aes(x=group, y=v)) +
geom_boxplot(outlier.shape = NA,outlier.size =0,coef = 0)+
theme(axis.text.x=element_text(angle=90))+
geom_hline(yintercept = -0.07983455,color= "red") +
geom_hline(yintercept = 0.3802926 ,color= "red") +
coord_cartesian(ylim = c( min(summary_stats$Q1)-.1, max(summary_stats$Q3)+.1 ))