将两个 ggplot() 箱线图合并在一起

Merging two ggplot() boxplots together

我正在尝试制作一个包含 4 个箱线图的图表。两个用于单词测试:我们和非美国,两个用于简单测试:我们和非我们。我正在尝试将它们合并在一起,所以它们看起来像这样:

What I want the graph to look like

我为每个测试制作了两个 ggplot,但我想不出将它们合并在一起的方法。这是我使用的tibble。请注意Q65是Easy测试,Q66是Word测试。

    # A tibble: 86 x 5
     Q65    Q66 nationality race_ethnicity  type
   <dbl>  <dbl> <chr>       <chr>          <dbl>
 1  55.1   51.2 Mexican     Mexican            2
 2  62.7   38.7 American    Asian              1
 3  53.2   34.7 USA         Hispanic           1
 4 193.    84.6 chinese     asian              2
 5  57.0   40.9 Taiwan      Asian              2
 6 103.    58.5 American    caucasian          1
 7  49.2   35.5 White       White              2
 8 213.  2135.  Chinese     Asian              2
 9  85.6   52.1 Chinese     Chinese            2
10 168.   113.  China       Asian              2
# ... with 76 more rows

这是 ggplots 的代码:

       EasyTestPlot <-
      ggplot(Qtibble, aes(as.factor(Qtibble$type), Q65, color = factor(type))) + geom_boxplot(
        alpha = 0.5, outlier.colour = "black", outlier.fill = "black", outlier.shape = 21, outlier.size = 
        2 ) + coord_cartesian(ylim = c(0, 250)) + labs(x = "Easy Test", y = "Number of Seconds to 
        Compare") + theme_light() + theme(axis.text.x = element_text(face = "bold", size = 10, angle =0),
        axis.text.y = element_text(face = "bold", size = 10, angle = 0))  + scale_x_discrete(labels = c("1" = "US", "2" = "Non-US")) + stat_summary(fun = mean, color = "darkred", position = position_dodge(0.75), geom = "point", shape = 18, size = 3, show.legend = FALSE) + scale_color_manual(values=c("Dark green", "red")) + labs(colour="Nationality",linetype="Nationality",shape="Nationality") + theme(legend.text = element_text(color = "white"),legend.title = element_text(color = "white"), legend.key = element_rect(fill = "white")) 
+ scale_color_discrete(guide = 
guide_legend(override.aes = list(color = "white")))

    WordTestPlot <-
      ggplot(Qtibble, aes(as.factor(Qtibble$type), Q66, color = factor(type))) + geom_boxplot(
        alpha = 0.5, outlier.colour = "black", outlier.fill = "black", outlier.shape = 21, outlier.size = 
        2 ) + coord_cartesian(ylim = c(0, 250)) + labs(x = "Word Test", y = "Number of Seconds to 
        Compare") + theme_light() + theme(axis.text.x = element_text(face = "bold", size = 10, angle =0),
        axis.text.y = element_text(face = "bold", size = 10, angle = 0))  + scale_x_discrete(labels = c("1" = "US", "2" = "Non-US")) + stat_summary(fun = mean, color = "darkred", position = position_dodge(0.75), geom = "point", shape = 18, size = 3, show.legend = FALSE) + scale_color_manual(values=c("Dark green", "red")) + labs(colour="Nationality",linetype="Nationality",shape="Nationality") + theme(legend.text = element_text(color = "white"),legend.title = element_text(color = "white"), legend.key = element_rect(fill = "white")) 
+ scale_color_discrete(guide = 
guide_legend(override.aes = list(color = "white")))

此外,我正在使用 grid.arrange,但我希望它不是并排显示在一张图表上:

grid.arrange(WordTestPlot ,EasyTestPlot, nrow = 1, ncol =2)

What the graphs currently look like

如何使箱线图看起来像第一张图片?

我们可以将数据重塑为 'long' 格式,然后应用 ggplot

library(dplyr)
library(tidyr)
library(ggplot2)
Qtibble_long <- Qtibble %>%
    pivot_longer(cols = Q65:Q66, names_to = 'Q6566') %>%
    mutate(type = factor(type))  %>%
    unite(typenew, Q6566, type)

现在使用

ggplot(Qtibble_long, aes(typenew, value, color = typenew)) + 
 geom_boxplot(alpha = 0.5, outlier.colour = "black",
    outlier.fill = "black", outlier.shape = 21, outlier.size = 
     2) + 
 coord_cartesian(ylim = c(0, 250))

你需要的是一个方面的方法。您可以为此使用 facet_wrap()ggplot2 中数据的通用结构与其他用户一样使用重塑数据。这里是多面图的代码。 df 是你的数据:

library(dplyr)
library(tidyr)
library(ggplot2)
#Code
df %>% pivot_longer(-c(nationality,race_ethnicity,type)) %>%
  ggplot(aes(x=factor(type),y=value,color = factor(type)))+
  geom_boxplot(alpha = 0.5, outlier.colour = "black",
               outlier.fill = "black", outlier.shape = 21,
               outlier.size =2)+coord_cartesian(ylim = c(0, 250))+
               labs(x = "Easy Test", y = "Number of Seconds to 
        Compare") +
  theme_light() +
  theme(axis.text.x = element_text(face = "bold", size = 10, angle =0),
        axis.text.y = element_text(face = "bold", size = 10, angle = 0))+
  scale_x_discrete(labels = c("1" = "US", "2" = "Non-US"))+
  stat_summary(fun = mean, color = "darkred",
               position = position_dodge(0.75),
               geom = "point",
               shape = 18, size = 3,
               show.legend = FALSE) +
  scale_color_manual(values=c("Dark green", "red")) +
  labs(colour="Nationality",linetype="Nationality",shape="Nationality") +
  theme(legend.text = element_text(color = "white"),
        legend.title = element_text(color = "white"),
        legend.key = element_rect(fill = "white"))+
  facet_wrap(.~name,scales = 'free',strip.position = 'bottom')+
  theme(panel.grid = element_blank(),
        strip.placement = 'outside',
        strip.background = element_blank(),
        strip.text = element_text(color='black',face='bold'))

输出:

如果您只想要一个 y 轴,您可以将小平面线替换为 facet_wrap(.~name,scales = 'free_x',strip.position = 'bottom')