简体   繁体   English

在基于百分位数的箱线图上向晶须添加水平条

[英]Add horizontal bars to whiskers on percentile based boxplot

I have my data and plot as: 我的数据和图为:

new_df <- structure(list(Group = c("k__Fungi; p__Ascomycota; c__Eurotiomycetes; o__Chaetothyriales; f__Chaetothyriaceae; g__unidentified", 
"k__Fungi; p__Ascomycota; c__Eurotiomycetes; o__Chaetothyriales; f__Chaetothyriaceae; g__unidentified", 
"k__Fungi; p__Ascomycota; c__Eurotiomycetes; o__Chaetothyriales; f__Chaetothyriaceae; g__unidentified", 
"k__Fungi; p__Ascomycota; c__Eurotiomycetes; o__Chaetothyriales; f__Chaetothyriaceae; g__unidentified"
), Percentile_0 = c(1, 1, 1, 1), Percentile_25 = c(1, 17.75, 
8, 99.5), Percentile_50 = c(1, 48, 32, 215.5), Percentile_75 = c(3, 
93, 51.25, 343.75), Percentile_100 = c(28, 337, 104, 788), Type = c("T1", 
"T2", "T3", "T4")), row.names = c(NA, -4L), class = "data.frame")


#plot 
ggplot(data = new_df, aes(x =Group, group = Type, fill = Type)) +
    geom_boxplot(
      stat = "identity",
      aes(
        ymin = Percentile_0,
        lower = Percentile_25,
        middle = Percentile_50,
        upper = Percentile_75,
        ymax = Percentile_100
      )
    ) +
    theme_classic()

在此处输入图片说明

I would like to add horizontal whiskers as stated in this thread here . 我想在这个线程说明添加水平晶须这里

Since you have already calculated the values for the ends of the whiskers you can use geom_errorbar() directly rather than via stat_boxplot() as in the link you gave. 由于您已经计算了晶须末端的值,因此可以直接使用geom_errorbar() ,而不必像给出的链接中那样通过stat_boxplot()来使用。

You will need to explicitly dodge the error bars to match the default dodging of the boxplots. 您将需要显式躲避误差线以匹配默认的箱形图躲避。

The required aesthetics for geom_errobar() are ymin and ymax . geom_errobar()所需的美观geom_errobar()yminymax I put these within the geom_errorbar() layer. 我将它们放在geom_errorbar()层中。 since you use these for both the boxplots and the errorbars you could move them up to the global aes() to avoid repetition. 由于将它们同时用于箱线图和错误栏,因此可以将它们移至全局aes()以避免重复。

ggplot(data = new_df, aes(x = Group, group = Type, fill = Type)) +
    geom_errorbar(aes(ymin = Percentile_0, 
                      ymax = Percentile_100), 
                  width = 0.5, 
                  position = position_dodge(width = 0.9) ) +
    geom_boxplot(
        stat = "identity",
        aes(
            ymin = Percentile_0,
            lower = Percentile_25,
            middle = Percentile_50,
            upper = Percentile_75,
            ymax = Percentile_100
        )
    ) +
    theme_classic()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM