简体   繁体   中英

How to make an overall boxplot alongside factors in R?

I am trying to create a boxplot that shows all of the factors of a variable, along with sample size, and at eh end of the plot also want an overall boxplot that combines all of the values into one. I am using the following line of code to do everything except making the overall plot:

library(ggplot2) 
library(plyr)
xlabels <- ddply(extract8, .(Fuel), summarize, xlabels = paste(unique(Fuel), '\n(n = ', length(Fuel),')'))

ggplot(extract8, aes(x = Fuel, y = Exfiltration.Fraction.Percentage))+geom_boxplot()+
  stat_boxplot(geom='errorbar', linetype=1) + 
  geom_boxplot(fill="pink") + geom_hline(yintercept = 0.4) + 
  scale_x_discrete(labels = xlabels[['xlabels']]) + ggtitle("Exfiltration Fraction (%) by Fuel   Type")

Not sure on how to proceed regarding adding a boxplot that combines all of the factors into one.

This is certainly not the most elegant way to solve it, but it works:

  1. Copy your dataset into a new object.
  2. Within the new object, replace the content of the variable containing the factors with the label you would like, for instance, "Total".
  3. Use rbind to attach the old and new objects together and attribute the result to the new object.
  4. In ggplot replace the old object by the new object.

I had the same issue, couldn't find an answer and proceeded this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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