簡體   English   中英

R ggplot2-如何在相同的x值上繪制2個箱形圖

[英]R ggplot2 - How to plot 2 boxplots on the same x value

假設我有兩個箱型圖。

trial1 <- ggplot(completionTime, aes(fill=Condition, x=Scenario, y=Trial1))
trial1 + geom_boxplot()+geom_point(position=position_dodge(width=0.75)) + ylim(0, 160)

trial2 <- ggplot(completionTime, aes(fill=Condition, x=Scenario, y=Trial2))
trial2 + geom_boxplot()+geom_point(position=position_dodge(width=0.75)) + ylim(0, 160)

我如何在相同的地塊和相同的X上繪制試驗1和試驗2? 它們的y范圍相同。

我看了geom_boxplot(position =“ identity”),但是在同一X上繪制了兩個條件(填充)。

我想在同一X上繪制兩個y列。


編輯:數據集

User Condition Scenario Trial1 Trial2
1     1        ME        a     67     41
2     1        ME        b     70     42
3     1        ME        c     40     15
4     1        ME        d     65     23
5     1        ME        e     45     45
6     1        SE        a    100     34
7     1        SE        b     54     23
8     1        SE        c     70     23
9     1        SE        d     56     15
10    1        SE        e     30     20
11    2        ME        a     42     23
12    2        ME        b     22     12
13    2        ME        c     28      8
14    2        ME        d     22      8
15    2        ME        e     38     37
16    2        SE        a     59     18
17    2        SE        b     65     14
18    2        SE        c     75      7
19    2        SE        d     37      9
20    2        SE        e     31      7

dput()

structure(list(User = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Condition = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L), .Label = c("ME", "SE"), class = "factor"), Scenario = 
structure(c(1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L), .Label = c("a", "b", "c", "d", "e"), class = "factor"), 
Trial1 = c(67L, 70L, 40L, 65L, 45L, 100L, 54L, 70L, 56L, 
30L, 42L, 22L, 28L, 22L, 38L, 59L, 65L, 75L, 37L, 31L), Trial2 = c(41L, 
42L, 15L, 23L, 45L, 34L, 23L, 23L, 15L, 20L, 23L, 12L, 8L, 
8L, 37L, 18L, 14L, 7L, 9L, 7L)), .Names = c("User", "Condition", 
"Scenario", "Trial1", "Trial2"), class = "data.frame", row.names = c(NA, 
-20L))

您可以嘗試使用interaction來結合兩個因素,並針對第三個因素進行規划。 例如,假設您要按原始代碼中的條件進行填充:

library(tidyr)
completionTime %>% 
gather(trial, value, -Scenario, -Condition, -User) %>%
ggplot(aes(interaction(Scenario, trial), value)) + geom_boxplot(aes(fill = Condition))

結果: 在此處輸入圖片說明

暫無
暫無

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

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