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")
給每個點加上對應(yīng)的注釋
p+geom_text(data=iris,mapping=aes(label=paste(iris$Sepal.Width)),vjust=1.5,colour="black",size=3)
根據(jù)坐標添加文本
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")
根據(jù)坐標添加數(shù)學(xué)表達式
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
查看數(shù)學(xué)表達式書寫方法
篩選數(shù)據(jù)摆寄,標記符合要求的數(shù)據(jù)
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")
)