ggplot2 : Plot mean with geom_bar

后端 未结 3 1542
你的背包
你的背包 2020-11-30 03:33

I have the following data frame:

test2 <- data.frame(groups = c(rep(\"group1\",4), rep(\"group2\",4)), 
    X2 = c(rnorm(4), rnorm(4)) , 
    label = c(re         


        
相关标签:
3条回答
  • 2020-11-30 03:47

    Try using ggpubr. It creates ggplot2-like charts.

    library(ggpubr)
    
    ggbarplot(test2, x = "label", y = "X2",
              add = "mean", fill = "groups")
    

    Alternatively, add a facet:

    ggbarplot(test2, x = "label", y = "X2",
              add = "mean", fill = "groups",
              facet.by = "groups")
    

    0 讨论(0)
  • ggplot2 likes 1 data point for 1 plot point. Create a new data frame with your summary statistics, then plot with stat="identity"

    require(reshape2)
    plot.data <- melt(tapply(test2$X2, test2$groups,mean), varnames="group", value.name="mean")
    
     ggplot(plot.data, aes(x=group,y=mean)) + geom_bar(position="dodge", stat="identity")
    

    0 讨论(0)
  • 2020-11-30 03:54

    simply use stat = "summary" and fun.y = "mean"

    ggplot(test2) + 
      geom_bar(aes(label, X2, fill = as.factor(groups)), 
               position = "dodge", stat = "summary", fun.y = "mean")
    

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