matrix plot using ggplot2

Dear all,
I am trying to plot a matrix using ggplot for 18 models and 5 variables. for each model i want to show a coorelation, bias and RMSE in one box and give different color based on the values (e.g., red to blue for correlation).
Unfortunatly I am getting the following error and dont know how to overcome this. Below is a smaple code

models <- paste0("Model", 1:18)
variables <- c("Variable1", "Variable2", "Variable3", "Variable4", "Variable5")
correlation <- matrix(runif(90, -1, 1), nrow = 18, ncol = 5)
bias <- matrix(runif(90, -10, 10), nrow = 18, ncol = 5)
rmse <- matrix(runif(90, 0, 100), nrow = 18, ncol = 5)

data <- data.frame(model = rep(models, each = 5),
                   variable = rep(variables, times = 18),
                   correlation = as.vector(correlation),
                   bias = as.vector(bias),
                   rmse = as.vector(rmse))

correlation_palette <- colorRampPalette(brewer.pal(11, "Spectral"))(256)
bias_palette <- colorRampPalette(brewer.pal(11, "RdYlBu"))(256)
rmse_palette <- colorRampPalette(brewer.pal(11, "BrBG"))(256)

matrixplot <- ggplot(data, aes(x = variable, y = model)) + ylab("")+ xlab("")+
  geom_tile(aes(fill = correlation), color = "white", width = 0.9, height = 0.9) +
  geom_tile(aes(fill = bias), color = "white", width = 0.6, height = 0.6, alpha = 0.8) +
  geom_tile(aes(fill = rmse), color = "white", width = 0.3, height = 0.3, alpha = 0.6) +
  scale_fill_gradientn(colors = correlation_palette) +
  scale_fill_gradientn(colors = bias_palette) +
  scale_fill_gradientn(colors = rmse_palette) +
  labs(title = "Model Performance") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
        legend.position = "bottom",
        legend.title = element_blank())


print(matrixplot)

error i am getting is Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale. Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.
Can anyone help.
Thanks, Solomon

In short; ggplot2 alone cannot do what you are trying to do.
There is an extension that can support multiple fill scales.

library(tidyverse)
library( RColorBrewer)
library(ggnewscale)
models <- paste0("Model", 1:18)
variables <- c("Variable1", "Variable2", "Variable3", "Variable4", "Variable5")
correlation <- matrix(runif(90, -1, 1), nrow = 18, ncol = 5)
bias <- matrix(runif(90, -10, 10), nrow = 18, ncol = 5)
rmse <- matrix(runif(90, 0, 100), nrow = 18, ncol = 5)

data <- data.frame(model = rep(models, each = 5),
                   variable = rep(variables, times = 18),
                   correlation = as.vector(correlation),
                   bias = as.vector(bias),
                   rmse = as.vector(rmse))

correlation_palette <- colorRampPalette(brewer.pal(11, "Spectral"))(256)
bias_palette <- colorRampPalette(brewer.pal(11, "RdYlBu"))(256)
rmse_palette <- colorRampPalette(brewer.pal(11, "BrBG"))(256)

matrixplot <- ggplot(data, aes(x = variable, y = model)) + ylab("")+ xlab("")+
  geom_tile(aes(fill = correlation), color = "white", width = 0.9, height = 0.9) +
  scale_fill_gradientn(colors = correlation_palette) +
  new_scale_fill() +
  geom_tile(aes(fill = bias), color = "white", width = 0.6, height = 0.6, alpha = 0.8) +
  scale_fill_gradientn(colors = bias_palette) +
  new_scale_fill() +
  geom_tile(aes(fill = rmse), color = "white", width = 0.3, height = 0.3, alpha = 0.6) +
  scale_fill_gradientn(colors = rmse_palette) +
  labs(title = "Model Performance") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
        legend.position = "bottom",
        legend.title = element_blank())


print(matrixplot)

Thank you very much @nirgrahamuk.

Instead of overlaying the box with different width is there a way to devide one box in to 3, which shows the correlation, bias and RMSE like |cc|bias|rmse|?

Thanks again for the help.

Solomon

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.