2017-04-07 67 views
0

假設我有兩個箱形圖。R ggplot2 - 如何在相同的x值上繪製2個箱形圖

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(位置=「身份」),但描述了這兩個條件(補)在同一個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)) 
+3

你能使用dput添加數據集(),使重複的​​例子? –

+3

聽起來像你需要將這些數據從廣泛的變爲長時間; _i.e._將'trial1'和'trial2'的值放在一個'y'列中。但是我們需要看一些數據。 – neilfws

+0

謝謝,所以我想我有3個條件我想在一個陰謀?(條件/情景/試驗)。我只知道如何做2 ..我不想面對其中一個條件。 – kevin0228ca

回答

2

您可以嘗試使用interaction將您的兩個因素組合起來,並對三分之一進行繪圖。例如,假設你要根據條件在你原來的代碼填寫:

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

結果: enter image description here

+0

謝謝。儘管我最初想知道這是可能的,例如, a.Trial1下a.Trial1(可能是不同的顏色),但也許這種情節沒有意義,或者是ggplot2的限制。反正這是一個很好的解決方案。 – kevin0228ca

相關問題