Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to ggrepel

Introduction to ggrepel

ggrepel is an R package that extends ggplot2 to automatically position text labels without any overlaps. Check it out at: https://github.com/slowkow/ggrepel

Avatar for Kamil Slowikowski

Kamil Slowikowski

July 19, 2018

Other Decks in Programming

Transcript

  1. library(ggplot2) ggplot(mtcars) + aes( x = wt, y = mpg,

    label = rownames(mtcars) ) + geom_point(color = "red") + geom_text() Problem: text placement 2 / 19
  2. library(ggrepel) ggplot(mtcars) + aes( x = wt, y = mpg,

    label = rownames(mtcars) ) + geom_point(color = "red") + geom_text_repel() Solution: ggrepel 3 / 19
  3. ggrepel is not always the best choice Sometimes other plots

    are easier to read Consider other options 11 / 19
  4. Don't label too many points! Or else you will end

    up with @accidental__aRt 12 / 19
  5. library(ggrepel) d <- subset( mtcars, wt > 3 & wt

    < 4 ) # Just label 3 items. d$car <- "" i <- c(2, 3, 16) d$car[i] <- rownames(d)[i] ggplot(d) + aes(wt, mpg, label = car) + geom_point( color = ifelse( d$car != "", "red", "grey50" ) ) + geom_text_repel() Use the empty string "" 13 / 19
  6. Please report bugs github.com/slowkow/ggrepel/issues Contributions are very welcome! We have

    8 contributors so far. ❓ Stackoverflow is the best place to ask questions. 15 / 19
  7. Make a ggplot2 extension! ⭐ Extending ggplot2 by Hadley Wickham

    ⭐ How to make a generic stat in ggplot2 by Elio Campitelli ggplot2 Internals (WOW!) by Brodie Gaslam 17 / 19