首页 > 解决方案 > 在 x 轴上创建具有多个变量的条形图

问题描述

我有以下数据集(graph_data):

# A tibble: 18 x 7
   construction phase     group         mean    se se_top se_bottom
   <chr>        <fct>     <fct>        <dbl> <dbl>  <dbl>     <dbl>
 1 hacer        pre-test  heritage     7.67  3.67  11.3       4    
 2 hacer        treatment heritage    15.5   3.00  18.5      12.5  
 3 hacer        post-test heritage     9.83  4.25  14.1       5.58 
 4 acc          pre-test  heritage     0.166 0.166  0.332     0    
 5 acc          treatment heritage     4.33  2.67   7.00      1.67 
 6 acc          post-test heritage     0.166 0.166  0.332     0    
 7 spe          pre-test  heritage     2.33  1.36   3.69      0.975
 8 spe          treatment heritage     6.67  2.69   9.36      3.98 
 9 spe          post-test heritage     0.833 0.477  1.31      0.356
10 hacer        pre-test  monolingual  1     0.707  1.71      0.293
11 hacer        treatment monolingual  1     0.577  1.58      0.423
12 hacer        post-test monolingual  0.25  0.25   0.5       0    
13 acc          pre-test  monolingual  0     0      0         0    
14 acc          treatment monolingual  1     0.577  1.58      0.423
15 acc          post-test monolingual  0     0      0         0    
16 spe          pre-test  monolingual  4     3.37   7.37      0.634
17 spe          treatment monolingual 15.8   2.36  18.1      13.4  
18 spe          post-test monolingual  3.5   3.18   6.68      0.325

ggplot2我想使用以下条件创建条形图:

我用过这段代码:

graph_data %>%
  ggplot(aes(graph_data, x=phase, y=mean, fill =phase)) + 
  geom_bar(stat = "identity", color = "black", position = "dodge") + 
  scale_y_continuous(limits = c(0,20)) +
  labs(x = "Phase", y = "Average number of targets produced") + 
  facet_wrap( ~ group) + 
  geom_errorbar(aes(ymin= se_bottom, ymax = se_top), width=.2,
                position=position_dodge(.9)) +
  theme(text = element_text(size=20)) + 
  theme_classic() +  scale_fill_manual(values=c("#90EE90", "#3CB371", "#2E8B57")) +
  theme(axis.text=element_text(size=16),
        axis.title=element_text(size=15,face="bold"), 
        axis.text.x = element_text(angle=45, hjust = 1),
        legend.position = "none") 

但是,在我得到的图表中,列堆叠在一起,即使我position = "dodge"在我的代码中使用过:

在此处输入图像描述

为了得到我想要的图表,我应该改变什么?

标签: rggplot2

解决方案


也许这很有用:

library(ggplot2)
#Code
graph_data %>%
  ggplot(aes(graph_data, x=interaction(phase,construction), y=mean, fill =phase,group=group)) + 
  geom_bar(stat = "identity", color = "black", position = position_dodge(0.9)) + 
  scale_y_continuous(limits = c(0,20)) +
  labs(x = "Phase", y = "Average number of targets produced") + 
  facet_wrap( ~ group) + 
  geom_errorbar(aes(ymin= se_bottom, ymax = se_top), width=.2,
                position=position_dodge(.9)) +
  theme(text = element_text(size=20)) + 
  theme_classic() +  scale_fill_manual(values=c("#90EE90", "#3CB371", "#2E8B57")) +
  theme(axis.text=element_text(size=16),
        axis.title=element_text(size=15,face="bold"), 
        axis.text.x = element_text(angle=45, hjust = 1),
        legend.position = "none") 

输出:

在此处输入图像描述

更新:为了得到一些订单,试试这个:

#Code 2
graph_data %>%
  mutate(phase=factor(phase,levels = c('pre-test','treatment','post-test'),
         ordered = T)) %>%
  arrange(phase) %>%
  mutate(conc=paste(as.character(phase),construction),
         conc=factor(conc,levels = unique(conc),ordered = T)) %>%
  ggplot(aes(graph_data, x=conc, y=mean, fill =phase,group=group)) + 
  geom_bar(stat = "identity", color = "black", position = position_dodge(0.9)) + 
  scale_y_continuous(limits = c(0,20)) +
  labs(x = "Phase", y = "Average number of targets produced") + 
  facet_wrap( ~ group) + 
  geom_errorbar(aes(ymin= se_bottom, ymax = se_top), width=.2,
                position=position_dodge(.9)) +
  theme(text = element_text(size=20)) + 
  theme_classic() +  scale_fill_manual(values=c("#90EE90", "#3CB371", "#2E8B57")) +
  theme(axis.text=element_text(size=16),
        axis.title=element_text(size=15,face="bold"), 
        axis.text.x = element_text(angle=45, hjust = 1),
        legend.position = "none") 

输出:

在此处输入图像描述


推荐阅读