Home > OS >  Define each label in heatmap clearly in ggplot in R
Define each label in heatmap clearly in ggplot in R

Time:04-09

I have following data frame:

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

I created a heatmap with the values a1, a2, a3, a4:

enter image description here For this I used this code:

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

I would like to find a way to see more clearly the separation of each factor on the y axis. At the moment it is not really clear which row belong to which label on the y axis. So I would like to have something like that:

enter image description here

Also it is weird, that the numbers are sometimes not really in the middle of each factor. For example, the 5 and 7 on the y axis are not centered. But I have searching how to do this, but couldn't find anything.

CodePudding user response:

You could use 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')

enter image description here

  • Related