Skip to content

Instantly share code, notes, and snippets.

@edavidaja
Created March 6, 2020 04:27
Show Gist options
  • Save edavidaja/cd43defce2c796cacb9d92f3f6003448 to your computer and use it in GitHub Desktop.
Save edavidaja/cd43defce2c796cacb9d92f3f6003448 to your computer and use it in GitHub Desktop.
direct labels with {ggrepel}

{directlabels} lets you attach categorical labels to many plots. for labeling values on plots, use ggrepel:

suppressPackageStartupMessages({
  library(ggplot2)
  library(ggrepel)
  library(dplyr)
})

extrema <- 
  economics %>%
  slice(which.max(date), which.min(date))

economics %>% 
ggplot(aes(date, psavert)) +
  geom_line() +
  geom_text_repel(
    data = extrema,
    aes(date, psavert, label = psavert),
    nudge_y = 1,
    min.segment.length = Inf
  )

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment