Search code examples
rggplot2facetgeom-bar

Adjust axis limits on facet_share population pyarmid


I have created a population pyramid using geom_bar and facet_share. However, the data labels do not fit inside the panels with the current axis limits, or panel sizes.

I have tried forcing the panel size, but this just increases the size of everything in proportion. I have also tried setting the ylim, but this increases the axis in both directions.

I would like there to be enough space in the facet panels for the data labels to be visible.

#Here is the data

AgeGroupDat <-
structure(list(sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("Male", "Female"), class = "factor"), Age_Group = c("00 - 20", "16 - 38",                                                                                         "18 - 34", "20 - 41", "32 - 54", "41 - 63", "48 - 69", "50 - 73",                                                                                                                                         "62 - 86", "76 - 98", "00 - 20", "16 - 38", "18 - 34", "20 - 41",                                                                                                                                  "32 - 54", "41 - 63", "48 - 69", "50 - 73", "62 - 86", "76 - 98"                                                                                 ), n = c(19318, 19050, 13948, 17416, 13893, 12495, 11038, 10813,                                                                                      4751, 612, -19915, -15443, -11091, -13599, -11969, -11525, -10442,                                                                                              -10119, -4124, -367)), row.names = c(NA, -20L), class = c("tbl_df",                                                                                                                                                   "tbl", "data.frame"))
#And here is the plot

AgeGroupDat %>% 
  ggplot(aes(x = Age_Group, y = n, fill = sex))+
  geom_bar(stat = "identity")+
  geom_text(data = subset(AgeGroupDat,
                          sex == "Female"),
            aes(label = abs(n)),
            hjust = -0.3,
            size = 3,
            #position = position_stack(vjust = -.01)
  )+ 
  geom_text(data = subset(AgeGroupDat,
                          sex == "Male"),
            aes(label = abs(n)),
            hjust = 1.2,
            size = 3,
            #position = position_stack()
  )+
  coord_flip()+
  facet_share(~sex, dir = "h", scales = "free", reverse_num = TRUE)+
  theme_classic()+
  scale_fill_manual(values = c("#9a0138","#000775"))+
    labs(title = "test")+
  labs(subtitle = "test")+
  labs(caption = "test")+
  labs(y = NULL)+
  labs(x = NULL)+
  labs(fill = "Member Sex")+
  theme(legend.position = "none")+
  ylim(-25000, 25000)

Solution

  • One option to achieve your desired result would be to switch to the ggh4x package which via facetted_pos_scales allows to set the scales individually for each panel. Doing so however requires to manually remove the axis text for one of the y scales. Additionally, for nicely centered labels we have to remove the panel spacing via panel.spacing.x = unit(0, "pt") and set the same margin for the y axis text.

    library(ggplot2)
    library(ggh4x)
    
    ggplot(AgeGroupDat, aes(y = Age_Group, x = n, fill = sex)) +
      geom_bar(stat = "identity") +
      geom_text(
        aes(label = abs(n), hjust = ifelse(sex == "Female", -.3, 1.3)),
        size = 3
      ) +
      scale_fill_manual(values = c(Female = "#9a0138", Male = "#000775"), drop = FALSE) +
      facet_wrap(~sex, scales = "free") +
      facetted_pos_scales(
        x = list(
          scale_x_continuous(
            labels = abs, expand = c(0, 2500, 0, 0),
            limits = c(-20000, 0)
          ),
          scale_x_continuous(
            expand = c(0, 0, 0, 2500),
            limits = c(0, 20000)
          )
        ),
        y = list(
          scale_y_discrete(position = "right"),
          scale_y_discrete(labels = NULL)
        )
      ) +
      labs(
        x = NULL, y = NULL, fill = "Member Sex",
        title = "test",
        subtitle = "test",
        caption = "test"
      ) +
      theme_classic() +
      theme(
        axis.text.y.right = element_text(margin = margin(0, 2.2, 0, 2.2)),
        legend.position = "bottom",
        panel.spacing.x = unit(0, "pt")
      )
    

    enter image description here