rggplot2discrete

Spacing on x-axis using ggplot2 discrete scale


I've been working with this code for a while but this is the first time I've only had two categories on my x-axis. For some reason, R will not space out the bins and instead bunches them at the beginning. How do I evenly spread them along the x-axis?

xlabels<-c("A","B")
CTplot <- ggplot(CTsum, aes(x=Treatment, y=FI)) + 
  geom_col(fill="lightsteelblue") + 
  scale_x_discrete(labels= xlabels)+xlab("")+ylab("")+
  theme(panel.background = element_blank(),axis.line = element_line(colour = "black"),axis.text.x = element_text(size=20,color="black"), axis.text.y = element_text(size=20, color="black"), axis.title.x = element_text(size=25), axis.title.y = element_text(size=25))+
  geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width=0.2))+
  annotate("text",x=1,y=0.84, label="A",size=5)+ 
  annotate("text",x=2,y=0.62, label="B",size=5)+ 
  annotate("text",x=3,y=0.29, label="",size=5)+ 
  annotate("text",x=4,y=0.26, label="",size=5)+
  annotate("text",x=0.65,y=0.5, label="",size=15)
CTplot

enter image description here


Solution

  • When you plot categorical data on the x axis, you are "really" plotting at integer values x = 1, x = 2, etc, but with the text labels used in place of numbers. This is what allows you to put text annotations at x = 1 and x = 2.

    However, the bars are bunched at the left because you have added two empty text annotations over to the right (at position x = 3 and x = 4). The plot has expanded right to accommodate them. Since they are empty anyway, you don't need them. Here is the plot without them:

    CTplot <- ggplot(CTsum, aes(Treatment, FI)) + 
      geom_col(fill = "lightsteelblue") + 
      geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width = 0.2)) +
      scale_x_discrete(labels = xlabels) +
      xlab("") +
      ylab("") +
      theme(panel.background = element_blank(),
            axis.line = element_line(colour = "black"),
            axis.text.x = element_text(size=20,color="black"), 
            axis.text.y = element_text(size=20, color="black"), 
            axis.title.x = element_text(size=25), 
            axis.title.y = element_text(size=25))+
      annotate("text",x=1,y=0.84,label="A",size = 5) +
      annotate("text",x=2,y=0.62,label="B",size = 5)
    
    CTplot
    

    enter image description here

    And here it is with an empty annotation at x = 4:

    CTplot + annotate("text",x = 4, y = 0.29, label = "", size = 5) 
    

    enter image description here

    To emphasise the point, let's see an empty annotation at x = 20:

    CTplot + annotate("text",x = 20, y = 0.29, label = "", size = 5) 
    

    enter image description here

    As you can see, the x axis has had to expand to accommodate the invisible text annotation at x = 20.


    If you want the bars a bit more spread out, you can do something like:

    CTplot <- ggplot(CTsum, aes(Treatment, FI)) + 
      geom_col(fill = "lightsteelblue", width = 0.6) + 
      geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width = 0.2)) +
      scale_x_discrete(labels = xlabels, expand = c(0.75, 0)) +
      xlab("") +
      ylab("") +
      theme(panel.background = element_blank(),
            axis.line = element_line(colour = "black"),
            axis.text.x = element_text(size=20,color="black"), 
            axis.text.y = element_text(size=20, color="black"), 
            axis.title.x = element_text(size=25), 
            axis.title.y = element_text(size=25))+
      annotate("text",x=1,y=0.84,label="A",size = 5) +
      annotate("text",x=2,y=0.62,label="B",size = 5)
    
    CTplot
    

    enter image description here


    Data used (approximated from image in OP)

    CTsum <- data.frame(Treatment = c("A", "B"), FI = c(0.71, 0.48), meanse = 0.1)