Creating a bar graph with several variables in the x axis

Viewed 73

I have the following dataset (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

I want to create a bar graph using ggplot2 with the following conditions:

  • y axis: mean
  • x axis: phase + construction
  • facet: group
  • error bars: standard error (se_top, se_bottom)

I have used this code:

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") 

However, in the graph that I get, columns are stacked on top of each other, even though I have used position = "dodge" in my code:

enter image description here

What should I change in order to get the graph I want?

1 Answers

Maybe this can be useful:

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") 

Output:

enter image description here

Update: In order to get some order, try this:

#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") 

Output:

enter image description here

Related