簡體   English   中英

R中多個條件的多個箱圖

[英]Multiple boxplots for multiple conditions in R

我明白了如何在其他幾個帖子的一個圖表中繪制多個箱圖。 但我有這種情況,我無法一起繪制多個條件。 我應用了與我以前的帖子( R中的多個 )相同的想法,但不適用於這種情況。

我有這個數據集

     Control                      Treatment
      L1    L2   L3  L4   L5        S1   S2    S3  S4  S5    
g1   10.5    12  10  11   12        13   14    10  11  12 
g2    11     13  10  10   11        10.5 12    8   9   10
g3    10     9   9   8    9         11   10    11  9   11
g4    9      8   8   9    8         6     5    5   7   6
g5    16     4   6.5 6.8  5         4     6    6   8   9
g6    11     12  7.8 7.5  6         5     4    9   10  11
g7    10     6   8.9 6.4  7.2       13    12   12  12  10
g8    5      4   9.0 5.6  7.8       12    12   9   8   7 
g9    11     12  11  8.5  7.4       10    11.5 8   7   6   
g10   8.9    7.8 13  5.6  6.8       7.6   5.8  5   4   5 

並且希望將多個條件表示為同一圖表中的多個箱圖。

我想制作第一個圖,比較L1從對照到S1和S2的治療,第二個圖比較L2和L3從對照到S3,S4,S5治療和第三個圖與L4和L5比較S4和S5治療。

這個多重條件箱圖如何可能? 或者我應該單獨制作這些箱圖,然后將它們放在同一個圖表中?

我不確定這是否是您正在尋找的,但它需要一些數據操作。

如果要在(L1,S1,S2 | L2,L3,S3,S4,S5 | L4,L5,S4,S5)的第四列(即“Group2”)中手動輸入分組,則需要進行分組復制S4和S5行並將它們放在適當的組中。 然后你會改變:

facet_wrap( ~ Group2, scales = 'free')

-

library(ggplot2)
library(reshape2)

control <- ## read in control data
control$group <- rep('control', nrow(control))
control <- melt(control, id.vars = 'group')

treatment <- ## read in control data
treatment$group <- rep('treatment', nrow(treatment))
treatment <- melt(treatment, id.vars = 'group')

allData <- rbind(control, treatment)

ggplot(allData, aes(x = variable, y = value, group = variable)) +
  geom_boxplot() +
  facet_wrap( ~ group, scales = 'free')

在此輸入圖像描述

- 更新 -

library(gdata)
library(reshape2)
library(ggplot2)

control <- ## read in control data
control$group <- rep('control', nrow(control))
control <- melt(control, id.vars = 'group')

treatment <- ## read in treatment data
treatment$group <- rep('treatment', nrow(treatment))
treatment <- melt(treatment, id.vars = 'group')

allData <- rbind(control, treatment)

compA <- subset(allData, 
              variable == 'L1' | 
              variable == 'S1' | 
              variable == 'S2')
compB <- subset(allData, 
              variable == 'L2' | 
              variable == 'L3' | 
              variable == 'S3' | 
              variable == 'S4' | 
              variable == 'S5')
compC <- subset(allData, 
              variable == 'L4' | 
              variable == 'L5' | 
              variable == 'S4' | 
              variable == 'S5')

allData <- combine(compA, compB, compC)

ggplot(allData, aes(x = variable, y = value, group = variable, fill = group)) +
  geom_boxplot() +
  facet_wrap( ~ source, scales = 'free_x')

在此輸入圖像描述

暫無
暫無

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

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