Ordering matrix plot using ggplot2

Viewed 154

I am trying to plot a matrix plot using ggplot2. I am using the following code

library(tidyverse)  
library(RColorBrewer)

df %>% 
  mutate(Models = factor(Models, labels = c("NDVI","SR","WBI","NWI-1","NWI-2","NWI-3","NWI-4","1650/2220 nm ratio"))) %>% 
  pivot_longer(-Models) %>% 
  mutate(p.value = cut(value, c(max(value, na.rm = T), 0.05, 0.01, min(value, na.rm = T)),
                       labels = c("NS","< 0.05","< 0.01"))) %>% 
  ggplot(aes(x=Models,y=name, fill=p.value)) +
  theme_bw() +
  geom_tile() + 
  xlab("Parameters") + ylab(" ") + 
  theme(text=element_text(size=18, family="serif"))+
  scale_colour_manual(values = c("#DAA520", "#F5DEB3", "#FFF8DC","#DCDCDC"),
                      aesthetics = c("colour", "fill")) +
  geom_text(aes(label=format(round(value, 2), nsmall = 2)), color="black", size=2)

which returns me the following plot

enter image description here

As you can see from the plot the x-axis labels are ordered according to my order. But I am unable to order y-axis. So, my questions are

  1. How can I order y-axis? and
  2. How to remove the NAs?
  3. Only colour values < 0.05 and < 0.01 and > 0.05, not all.

Data

df = structure(list(Models = c("NDVI", "SR", "WBI", "NWI-1", "NWI-2", 
"NWI-3", "NWI-4", "1650/2220 nm ratio"), NDVI = c(NA, 0.008, 
0.017, 0.58, 0.02, 0.035, 0.067, 0.027), SR = c(NA, NA, 0.203, 
0.542, 0.618, 0.825, 0.007, 0.015), WBI = c(NA, NA, NA, 0.506, 
0.438, 0.086, 0.035, 0.067), `NWI-1` = c(NA, NA, NA, NA, 0.912, 
0.698, 0.868, 0.319), `NWI-2` = c(NA, NA, NA, NA, NA, 0.782, 
0.956, 0.268), `NWI-3` = c(NA, NA, NA, NA, NA, NA, 0.825, 0.166
), `NWI-4` = c(NA, NA, NA, NA, NA, NA, NA, 0.052), `1650/2220.nm.ratio` = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
)), row.names = c(NA, 8L), class = "data.frame")

The output should look like the following enter image description here

1 Answers

Try the following :

Get the data in long format first and then change both Models and column names to factor in different order.

library(tidyverse)

fac_levels <- c("NDVI","SR","WBI","NWI-1","NWI-2","NWI-3","NWI-4","1650/2220 nm ratio")
df %>% 
  pivot_longer(-Models, values_drop_na = TRUE)  %>%
  mutate(Models = factor(Models, levels = fac_levels), 
         name = factor(name, levels = rev(fac_levels)),
         p.value = cut(value, c(max(value, na.rm = T), 0.05, 0.01, min(value, na.rm = T)),
                       labels = c("NS","< 0.05","< 0.01"))) %>%
  ggplot(aes(x=Models,y=name, fill=p.value)) +
  theme_bw() +
  geom_tile() + 
  xlab("Parameters") + ylab(" ") + 
  theme(text=element_text(size=18, family="serif"))+
  scale_colour_manual(values = c("#DAA520", "#F5DEB3", "#FFF8DC","#DCDCDC"),
                      aesthetics = c("colour", "fill")) +
  geom_text(aes(label=format(round(value, 2), nsmall = 2)), color="black", size=2) + 
  scale_x_discrete(drop=FALSE) +
  scale_y_discrete(drop=FALSE) 

enter image description here

Related