简体   繁体   English

创建一个箱线图ggplot2

[英]Creating a boxplot, ggplot2

I have the following data set: 我有以下数据集:

depth <- data.frame(Sample = c("AD_001", "AD_009", "AD_017", "AD_025", 
                           "AD_033", "AD_041", "AD_049", "AD_057", 
                           "AD_065", "AD_073", "AD_081", "AD_089"), 
                median = c(12, 13, 11, 12, 12, 12, 13, 13, 14, 15, 15, 13), 
                granular_first_quartile = c(5, 6, 5, 6, 5, 6, 6, 6, 7, 7, 7, 6), 
                granular_third_quartile = c(23, 25, 21, 22, 23, 23, 24, 25, 27, 28, 28, 24))

and would like to create a boxplot but, the graphs I'm generating do not have an equally separated x field. 并想创建一个箱线图,但是,我正在生成的图形没有相等分隔的x字段。

ggplot(depth, aes(as.factor(Sample))) + geom_boxplot(aes(middle = median, lower = granular_first_quartile, upper = granular_third_quartile, ymin = granular_first_quartile, ymax = granular_third_quartile), stat = 'identity') + coord_flip()

Thanks for the help! 谢谢您的帮助!

You already have all (median, Q1, and Q3), and just need to assign lower , upper , middle , ymin , and ymax 您已经拥有所有(中位数,Q1和Q3),并且只需要分配loweruppermiddleyminymax

(FYI, https://en.wikipedia.org/wiki/Box_plot#/media/File:Boxplot_vs_PDF.svg ) (仅供参考, https ://en.wikipedia.org/wiki/Box_plot#/media/File: Boxplot_vs_PDF.svg

ggplot(depth, aes(Sample, median)) +
geom_boxplot(aes(lower = granular_first_quartile, upper = granular_third_quartile, 
middle = median, 
ymin = granular_first_quartile - 1.5*(granular_third_quartile-granular_first_quartile), 
ymax = granular_third_quartile+1.5*(granular_third_quartile-granular_first_quartile)),
stat="identity")+ coord_flip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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