Posted on

加载数据,绘制散点图:

library(ggplot2)
mtcars$gear <- as.character(mtcars$gear)
p <- ggplot(mtcars, aes(wt, mpg, color = gear))+
geom_point(size = 2.0)+
theme_bw()
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))


重新定义颜色:

p + scale_color_manual(values = c("red", "blue", "green"))

2 Replies to “ggplot2 添加文字说明geom_text”

发表评论

邮箱地址不会被公开。 必填项已用*标注