在 R 中的 ggplot 中清楚地定义热图中的每个标签

Define each label in heatmap clearly in ggplot in R

我有以下数据框:

ID  position    hum_chr_pos CHROM   a1  a2  a3  a4  ID_rn
rs1 197_V   897738  1   0.343442666 0.074361225 1   0.028854932 1
rs3 1582_N  2114271 2   0.015863115 1   0.003432604 0.840242328 2
rs6 2266_I  79522907    3   0.177445544 0.090282782 1   0.038199399 3
rs8 521_D   86959173    4   0.542804846 0.088721027 1   0.047758851 4
rs98    1368_G  92252015    5   0.02861059  0.979995611 0.007545923 1   5
rs23    540_A   96162102    5   0.343781806 0.062643599 1   0.024992095 6
rs43    2358_S  147351955   6   0.042592955 0.862087128 0.013001476 1   7
rs65    577_E   168572720   6   0.517111734 0.080471431 1   0.034521778 8
rs602   1932_T  169483561   6   0.043270585 1   0.009731403 0.988762282 9
rs601   1932_T  169511878   6   0.042963813 0.911392392 0.010562154 1   10
rs603   1932_T  169513583   6   0.04096538  0.956129216 0.010983517 1   11
rs606   1936_T  169513573   7   0.04838 0.0126129216    0.090983517 1   12
rs609   1935_T  169513574   7   0.056 0.045    0.086 1   13

我用值 a1, a2, a3, a4:

创建了一个热图

为此,我使用了这段代码:

df_melt <- melt(dummy, id.vars=c("ID", "position","hum_chr_pos","CHROM","ID_rn"))

pos <- df_melt %>% 
  group_by(CHROM) %>% 
  summarize(avg = round(mean(ID_rn))) %>% 
  pull(avg)

ggplot(df_melt, aes(x=variable, y=ID_rn)) + geom_tile(aes(fill=value))+theme_bw()+
  scale_fill_gradient2(low="lightblue", mid="white", high="darkblue", midpoint=0.5, limits=range(df_melt$value))+
  theme_classic()+ labs(title="graph", x= "a", fill = "value")+ 
  ylab("CHROM") +
  scale_y_discrete(limits = pos,labels = unique(limits = pos,df_melt$CHROM))

我想找到一种方法来更清楚地看到每个因素在 y 轴上的分离。目前还不清楚哪一行属于 y 轴上的哪个标签。所以我想要这样的东西:

同样奇怪的是,数字有时并不真正位于每个因素的中间。例如,y 轴上的 5 和 7 没有居中。 但我一直在寻找如何做到这一点,但找不到任何东西。

您可以使用 geom_hline

ggplot(df_melt, aes(x = variable, y = ID_rn)) +
  geom_tile(aes(fill = value)) +
  theme_bw() +
  scale_fill_gradient2(low = "lightblue", mid = "white", high = "darkblue",
                       midpoint = 0.5, limits = range(df_melt$value)) +
  theme_classic() + 
  labs(title="graph", x= "a", fill = "value", y = "CHROM") +
  scale_y_discrete(limits = c(1, 2, 3, 4, 5.5, 9, 12.5),
                   labels = unique(df_melt$CHROM)) +
  geom_hline(yintercept =  c(1, 2, 3, 4, 6, 11, 13) + 0.5, color = 'red')