'Aligning a geom_text layer vertically on a bar chart

I currently have the geom_text set to the center of the bar chart...

enter image description here

library(dplyr)
library(ggplot2)

enroll_bar <- enroll_cohort %>%
    filter(chrt_grad != 2013) %>%
    mutate(college_enrolled = factor(college_enrolled),
           chrt_grad = factor(chrt_grad)) %>%
    mutate(label_height = cumsum(n)) %>%
    ggplot() +
    geom_col(mapping = aes(x = chrt_grad,
                           y = n,
                           fill = fct_rev(college_enrolled))) +
    geom_text(aes(x = chrt_grad, y = label_height, label = n), color = "white",
              position = position_stack(vjust = 0.5)) +
    scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
    labs(x = NULL, y = NULL) +
    scale_fill_manual(labels = c("Enrolled", "Not Enrolled"),
                      values = c("#00aeff", "#005488"))


How do I align the text vertically, so the geom_text layer is set at the same y-axis position for each bar? Also, is there a way to do this so the text is always aligned no matter the scale of the y-axis because this is a parameterized report and the y-axis values change with each report?



Solution 1:[1]

If you want the labels to be at a uniform height, you can designate a y in geom_text() outside of aes with the height you're looking for.

I used the dataset diamonds. The columns are too non-uniform for the white text to be visible, so I changed the top labels to black.

Because the bottom group's lowest value is really low, I set it to 100. The top group is set to 5000. Essentially, I created a vector -- 100, 5000, 100, 5000, 100... and so on, using rep() (repeat).

The data is set up differently since you didn't include that in your question. However, that shouldn't matter.

library(tidyverse)

diamonds %>% 
  filter(color %in% c("E", "I")) %>% 
  ggplot(aes(x = cut,
             fill = color)) + 
  geom_bar() +
  geom_text(aes(label = ..count..),  # use the count
            y = rep(c(100, 5000),    # btm label y = 100, top label y = 5K
                    times = 5),
            stat = 'count',
            color = rep(c("white", "black"), # btm label white, top black
                        times = 5),
            position = position_stack(vjust = .5)) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
  labs(x = NULL, y = NULL) +
  scale_fill_manual(labels = c("E", "I"),
                    values = c("#00aeff", "#005488")) +
  theme_bw()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1