【ggplot2】箱线图geom

    科技2024-07-09  68

    1. 保留离群值

    将coef设置为5,stats <- quantile(x, probs = c(0, 0.25, 0.5, 0.75, 1)),如果没有fact,直接在 geom_boxplot(coef = 5)。

    calc_stat <- function(x) { coef <- 5 n <- sum(!is.na(x)) # calculate quantiles stats <- quantile(x, probs = c(0, 0.25, 0.5, 0.75, 1)) names(stats) <- c("ymin", "lower", "middle", "upper", "ymax") return(stats) } p = ggplot(plot_data, aes(x = method, y = value, fill = method),coef = 5) + #geom_boxplot(coef = 5) + stat_summary(fun.data = calc_stat, geom="boxplot") + facet_wrap(~metric, scales = "free") + theme_bw()+ theme(legend.position = "bottom", panel.grid = element_blank(), axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5), axis.title = element_blank()) p

    2.去掉离群值每个facet的ylim不包含离群值

    calc_stat <- function(x) { coef <- 1.5 n <- sum(!is.na(x)) # calculate quantiles stats <- quantile(x, probs = c(0.1, 0.25, 0.5, 0.75, 0.9)) names(stats) <- c("ymin", "lower", "middle", "upper", "ymax") return(stats) } p = ggplot(plot_data, aes(x = method, y = value, fill = method),coef = 5) + #geom_boxplot(coef = 5) + stat_summary(fun.data = calc_stat, geom="boxplot") + facet_wrap(~metric, scales = "free") + theme_bw()+ theme(legend.position = "bottom", panel.grid = element_blank(), axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5), axis.title = element_blank()) p
    Processed: 0.012, SQL: 8