首页 > 解决方案 > 在 R 中使用 PreSummarized 数据创建 BoxPlot

问题描述

我汇总了已经汇总的成本数据。我无权访问原始数据。我有平均值、标准差、中位数和 IQR。数据如下所示:

在此处输入图像描述

我想使用这些数据在 R 中创建箱线图。任何帮助,将不胜感激。

标签: rggplot2boxplot

解决方案


这个怎么样:

dat <- data.frame(
  Mean_Cost = c(300, 760, 500), 
  Std = c(20,55,100), 
  Median_Cost = c(200, 222, 467), 
  LowerIQR = c(150, 100, 333), 
  UpperIQR = c(220, 300, 500), 
  Group = c(1,2,3))


ggplot(dat, aes(xmin = Group-.25, xmax=Group+.25, ymin=LowerIQR, ymax=UpperIQR)) + 
  geom_rect(fill="transparent", col = "black") + 
  geom_segment(aes(y=Median_Cost, yend=Median_Cost, x=Group-.25, xend=Group+.25)) + 
  scale_x_continuous(breaks=1:3, labels=c("Group 1", "Group 2", "Group 3")) + 
  theme_classic() + 
  labs(x="", y="Cost")

在此处输入图像描述


推荐阅读