Home > Software design >  Plot only upper triangle of the matrix using ggplot2
Plot only upper triangle of the matrix using ggplot2

Time:09-20

I have a matrix that I already cut into the upper triangle:

eSim <- c(1,1,-1,-1,1,1,-1,-1,-1,-1,1,1,-1,-1,1,1)
dim(eSim) <- c(4,4)
rownames(eSim)=c("PosLong\n1200", "PosLong\n1800", "Neglong\n1200", "Neglong\n1800")
colnames(eSim)=c("PosLong\n1200", "PosLong\n1800", "Neglong\n1200", "Neglong\n1800")

# Get upper triangle of the correlation matrix
  get_upper_tri <- function(cormat){
    cormat[lower.tri(cormat)]<- NA
    return(cormat)
  }
eSim_upper <- get_upper_tri(eSim)
melted_eSim_upper <- melt(eSim_upper, na.rm = TRUE)

And this is my code for plotting this matrix

ggplot(data = melted_eSim_upper, aes(Var2, Var1, fill = as.factor(value))) 
 geom_tile(color = "black", size=0.8) 
 scale_fill_manual(values=c("#8DD3C7", "#FB8072")) 
 theme_minimal()  
 theme(axis.text.x = element_text(angle = 45, vjust = 1, 
    size = 12, hjust = 1)) 
  theme(axis.text.y = element_text(angle = 45, vjust = 1, 
    size = 12, hjust = 1)) 
 coord_fixed() 
 theme(axis.title.x = element_blank(),
  axis.title.y = element_blank()) 
  theme(panel.background = element_blank(), 
  panel.grid.major = element_blank(),
  panel.border = element_blank()) 
  theme(axis.text.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = 50)))

The outcome:

enter image description here

I have several questions to acheive my final goal:

  1. Is there a way to rotate this plot diagnoally? Something like this: enter image description here
  2. Is there a way to move the y axis text further away to the matrix?

CodePudding user response:

  1. Yes, use limits = rev in a discrete scale.
  2. Yes, but the more convenient thing to do is to use guide = guide_axis(angle = 45) instead of micromanaging the theme settings.
library(ggplot2)

eSim <- c(1,1,-1,-1,1,1,-1,-1,-1,-1,1,1,-1,-1,1,1)
dim(eSim) <- c(4,4)
rownames(eSim)=c("PosLong\n1200", "PosLong\n1800", "Neglong\n1200", "Neglong\n1800")
colnames(eSim)=c("PosLong\n1200", "PosLong\n1800", "Neglong\n1200", "Neglong\n1800")

# Get upper triangle of the correlation matrix
get_upper_tri <- function(cormat){
  cormat[lower.tri(cormat)]<- NA
  return(cormat)
}
eSim_upper <- get_upper_tri(eSim)
melted_eSim_upper <- reshape2::melt(eSim_upper, na.rm = TRUE)

ggplot(data = melted_eSim_upper, aes(Var2, Var1, fill = as.factor(value))) 
  geom_tile(color = "black", size=0.8) 
  scale_fill_manual(values=c("#8DD3C7", "#FB8072")) 
  scale_y_discrete(limits = rev, guide = guide_axis(angle = 45))  
  theme_minimal()  
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 12, hjust = 1)) 
  theme(axis.text.y = element_text(size = 12)) 
  coord_fixed() 
  labs(x = NULL, y = NULL)  
  theme(panel.background = element_blank(), 
        panel.grid.major = element_blank(),
        panel.border = element_blank())

Created on 2022-09-19 by the reprex package (v2.0.1)

  • Related