I have been trying to change the code from the 'labeling outliers in a boxplot' to get it to work in a loop (of columns in a df).
ens_id=names(mtcars)
for(i in 1:length(ens_id)){
dat <- test %>% tibble::rownames_to_column(var="outlier") %>% group_by(cond) %>% mutate(is_outlier=ifelse(is_outlier(as.numeric(ens_id[i])),as.numeric(ens_id[i]), as.numeric(NA)))
dat$outlier[which(is.na(dat$is_outlier))] <- as.numeric(NA)
p=ggplot(dat, aes_string(y=ens_id[i], x="cond",fill="cond")) + geom_boxplot() + ylab(gene_id[i])+ geom_text(aes(label=outlier),na.rm=TRUE,nudge_x=0.15)
ggsave(p, file=paste0("/media/chi/Figures/HVOLvsCDCS/",ens_id[i],".png"), width = 14, height = 10, units = "cm")
}
Error in quantile.default(x, 0.25) : missing values and NaN's not allowed if 'na.rm' is FALSE In addition: Warning message: In is_outlier(as.numeric(ens_id[i])) : NAs introduced by coercion
I used as.numeric(ens_id[i]) to get around the error:
Error in (1 - h) * qs[i] : non-numeric argument to binary operator