Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning geom_text to geom_jitter points

Tags:

r

ggplot2

How can I align (along the x axis dimension) the text labels with the jittered points in the following plot in R ggplot2?

library(dplyr)
library(ggplot2)
mtcars %>% 
    ggplot(aes(am, wt, group = am, label = wt)) +
    geom_boxplot(outlier.shape = NA) +
    geom_jitter() +
    geom_text()

enter image description here

like image 981
David Rubinger Avatar asked Sep 13 '18 15:09

David Rubinger


1 Answers

Easy solution would be to specify position_jitter in both geom_text and geom_jitter with the same seed.

library(ggplot2)
ggplot(mtcars, aes(am, wt, group = am, label = wt)) +
    geom_boxplot(outlier.shape = NA) +
    geom_jitter(position = position_jitter(seed = 1)) +
    geom_text(position = position_jitter(seed = 1))

enter image description here

like image 77
pogibas Avatar answered Sep 30 '22 23:09

pogibas