【问题标题】:Creating a boxplot, ggplot2创建箱线图,ggplot2
【发布时间】:2016-05-04 20:23:58
【问题描述】:

我有以下数据集:

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))

并且想创建一个箱线图,但是我生成的图表没有等距的 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()

感谢您的帮助!

【问题讨论】:

  • ?geom_boxplot 中的最后一个示例向您展示了如何根据汇总统计信息制作箱线图。 stackoverflow.com/questions/22212885/… 也是如此
  • 您的数据在样本中每个因子水平只有一个中值。如果您创建单个值的箱线图,它只会返回该值的一条线,我假设这就是您的图的样子 - 每个样本级别的中值线。情节不会有任何胡须。

标签: r ggplot2 boxplot


【解决方案1】:

您已经拥有所有(中位数、Q1 和 Q3),只需分配 loweruppermiddleyminymax

(仅供参考,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()

【讨论】:

  • 很好的答案。我不需要胡须,所以我将 ymin 和 ymax 分别设置为 grain_first_quartile 和 grain_third_quartile。我唯一的问题是 x 轴的间距不均匀。
  • 但是,如果我使用你的答案,x 轴是均匀分布的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多