美文网首页
ggplot文字标签设置之ggrepel包

ggplot文字标签设置之ggrepel包

作者: 夕颜00 | 来源:发表于2022-11-23 17:45 被阅读0次

    在很多作图中,我们都涉及过添加标签,例如火山图,一般都是用ggrepel中的geom_text_repel函数。
    构建一个散点图。

    
    library(ggplot2)
    library(ggrepel)
    setwd("C:/Users/tq199/Desktop")
    A <- read.csv("A.csv", header = T)
    
    ggplot(A, aes(x=rank, y=avg_log2FC)) + 
      geom_point(size=3, color='#DC050C')+
      theme_bw()+
      theme(panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),
            axis.title = element_text(colour = 'black', size = 15),
            axis.text = element_text(colour = 'black', size = 12))+
      labs(x='Rank', y='Log2FC')
    
    image.png

    需要标签的点整理一个单独的文件,直接使用geom_text_repel,设置下箭头、大小、字体等等。但是我们会发现一个问题,那就是有些标签不会显示(显示不全),这是因为太多导致重复了。

    B <- read.csv("B.csv", header = T)
    
    ggplot(A, aes(x=rank, y=avg_log2FC)) + 
      geom_point(size=3, color='#DC050C')+
      geom_point(data = A[abs(A$avg_log2FC)<0.5,], 
                 stroke = 0.5, size=3, shape=16, color="grey50",alpha =0.4)+
      theme_bw()+
      theme(panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),
            axis.title = element_text(colour = 'black', size = 15),
            axis.text = element_text(colour = 'black', size = 12))+
      labs(x='Rank', y='Log2FC')+
      geom_text_repel(data=B, aes(label=X), color="black", size=4, fontface="italic", 
                      arrow = arrow(ends="first", length = unit(0.01, "npc")), box.padding = 0.2,
                      point.padding = 0.3, segment.color = 'black', segment.size = 0.3, force = 1, max.iter = 3e3)
    
    image.png

    在很多文章中,我们发现别人的标签排列很整齐,这里只需要设置两个小参数即可。第一direction设置y轴对齐,nudge_x设置指示线长一点即可。

    
    B1 <- B[B$avg_log2FC>0,]
    B2 <- B[B$avg_log2FC<0,]
    
    
    ggplot(A, aes(x=rank, y=avg_log2FC)) + 
      geom_point(size=3, color='#DC050C')+
      geom_point(data = A[abs(A$avg_log2FC)<0.5,], 
                 stroke = 0.5, size=3, shape=16, color="grey50",alpha =0.4)+
      theme_bw()+
      theme(panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),
            axis.title = element_text(colour = 'black', size = 15),
            axis.text = element_text(colour = 'black', size = 12))+
      labs(x='Rank', y='Log2FC')+
      geom_text_repel(data=B1, aes(label=X), color="black", size=4, fontface="italic", 
                      size=3, segment.size=0.5, nudge_x=500, direction="y", hjust=0)+
      geom_text_repel(data=B2, aes(label=X), color="black", size=4, fontface="italic", 
                     size=3, segment.size=0.5, nudge_x=-1000, direction="y", hjust=0)
    
    
    图片

    最后,如果需要标注的点比较散,也可以使用x对齐。

    
    B3 <- B2[abs(B2$avg_log2FC)>1.2,]
    ggplot(A, aes(x=rank, y=avg_log2FC)) + 
      geom_point(size=3, color='#DC050C')+
      geom_point(data = A[abs(A$avg_log2FC)<0.5,], 
                 stroke = 0.5, size=3, shape=16, color="grey50",alpha =0.4)+
      theme_bw()+
      theme(panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),
            axis.title = element_text(colour = 'black', size = 15),
            axis.text = element_text(colour = 'black', size = 12))+
      labs(x='Rank', y='Log2FC')+
      geom_text_repel(data=B3, aes(label=X), color="black", size=4, fontface="italic", 
                      size=3, segment.size=0.5, nudge_x=-1000, direction="x", hjust=0)
    

    参数

    大部分geom_text()的参数都适用于geom_text_repel(),除了以下几个:

    hjust
    vjust
    position
    check_overlap
    

    ggrepel包为geom_text_repel()与geom_label_repel()提供了特有的参数设置:

    segment.color:连接点与标签的线段的颜色
    segment.size:线段的粗细
    segment.alpha:线段的透明度
    box.padding:文本框周边填充
    point.padding:点周围填充
    arrow:grid:arrow提供的箭头
    force:强制性将重叠文本散开
    max.oter:最大迭代次数
    nudge_x/y:标签开始位置在坐标轴的移动距离
    direction:允许标签的方向,x、y or both
    

    下面举个栗子来详细了解这些参数的图形效果

    set.seed(42)
    ggplot(mtcars)+ geom_point(aes(wt, mpg, color=factor(cyl)), size=3)+
    geom_text_repel(aes(wt, mpg, color=factor(cyl),
    label=rownames(mtcars), angle=ifelse(mtcars$cyl==4, 90, 0)),
    size=4, family="Times", fontface="bold",
    box.padding=unit(0.5, "lines"), point.padding=unit(1.6, "lines"),
    segment.color = "#cccccc", segment.size = 0.5,
    arrow = arrow(length=unit(0.01, "npc")),force = 1, max.iter = 3e3,
    nudge_x = ifelse(mtcars$cyl==6, 2, 0), nudge_y = ifelse(mtcars$cyl==6, 9, 0))+
    scale_color_discrete(name="cyl")+
    scale_x_continuous(expand = c(0.5, 0))+
    scale_y_continuous(expand = c(0.25, 0))+
    theme_classic(base_size = 16)
    
    image.png

    参考
    ggplot文字标签设置(geom_text_repel函数)的一个小例子
    R语言可视化学习笔记之ggrepel包

    相关文章

      网友评论

          本文标题:ggplot文字标签设置之ggrepel包

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