Search code examples
rggplot2geom-bar

How to change the size of x-axis for categorical variables in ggplot2?


I am tryig to adjust the size of my x-axis in a plot so that it won't have too much blank space in the plot area.

This is my plot:

criterio.fig <- ggplot(criterio_sum, aes(x = session,
                                y = mean,
                                fill = sham_cat)) +
        geom_bar(stat = "identity", position = "dodge", width = 0.5) +
        geom_errorbar(aes(ymin = se_low, ymax = se_high),
                      width = 0.2,
                      position = position_dodge(0.5)) +
        scale_fill_manual(values = c(roxo, verde)) +
        # Add a title
        ggtitle("") +
        scale_x_discrete(breaks = unique(criterio_sum$session), 
                         labels = c("Sessão 1", "Sessão 2")) +
        scale_y_continuous(expand = c(0, 0), limits = c(0, 0.5)) +
        # Customize the x-axis
        xlab("Sessão") +
        # Customize the y-axis
        ylab(eixo_y) +
        # Get rid of title for legend
        labs(fill = "") +
        # Remove dark background
        theme_minimal() +
        theme(panel.border = element_blank(),
              panel.grid = element_blank(),
              strip.background = element_rect(colour = "white", fill = "white"),
              legend.position = "top", axis.line = element_line(colour = "black"),
              axis.text.x = element_text(colour = "black", size = 11, vjust = 0.5),
              axis.text.y = element_text(colour = "black", size = 11, hjust = 0,
                                         margin = margin(r = 5)),
              axis.ticks.y = element_line(colour = "black"), 
              axis.ticks.length = unit(-0.07, "cm"),
              axis.title.x = element_text(colour = "black", face = "bold"),
              axis.title.y = element_text(colour = "black", face = "bold"),
              text = element_text(family = "Times New Roman", size = 13))

This is the data being used to make the graph:

  sham_cat session  medida    mean    sd     n      se se_low se_high
  <fct>    <fct>    <fct>    <dbl> <dbl> <int>   <dbl>  <dbl>   <dbl>
1 Sham     session1 Critério 0.209 0.264    48 0.00549  0.203   0.214
2 Sham     session2 Critério 0.338 0.412    51 0.00808  0.330   0.346
3 Catódica session1 Critério 0.184 0.282    49 0.00575  0.178   0.190
4 Catódica session2 Critério 0.319 0.362    50 0.00724  0.312   0.326

The graph from the code is this one below:

This is the image resulted from the code above

This is how I want it to be:

The graph should look like this

I have already tried a lot of things, including expand or change the size of the bars, adjusting the size of the window and etc. It changes the size of the bar, the size of the window, but the space between the categories stays the same.

Thanks in adavance.


Solution

  • You can get pretty close by using theme(asepct.ratio = 1) and increasing the width = in geom_bar and geom_errorbar.

    ggplot(criterio_sum, aes(x = session,
                             y = mean,
                             fill = sham_cat)) +
      geom_bar(stat = "identity", position = "dodge", width = 0.9) +
      geom_errorbar(aes(ymin = se_low, ymax = se_high),
                    width = 0.2,
                    position = position_dodge(0.9)) +
      scale_fill_manual(values = c("purple", "green")) +
      ggtitle("") +
      scale_x_discrete(breaks = unique(criterio_sum$session), 
                       labels = c("Sessão 1", "Sessão 2")) +
      scale_y_continuous(expand = c(0, 0), limits = c(0, 0.5)) +
      labs(x = "Sessão", y = expression(bold("Viés de Resposta"~(italic("c"))))) +
      labs(fill = "") +
      theme_minimal() +
      theme(aspect.ratio = 1,
            panel.border = element_blank(),
            panel.grid = element_blank(),
            strip.background = element_rect(colour = "white", fill = "white"),
            legend.position = "top", axis.line = element_line(colour = "black"),
            axis.text.x = element_text(colour = "black", size = 11, vjust = 0.5),
            axis.text.y = element_text(colour = "black", size = 11, hjust = 0,
                                       margin = margin(r = 5)),
            axis.ticks.y = element_line(colour = "black"), 
            axis.ticks.length = unit(-0.07, "cm"),
            axis.title.x = element_text(colour = "black", face = "bold"),
            axis.title.y = element_text(colour = "black", face = "bold"),
            text = element_text(family = "Times New Roman", size = 13))
    

    enter image description here

    Please note that I had to change the colors in my locale and manually set up your x-axis label because it wasn't provided.