Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color only of the upper triangle matrix of a heat map in ggplot2

I have this melted matrix that I want to plot as a heatmap, with correlation coefficient in the lower matrix and P-values in the upper triangle

> melted_corfinal
     Var1   Var2 value
1     iHS    iHS  1.00
2     nSL    iHS  0.89
3  XP-EHH    iHS  0.01
4     PBS    iHS  0.00
5     iHS    nSL  0.00
6     nSL    nSL  1.00
7  XP-EHH    nSL  0.01
8     PBS    nSL  0.00
9     iHS XP-EHH  0.00
10    nSL XP-EHH  0.00
11 XP-EHH XP-EHH  1.00
12    PBS XP-EHH  0.18
13    iHS    PBS  0.90
14    nSL    PBS  0.41
15 XP-EHH    PBS  0.00
16    PBS    PBS  1.00

However, I could not find a way to just change the color of only the upper triangle matrix, while keeping the values. I want it to be just white (background).

Here's the code that I've come up so far:

p <- ggplot(melted_corfinal, aes(Var2, Var1)) +
  geom_tile(aes(fill = value)) + 
  geom_text(aes(label = round(value, 2))) +
  scale_fill_continuous("",limits=c(0, 1), breaks=seq(0,1,by=0.2),low = "#fee8c8", high = "#e34a33") + 
  theme_light() + theme(legend.position="none",axis.title.x = element_blank(),axis.title.y = element_blank()) +
  guides(fill = guide_colorbar(barwidth = 20)) +
  ylim(rev(levels(melted_corfinal$Var1))) + xlim(levels(melted_corfinal$Var2))

plot(p) 

Also, I still want the 2 decimal places to be present in the table, but they are "rounded" when they are zero. dput:

structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("iHS", "nSL", 
"XP-EHH", "PBS"), class = "factor"), Var2 = structure(c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("iHS", 
"nSL", "XP-EHH", "PBS"), class = "factor"), value = c(1, 0.89, 
0.01, 0, 0, 1, 0.01, 0, 0, 0, 1, 0.18, 0.9, 0.41, 0, 1)), .Names = c("Var1", 
"Var2", "value"), row.names = c(NA, -16L), class = "data.frame")
like image 715
GabrielMontenegro Avatar asked Dec 13 '25 13:12

GabrielMontenegro


1 Answers

Ok first I convert back to regular unmelted matrix the data you provided so I can easily set to NA the upper triangle. I do this with dcast. This matrix is already a combination of correlation on diagonal and lower triangle, and p-values on upper triangle.

melted_corfinal <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), 
                                                   .Label = c("iHS", "nSL", "XP-EHH", "PBS"), class = "factor"), 
                                  Var2 = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), 
                                                   .Label = c("iHS", "nSL", "XP-EHH", "PBS"), class = "factor"), 
                                  value = c(1, 0.89, 0.01, 0, 0, 1, 0.01, 0, 0, 0, 1, 0.18, 0.9, 0.41, 0, 1)), 
                             .Names = c("Var1", "Var2", "value"), row.names = c(NA, -16L), class = "data.frame")

cor_pval <- dcast(melted_corfinal, Var1~Var2)[, -1]
# Set to NA upper triangle excluding diagonal
cor_pval[upper.tri(cor_pval, diag=F)] <- NA 

I then melt this and add as second value to melted_corfinal

cor_pval_col <- melt(cor_pval)
melted_corfinal$value2 <- cor_pval_col$value
melted_corfinal

Now we plot as you have, but for geom_tile we use value2 with NA for p-values. We then set na.value="white" in scale_fill_continuous.

And finally to get 2 signif digits for 0's I use format

p <- ggplot(melted_corfinal, aes(Var2, Var1)) +
  geom_tile(aes(fill = value2)) +
  scale_fill_continuous("",limits=c(0, 1), breaks=seq(0,1,by=0.2), low = "#fee8c8", high = "#e34a33", na.value = "white") + 
  geom_text(aes(label = format(value, nsmall=2))) +
  theme_light() + theme(legend.position="none",axis.title.x = element_blank(),axis.title.y = element_blank()) +
  guides(fill = guide_colorbar(barwidth = 20)) +
  ylim(rev(levels(melted_corfinal$Var1))) + xlim(levels(melted_corfinal$Var2))
p

enter image description here

like image 113
Djork Avatar answered Dec 15 '25 02:12

Djork