Skip to content

Instantly share code, notes, and snippets.

@jkaupp
Created April 9, 2018 23:17
Show Gist options
  • Save jkaupp/7600bc43873fa8887d3bb75104bddfd6 to your computer and use it in GitHub Desktop.
Save jkaupp/7600bc43873fa8887d3bb75104bddfd6 to your computer and use it in GitHub Desktop.
One method for adding nicely aligned labels to line graphs in ggplot2
library(tidyverse)
library(nycflights13)
plot_data <- flights %>%
filter(carrier %in% c("AA","UA")) %>%
count(year, month, carrier)
ggplot(plot_data, aes(x = month, y = n, group = carrier)) +
geom_path() +
geom_text(data = filter(plot_data, month == 12), aes(label = carrier), nudge_x = 0.5) +
scale_x_continuous(limits = c(1,13), breaks = 1:12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment