Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

r - Aligning geom_text to geom_jitter points

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...