簡體   English   中英

R 中匯總和分組數據的箱線圖

[英]Boxplot with Summarized and Grouped Data in R

我有以下預先匯總的成本數據:

平均成本 標准 中位數成本 較低的IQR 上IQR 狀態組 年齡階層
700 500 650 510 780 死的 年輕的
800 600 810 666 1000 年輕的
500 200 657 450 890 昏迷 年輕的
300 400 560 467 670 死的 老的
570 600 500 450 600 老的
555 500 677 475 780 昏迷 老的
333 455 300 200 400 死的 中間
678 256 600 445 787 中間
1500 877 980 870 1200 昏迷 中間

我希望用這些信息創建一個箱線圖 - 類似於下面的。 其中每種顏色代表狀態組(藍色=死亡,讀取=活着,綠色=昏迷)。 每個分組的集群代表一個年齡組(左集群=年輕,中間集群=中,右集群=老)。

在此處輸入圖像描述

我知道我沒有最小值和最大值,所以不需要胡須。
我想在 R 中對此進行編碼,我們將不勝感激。 謝謝你。

這是我嘗試過的代碼:

 dattest<- data.frame(
  Mean_Cost = c(700,800,500,300,570,555,333,678,1500), 
  Std = c(500,600,200,400,600,500,455,256,877), 
  Median_Cost = c(650,810,657,560,500,677,300,600,980), 
  LowerIQR = c(510,666,450,467,450,475,200,445,870), 
  UpperIQR = c(780,1000,890,670,600,780,400,787,1200), 
  StatusGroup = c(1,2,3,1,2,3,1,2,3),
  AgeGroup = c(1,1,1,2,2,2,3,3,3))

其中 StatusGroup 1=死亡,2=活着,3-昏迷
對於年齡組 1=年輕,2=老,3=中

 ggplot(dattest, aes(xmin = AgeGroup-.25, xmax=AgeGroup+.25, ymin=LowerIQR, ymax=UpperIQR)) + 
    geom_rect(fill="transparent", col = "blue") + 
    geom_segment(aes(y=Median_Cost, yend=Median_Cost, x=AgeGroup-.25, xend=AgeGroup+.25), col="blue") + 
    geom_point(mapping=aes(x = StatusGroup, y = Mean_Cost), col="red") +
    scale_x_continuous(breaks=1:3, labels=c("Young","Old","Middle")) + 
    theme_classic()

這段代碼絕對沒有給我我想要的

這是你想要做的嗎?

library(tidyverse)
df <- tibble::tribble(
  ~MeanCost, ~Std, ~MedianCost, ~LowerIQR, ~UpperIQR, ~StatusGroup, ~AgeGroup,
       700L, 500L,        650L,      510L,      780L,       "Dead",   "Young",
       800L, 600L,        810L,      666L,     1000L,      "Alive",   "Young",
       500L, 200L,        657L,      450L,      890L,   "Comatose",   "Young",
       300L, 400L,        560L,      467L,      670L,       "Dead",     "Old",
       570L, 600L,        500L,      450L,      600L,      "Alive",     "Old",
       555L, 500L,        677L,      475L,      780L,   "Comatose",     "Old",
       333L, 455L,        300L,      200L,      400L,       "Dead",  "Middle",
       678L, 256L,        600L,      445L,      787L,      "Alive",  "Middle",
      1500L, 877L,        980L,      870L,     1200L,   "Comatose",  "Middle"
  )

df %>% 
  mutate(AgeGroup = factor(AgeGroup, levels = c("Young", "Middle", "Old"))) %>% 
  ggplot(aes(x = AgeGroup, fill = StatusGroup)) +
  geom_boxplot(aes(
    lower = LowerIQR, 
    upper = UpperIQR, 
    middle = MedianCost, 
    ymin = MedianCost - Std, 
    ymax = MedianCost + Std),
    stat = "identity", width = 0.5)

測試.png

編輯

要在平均值處添加“x”,您可以調整 position:

df %>% 
  mutate(AgeGroup = factor(AgeGroup, levels = c("Young", "Middle", "Old"))) %>% 
  ggplot(aes(x = AgeGroup, fill = StatusGroup)) +
  geom_boxplot(aes(
    lower = LowerIQR, 
    upper = UpperIQR, 
    middle = MedianCost, 
    ymin = MedianCost - Std, 
    ymax = MedianCost + Std),
    stat = "identity", width = 0.5) +
  geom_point(aes(y = MeanCost),
             position = position_dodge(width = 0.5),
             shape = 4)

測試2.png

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM