Lable specific points of geom_jitter() by using ggplot2

霸气de小男生 提交于 2020-01-07 03:50:51

问题


when using this code:

t1 <- ggplot(mtcars, aes(x=as.factor(cyl),y=mpg))
t2 <- geom_boxplot(outlier.shape=NA)
t3 <- geom_jitter(width=0.3, size=1.5, aes(color = disp))
t4 <- scale_colour_gradient(low="blue",high="red")
t5 <- geom_text(aes(label=ifelse(mpg > 30,as.character(mpg),'')))

t1 + t2 + t3 + t4 + t5

I get a boxplot in combination with jitter points. I am also able to label the points of interest, however: the labeling is not next to the specific point, but rather in the vertical middle of the boxplot.

Here is the figure

Any idea how I can place the text next to the corresponding point?

Thank you a lot guys!

By the way: can you recommend me a course or a tutorial for ggplot2 beginners?


回答1:


Jitter it beforehand?

library(ggplot2)
set.seed(1)
t1 <- ggplot(transform(mtcars, xjit=jitter(as.numeric(as.factor(cyl)))), aes(x=as.factor(cyl),y=mpg))
t2 <- geom_boxplot(outlier.shape=NA)
t3 <- geom_point(size=1.5, aes(x=xjit, color = disp))
t4 <- scale_colour_gradient(low="blue",high="red")
t5 <- geom_text(aes(x=xjit, label=ifelse(mpg > 30,as.character(mpg),'')), vjust = 1)

t1 + t2 + t3 + t4 + t5



来源:https://stackoverflow.com/questions/36882882/lable-specific-points-of-geom-jitter-by-using-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!