'Creating a heatmap in R

I am trying to create a heatmap of the following plot. Whenever i use the heatmap function instead of geom_point, I get this error:

"Error in heatmap(Wizard_heatmap, aes(x = Gate)) : 'x' must be a numeric matrix"

Dataframe

Wizard_heatmap <- structure(list(Response = c("LIZARD", "LIZARD", "LIZARD", "NR", 
                                              "NR", "WAITER", "WEEKEND", "WHALE", "WHEELCHAIR", "WHIP", "WHISKEY", 
                                              "WHISTLE", "WHISTLE", "WHISTLE", "WIND", "WINDMILL", "WINDOW", 
                                              "WINDOW", "WINTER", "WISDOM", "WISDOM", "WIZARD", "WIZARD", "WIZARD", 
                                              "WIZARD", "WOMEN", "WOOD", "WOODCHIP"), 
                                 Target = c("WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", 
                                            "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", 
                                            "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD",
                                            "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD", "WIZARD"), 
                                 Gate = c("Full", "Gate 2", "Gate 3", "Gate 0", "Gate 1", 
                                          "Gate 0", "Gate 1", "Gate 0", "Gate 0", "Gate 0", "Gate 1", 
                                          "Gate 0", "Gate 1", "Gate 2", "Gate 0", "Gate 0", "Gate 0", 
                                          "Gate 1", "Gate 0", "Gate 1", "Gate 2", "Full", "Gate 1", 
                                          "Gate 2", "Gate 3", "Gate 0", "Gate 0", "Gate 0"), 
                                 n = c(1,  2, 2, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1, 7,
                                       1, 15, 1, 12, 14, 1, 1, 1)), row.names = c(NA, -28L), class = c("tbl_df", "tbl", "data.frame"))

Simple plot

ggplot(Wizard_heatmap, aes(x = Gate)) +
    geom_point(aes(y = Response), size = 4, color = "red")+
    xlab("Gate")

This is for an error analysis, and we are trying to see where errors are concentrated. We also want to get a sense of where the errors are occurring, so that is why the x axis is sorted by gate. Lastly, in the df, you will see one of the columns is labelled "n". larger "n" should be darker on the heatmap. Thanks in advance!



Solution 1:[1]

Here's another solution using the tidyverse package that needs a little pre processing to get all the combinations of Response and Gate and then make the heatmap.

library(tidyverse)

Wizard_heatmap |>
  # Get all combinations of Response and Gate
  expand(Response, Gate) |>
  # Left join with original data frame; new entries will get NA's
  left_join(Wizard_heatmap, 
            by = c("Response", "Gate")) |>
  # Ggplot and set the order of x and y factors
  ggplot(aes(x = fct_inorder(Gate), 
             y = Response,
             fill = n)) +
  # Make tile plot
  geom_tile()+
  # Change labs
  labs( x = "Gate", 
        y = "Response") +
  # Override default fill 
  scale_fill_distiller(type = "seq",
                       palette = "Reds",
                       direction = 1,
                       # Set na values color in hexadecimal code
                       na.value = "#FBF2F0") 

heatmap

Solution 2:[2]

If you want a heatmap with this dataset you could do:

heatmap(with(Wizard_heatmap, as.matrix(table(factor(Response), factor(Gate)))))

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonathan V. Solórzano
Solution 2 Allan Cameron