繁体   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