2017-12-26 15 views
4

ggplot2로 히트 맵을 그립니다. y 축에 여러 눈금 표시가 필요합니다. 그러나 그 중 일부는 너무 가깝고 겹칩니다. ggrepel이 텍스트 레이블을 분리 할 수 ​​있다는 것을 알고 있지만 현재 내 문제에 대해서는 해결하지 못했습니다.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 

enter image description here

답변

4

, 나는 별도의 음모로 축을 그릴 후 결합하는 것을 선호합니다. 그것은 약간의 부끄러움을 필요로하지만 원하는 축을 거의 그릴 수 있습니다.

제 솔루션에서 나는 cowplot 패키지의 get_legend(), align_plots()plot_grid() 기능을 사용하고 있습니다. 면책 조항 : 저는 패키지 저자입니다. 원래 data.frame이 row.names 및 col.names이있는 경우

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)) 

enter image description here

+0

, 하나 같이 라벨 = NULL을 추가해야합니다 : =을 C (0 확장 (scale_x_discrete, 0) = TRUE, 라벨 드롭 = NULL) + scale_y_discrete (확장 = c (0, 0), drop = TRUE, labels = NULL). – PengY