【问题标题】:Is it possible to avoid axis label overlapping by ggrepel?是否可以通过ggrepel避免轴标签重叠?
【发布时间】:2018-06-07 08:37:18
【问题描述】:

我正在用 ggplot2 绘制热图。 y 轴上的几个刻度需要标记。然而,它们中的一些太接近并且重叠。我知道 ggrepel 可以分隔文本标签,但目前我还没有解决我的问题。

我的代码如下。欢迎任何建议。谢谢。

代码:

df <- data.frame()

for (i in 1:50){
  tmp_df <- data.frame(cell=paste0("cell", i), 
                       gene=paste0("gene", 1:100), exp = rnorm(100), ident = i %% 5)
  df<-rbind(df, tmp_df)
}

labelRow=rep("", 100)
for (i in c(2, 5, 7, 11, 19, 23)){
  labelRow[i] <- paste0("gene", i)
}

library(ggplot2)
heatmap <- ggplot(data = df, mapping = aes(x = cell, y = gene, fill = exp)) +
  geom_tile() + 
  scale_fill_gradient2(name = "Expression") + 
  scale_y_discrete(position = "right", labels = labelRow) +
  facet_grid(facets = ~ident,
             drop = TRUE,
             space = "free",
             scales = "free", switch = "x") +
  scale_x_discrete(expand = c(0, 0), drop = TRUE) +
  theme(axis.line = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_text(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        strip.text.x = element_text(angle = -90))

heatmap

【问题讨论】:

    标签: r ggplot2 facet cowplot ggrepel


    【解决方案1】:

    对于这类问题,我更喜欢将轴绘制为单独的图,然后合并。这需要一些摆弄,但可以让您绘制几乎任何您想要的轴。

    在我的解决方案中,我使用了来自 cowplot 包的函数 get_legend()align_plots()plot_grid()。免责声明:我是包作者。

    library(ggplot2)
    library(cowplot); theme_set(theme_gray()) # undo cowplot theme setting
    library(ggrepel)
    
    df<-data.frame()
    for (i in 1:50){
      tmp_df <- data.frame(cell=paste0("cell", i), 
                           gene=paste0("gene", 1:100), exp=rnorm(100), ident=i%%5)
      df<-rbind(df, tmp_df)
    }
    
    
    labelRow <- rep("", 100)
    genes <- c(2, 5, 7, 11, 19, 23)
    labelRow[genes] <- paste0("gene ", genes)
    
    # make the heatmap plot
    heatmap <- ggplot(data = df, mapping = aes(x = cell,y = gene, fill = exp)) +
      geom_tile() + 
      scale_fill_gradient2(name = "Expression") + 
      scale_x_discrete(expand = c(0, 0), drop = TRUE) + 
      facet_grid(facets = ~ident,
                 drop = TRUE,
                 space = "free",
                 scales = "free", switch = "x") + 
      theme(axis.line = element_blank(),
            axis.title = element_blank(),
            axis.text = element_blank(),
            axis.ticks = element_blank(),
            strip.text.x = element_text(angle = -90),
            legend.justification = "left",
            plot.margin = margin(5.5, 0, 5.5, 5.5, "pt"))
    
    # make the axis plot
    axis <- ggplot(data.frame(y = 1:100,
                              gene = labelRow),
                   aes(x = 0, y = y, label = gene)) +
      geom_text_repel(min.segment.length = grid::unit(0, "pt"),
                     color = "grey30",  ## ggplot2 theme_grey() axis text
                     size = 0.8*11/.pt  ## ggplot2 theme_grey() axis text
                     ) +
      scale_x_continuous(limits = c(0, 1), expand = c(0, 0),
                         breaks = NULL, labels = NULL, name = NULL) +
      scale_y_continuous(limits = c(0.5, 100.5), expand = c(0, 0),
                         breaks = NULL, labels = NULL, name = NULL) +
      theme(panel.background = element_blank(),
            plot.margin = margin(0, 0, 0, 0, "pt"))
    
    # align and combine
    aligned <- align_plots(heatmap + theme(legend.position = "none"), axis, align = "h", axis = "tb")
    aligned <- append(aligned, list(get_legend(heatmap)))
    plot_grid(plotlist = aligned, nrow = 1, rel_widths = c(5, .5, .7))
    

    【讨论】:

    • 如果您的原始 data.frame 有 row.names 和 col.names,则应添加标签=NULL,例如: scale_x_discrete(expand = c(0, 0), drop = TRUE,labels=NULL) +scale_y_discrete(expand = c(0, 0), drop = TRUE,labels=NULL) 以避免显示不需要的标签。
    • 克劳斯,谢谢你的建议!我稍微修改了您的代码,以便它可以在热图的左侧工作。即使我可以对齐标签,轴和热图之间也有很大的余量。你知道这是为什么吗?我将热图和轴的所有边的边距都设置为0,所以我不知道为什么它们之间还有一个空间。你有什么建议吗?
    猜你喜欢
    • 2022-07-25
    • 2011-10-10
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 2019-08-05
    • 1970-01-01
    • 2020-07-06
    相关资源
    最近更新 更多