如何与R中的因素一起制作整体箱形图? [英] How to make an overall boxplot alongside factors in R?
本文介绍了如何与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")
不知道如何添加将所有因素合并为一个的boxplot。
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:
- 将您的数据集复制到新对象中。
- 在新对象中,将vari能够包含您想要的标签的因素,例如总计。 使用
rbind
来附加旧的和新的将对象结合到一起并将结果归因于新对象。 在
ggplot
中,用新对象替换旧对象。 - Copy your dataset into a new object.
- Within the new object, replace the content of the variable containing the factors with the label you would like, for instance, "Total".
- Use
rbind
to attach the old and new objects together and attribute the result to the new object. - In
ggplot
replace the old object by the new object.
我有同样的问题,无法找到答案并以此方式进行。
I had the same issue, couldn't find an answer and proceeded this way.
这篇关于如何与R中的因素一起制作整体箱形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文