美文网首页
ggplot中文字标注

ggplot中文字标注

作者: 风口的猪都会飞 | 来源:发表于2018-08-29 09:06 被阅读0次

    geom_text() 参数有:
    size 设置字体大小
    angle 设置倾斜角度,逆时针旋转角度
    label 标记的文字
    family 设置字体,["sans","serif","mono"],默认为sans字体
    vjust ["bottom","middle","top","inward(文字对齐到主画面)","outward"]
    hjust ["left","center","right","inward(文字对齐到主画面)","outward"]
    nudge 设置文字距原坐标点的距离,在散点和文字同时存在时很有必要,可以作为散点的标注,如果不添加该参数,点和文字就会重合。nudge_y=-0.25为y轴负方向下移0.25
    check_overlap 查找重复值,当注释中有大量重复时,设置check_overlap=TRUE可以自动删除重复标签
    geom_label() 在文字后方绘制一个圆角矩形标签,当需要在复杂的背景上标注文字时可以使用。

    library(ggplot2)
    USArrests_plot=USArrests
    USArrests_plot$Place=rownames(USArrests)
    p=ggplot(data = USArrests_plot,aes(x=Murder,y=Assault))
    #将州名标注在图像中
    p+geom_text(label=USArrests_plot$Place)
    #设置字体大小、旋转角度、字体格式
    p+geom_text(label=USArrests_plot$Place,size=3,angle=0,family="mono")
    #散点图文字中每个点都进行文字标注
    p+geom_point()+geom_text(label=USArrests_plot$Place,vjust="inward",hjust="inward",size=3.5)
    #散点图中写入文字
    text_to_plot=data.frame(x=c(2.5,15),y=c(300,100),col=c("red","blue"),text=c("red","blue"))
    p+geom_point()+geom_text(data=text_to_plot,aes(x=text_to_plot$x,y=text_to_plot$y,color=text_to_plot$col,label=text_to_plot$text))+theme(legend.position = "none")
    
    
    11.png

    相关文章

      网友评论

          本文标题:ggplot中文字标注

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