ggpot2 散点图添加注释

作者: JeremyL | 来源:发表于2018-10-17 12:25 被阅读36次
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")
  )

相关文章

网友评论

    本文标题:ggpot2 散点图添加注释

    本文链接:https://www.haomeiwen.com/subject/ryyeoftx.html