简体   繁体   English

使用ggplot2标记geom_jitter()的特定点

[英]Lable specific points of geom_jitter() by using ggplot2

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? 顺便说一句:您能为我推荐ggplot2初学者的课程或教程吗?

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM