library(ggplot2)
p<-ggplot(iris,aes(iris$Petal.Length, iris$Sepal.Width))
p<-p+geom_point(aes(color= iris$Species ))+scale_color_manual(values = c("red", "grey","blue"))+theme(legend.position = "right")
给每个点加上对应的注释
p+geom_text(data=iris,mapping=aes(label=paste(iris$Sepal.Width)),vjust=1.5,colour="black",size=3)
根据坐标添加文本
p+annotate("text", x=iris$Petal.Length[1], y=iris$Sepal.Width[1], label=iris$Species[1],vjust=1.5,colour="green", size=6)+
annotate("rect", xmin=1.25, xmax=1.55, ymin=3.41, ymax=3.49, alpha=.1,color="blue")
根据坐标添加数学表达式
p + annotate("text",x=5.5,y=4, parse=TRUE,label="x %->% y",size=5)+
annotate("rect", xmin=5.3, xmax=5.7, ymin=3.9, ymax=4.1, alpha=.1,color="blue")
?plotmath
查看数学表达式书写方法
筛选数据,标记符合要求的数据
library(ggrepel)
ggplot(iris, aes(x = Petal.Length, y = Sepal.Width)) +
geom_point(aes(color = Species)) +
scale_color_manual(values = c("red", "grey","black")) +
theme_bw(base_size = 12) + theme(legend.position = "bottom") +
geom_text_repel(
data = subset(iris, Species=="setosa"),
aes(label = Sepal.Length),
size = 3,
box.padding = unit(0.35, "lines"),
point.padding = unit(0.3, "lines")
)