加载数据,绘制散点图:
library(ggplot2)
mtcars$gear <- as.character(mtcars$gear)
p <- ggplot(mtcars, aes(wt, mpg, color = gear))+
geom_point(size = 2.0)+
theme_bw()
![图片[1]-ggplot2 添加文字说明geom_text—科研工具箱-叨客学习资料网](https://cdn.leobba.cn/wp-content/uploads/image-1-3.png~tplv-vsxgrxnt6c-1.image)
nnn <- table(mtcars$gear)
data_text<-data.frame(label=c(paste0(\"n = \",nnn[1]),paste0(\"n = \",nnn[2]),paste0(\"n = \",nnn[3])),
x=c(5,5,5),
y=c(30,29,28),
color=unique(mtcars$gear))
p <- p+geom_text(data=data_text,
mapping=aes(x=x,y=y,label=label,color=color))
![图片[2]-ggplot2 添加文字说明geom_text—科研工具箱-叨客学习资料网](https://cdn.leobba.cn/wp-content/uploads/image-2-2.png~tplv-vsxgrxnt6c-1.image)
重新定义颜色:
p + scale_color_manual(values = c(\"red\", \"blue\", \"green\"))
![图片[3]-ggplot2 添加文字说明geom_text—科研工具箱-叨客学习资料网](https://cdn.leobba.cn/wp-content/uploads/image-3-2.png~tplv-vsxgrxnt6c-1.image)
© 版权声明
THE END
暂无评论内容