Home > Net >  Creating a heatmap in R
Creating a heatmap in R

Time:03-23

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!

CodePudding user response:

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

  • Related