0

Consider the following data frames:

> df1
      c1    c2   c3
c1  1.00 -0.56 0.72
c2 -0.56  1.00 0.13
c3  0.72  0.13 1.00
> df2
      c1    c2    c3
c1     0 0, -4  0, 3
c2 0, -4     0 0, -2
c3  0, 3 0, -2     0

df1 stores the correlation values and df2 stores some values which I want to show them together with the correlation values. Actually, I want to create a correlation heatmap matrix like this:

enter image description here

I wrote the following code:

#Lower triangle of a matrix
get_lower_tri<-function(matrix){
  matrix[upper.tri(matrix)] <- NA
  return(matrix)
}

lower_tri <- get_lower_tri(as.matrix(df1))
lower_lagTri <- get_lower_tri(as.matrix(df2))

library(reshape2)
melted_cormat <- melt(lower_tri, na.rm = TRUE)
melted_lagmat <- melt(lower_lagTri, na.rm = TRUE)

#Heatmap
library("forcats")
ggheatmap <- ggplot(data = melted_cormat, aes(Var2, fct_rev(Var1), fill = value)) + 
  geom_tile(color = "white") + xlab("X") + ylab("Y") +
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1,1), space = "Lab", 
                       name="Correlation") +
  theme_minimal()+ 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 12, hjust = 1)) +
  coord_fixed()

ggheatmap + 
  geom_text(data = melted_lagmat, aes(Var2, fct_rev(Var1), label = value), color = "black", size = 4) +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    panel.grid.major = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    axis.ticks = element_blank())

But I get this error:

Error: Discrete value supplied to continuous scale
Patris
  • 167
  • 2
  • 11

0 Answers0