问题
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