Kimberly Peter
Kimberly Peter

Reputation: 103

Adding labels to the bottom of a bar plot

enter image description here

I have added the n= labels to the following graph using the code

  geom_text(angle = 0, nudge_y = -0.02, col = "#d3d3d3")

I would like to have the n= labels located at the bottom of the bars so that they don't interfere with the error bars. When I change the

 nudge_y = 

it moves all the labels down the same amount. How do I get the labels to align together at the bottom of the bars?

Upvotes: 1

Views: 2987

Answers (1)

stefan
stefan

Reputation: 123758

When using geom_col to draw the bars one can put the labels at the bottom by setting the y-aesthetic in geom_text to (near) zero (+ optional nudging). Try this with example data mtcars:

library(dplyr)
library(ggplot2)
mtcars %>% 
  # Add count
  count(cyl, gear) %>% 
  # Add label
  mutate(label = paste("n =", n)) %>% 
  # Plot
  ggplot(aes(x = factor(cyl), y = n, fill = factor(cyl))) +
  geom_col() +
  geom_text(aes(y = 0, label = label), vjust = 0, nudge_y = .2) + 
  facet_wrap(~gear, scales = "free_x")

Created on 2020-03-10 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions