How to make an overall boxplot alongside factors in R?

孤人 提交于 2020-01-13 07:09:31

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/18190627/how-to-make-an-overall-boxplot-alongside-factors-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!