How to plot additional statistics in boxplot for each group?

后端 未结 1 2040
野趣味
野趣味 2020-12-19 20:07

I would like to see boxplots of combination of factors and I was told to use lattice for that. I tried it and it looks like this:

相关标签:
1条回答
  • 2020-12-19 20:48

    Here's one way using ggplot2. First we can compute the p-values separately for every month/country combination (I use data.table. you can use whichever way you're comfortable with). Then, we add geom_text and specify pvalue as the label and specify x and y coordinates where the text should be within each facet.

    require(data.table)
    dt <- data.table(df)
    pval <- dt[, list(pvalue = paste0("pval = ", sprintf("%.3f", 
            summary(aov(x ~ type))[[1]][["Pr(>F)"]][1]))), 
            by=list(country, month)]
    
    ggplot(data = df, aes(x=type, y=x)) + geom_boxplot() + 
    geom_text(data = pval, aes(label=pvalue, x="river", y=2.5)) + 
    facet_grid(country ~ month) + theme_bw() + 
    theme(panel.margin=grid::unit(0,"lines"), # thanks to @DieterMenne
    strip.background = element_rect(fill = NA), 
    panel.grid.major = element_line(colour=NA), 
    panel.grid.minor = element_line(colour=NA))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题