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

使用ggplot2标记geom_jitter()的特定点,r,ggplot2,jitter,R,Ggplot2,Jitter,使用此代码时: 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=ifels

使用此代码时:

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
t1事先抖动它

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
库(ggplot2)
种子(1)

t1这个主题必须给你一些提示:我有一个关于你选择情节的问题。如果x轴是一个因子,只有4、6和8是唯一的值,那么应该如何读取点的水平抖动?只是好奇!谢谢你,卢卡。因为我仍然认为自己是初学者,我必须问你一些代码的变化。您能告诉我为什么在t1行中使用transform,以及transform对其余代码的作用是什么吗?我也不明白xjit是什么,以及t3行color=disp中的内容(disp是什么)?抱歉问了这么多问题……当然。我使用
transform
动态创建一个新列
xjit
,即仅用于绘图,而不在R环境中的某个位置存储另一个变量
xjit
是jit的x值
color=disp
来自您的示例-它将列
disp
映射到颜色美学。