Search code examples
rggplot2geom-bar

How to plot geom_line over bar chart grouped by x variable not fill variable?


I have a data frame df

  Group Time_Period     mean      uci      lci
1     A      Before 4.712195 5.054539 4.369852
2     A       After 5.881463 6.241784 5.521142
3     B      Before 5.349754 5.872940 4.826567
4     B       After 6.653595 7.246231 6.060959

I want to plot this to illustrate that there is no difference in the mean increase between groups. I tried the following :

ggplot(df, aes(x=Time_Period, y=mean, fill=Group)) +
  geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
  geom_point(position = position_dodge(width = 1))+
  geom_line(aes(group=Group, color=Group), color=c("cyan4","firebrick","cyan4","firebrick"), size =1, position = position_dodge(width = 1)) +
  geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
  theme_bw() +
  scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
  theme(panel.grid.minor = element_line(colour="lightgrey", size=0.5)) +
  theme(panel.grid.major = element_line(colour="grey", size=0.5)) +
  labs(y="Sales", x="Time Period", fill="Category") +
  theme(axis.text.x = element_text(face="bold", size=12)) +
  theme(axis.text.y = element_text(face="bold", size=12)) +
  theme(axis.title.x = element_text(face="bold", size=16)) +
  theme(axis.title.y = element_text(face="bold", size=16)) +
  theme(legend.text= element_text(face="bold", size=12)) +
  theme(legend.title= element_text(face="bold", size=16))

which plots:

plot1

However my manager is concerned it is difficult to differentiate the two lines due to the overlap, so he told me to rearrange the columns so that x is Group and fill is Time Period.

I tried the following:

ggplot(df, aes(x=Group, y=mean, fill=Time_Period)) +
  geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
  geom_point(position = position_dodge(width = 1))+
  geom_line(aes(group=Group), color="black", size =1, position = position_dodge(width = 1)) +
  geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
  theme_bw() +
  scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
  theme(panel.grid.minor = element_line(colour="lightgrey", size=0.5)) +
  theme(panel.grid.major = element_line(colour="grey", size=0.5)) +
  labs(y="Sales", x="Group", fill="Time Period") +
  theme(axis.text.x = element_text(face="bold", size=12)) +
  theme(axis.text.y = element_text(face="bold", size=12)) +
  theme(axis.title.x = element_text(face="bold", size=16)) +
  theme(axis.title.y = element_text(face="bold", size=16)) +
  theme(legend.text= element_text(face="bold", size=12)) +
  theme(legend.title= element_text(face="bold", size=16))

But I can't work out how to get the lines to plot correctly between the two bars instead of just vertically in the centre, even if I adjust the "width" argument for position_dodge:

enter image description here

Please could anyone advise me on how to fix the plot?


Solution

  • You're looking for position_dodge2(). There's a little about it on the ggplot2 dodge reference, and a little more in the actual code on Github. The relevant section below, with some emphasis added:

    Dodging preserves the vertical position of an geom while adjusting the horizontal position. position_dodge2 is a special case of position_dodge for arranging box plots, which can have variable widths. position_dodge2 also works with bars and rectangles. But unlike position_dodge, position_dodge2 works without a grouping variable in a layer.

    So here's the code, with some of the theming removed.

    library(tidyverse)
    txt = "
    Group Time_Period     mean      uci      lci
    1     A      Before 4.712195 5.054539 4.369852
    2     A       After 5.881463 6.241784 5.521142
    3     B      Before 5.349754 5.872940 4.826567
    4     B       After 6.653595 7.246231 6.060959"
    
    df <- read.table(text = txt, header = TRUE) %>%
      mutate(Group = fct_relevel(Group, "A", "B")) %>%
      mutate(Time_Period = fct_relevel(Time_Period, "Before", "After"))
    
    ggplot(df, aes(x=Group, y=mean, fill=Time_Period)) +
      geom_bar(stat="identity", position=position_dodge(width = 1), color="black") +
      geom_point(position = position_dodge(width = 1))+
      geom_line(aes(group=Group), color="black", size =1, 
                position = position_dodge2(width = 1)) +
      geom_errorbar(aes(ymin=lci, ymax=uci), position=position_dodge(width = 1)) +
      theme_bw() +
      scale_y_continuous(limits=c(-0.2,8), breaks= seq(0,300,1), minor_breaks=seq(0,300,0.5)) +
      labs(y="Sales", x="Group", fill="Time Period") 
    

    Created on 2019-11-21 by the reprex package (v0.3.0)