首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将两个ggplot()箱图合并在一起

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

Stack Overflow用户
提问于 2020-11-21 07:06:32
回答 2查看 86关注 0票数 1

我正在试着制作一个有4个盒子图的图。两个用于Word测试: Us和非Us,两个用于简单测试:Us和非Us。我正在尝试将它们合并在一起,这样它们看起来就像这样:

What I want the graph to look like

我为每个测试创建了两个ggplot,但我想不出一种方法来将它们合并在一起。这是我使用的tibble。请注意,Q65用于简单测试,而Q66用于单词测试。

代码语言:javascript
运行
复制
    # 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的代码:

代码语言:javascript
运行
复制
       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")))

代码语言:javascript
运行
复制
    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,但我希望它出现在一个图中,而不是并排:

代码语言:javascript
运行
复制
grid.arrange(WordTestPlot ,EasyTestPlot, nrow = 1, ncol =2)

What the graphs currently look like

我该怎么做才能让盒子图看起来像第一张图片呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-21 07:10:21

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

代码语言:javascript
运行
复制
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)

现在使用

代码语言:javascript
运行
复制
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))
票数 0
EN

Stack Overflow用户

发布于 2020-11-21 07:27:29

您需要的是一种刻面方法。为此,您可以使用facet_wrap()ggplot2中的常见数据结构是像其他用户一样使用整形后的数据。这里是多面图的代码。df是您的数据:

代码语言:javascript
运行
复制
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')替换小平面直线。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64938095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档