ggplot2 添加文字说明geom_text—科研工具箱

请关注公众号【叨客学习资料】 在使用网站的过程中有疑问,请来公众号进行反馈哦

加载数据,绘制散点图:

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—科研工具箱-叨客学习资料网
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—科研工具箱-叨客学习资料网

重新定义颜色:

p + scale_color_manual(values = c(\"red\", \"blue\", \"green\"))
图片[3]-ggplot2 添加文字说明geom_text—科研工具箱-叨客学习资料网
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
请输入有效评论哦,肆意灌水或者乱打评论是不会通过的,会影响您评论后获得资源哦~~
提交
头像

昵称

取消
昵称表情

    暂无评论内容