Help to create a heatmap

I would like to create a heatmap of this sample data. I am using this code to generate the graph. The x and y represent the coordinates of an image. I need the heatmap to show the avarge time for each machine_name. Any advise please?

 ggplot(df, aes(x, y), fill = n ) + geom_tile()

Sample Data

data.frame(stringsAsFactors=FALSE,
           machine_name = c("A",
                        "B",
                        "C",
                        "A", 
                        "D",
                        "H",
                        "L", 
                        "O",
                        "E",
                        "Q"),
           x = c("4",
                        "4",
                        "2",
                        "56", 
                        "2",
                        "87",
                        "45", 
                        "89",
                        "4",
                        "5"),
           y = c("",
                        "8",
                        "8",
                        "63", 
                        "85",
                        "44",
                        "5", 
                        "5",
                        "85",
                        "56"),
           averge_time = c("2.3",
                        "2.3",
                        "50",
                        "6.0", 
                        "56",
                        "2.5",
                        "8.9", 
                        "8.9",
                        "8.9",
                        "10.5"),

You have to declare fill within aes() and map your average time variable to the fill aesthetic.

library(tidyverse, quietly = TRUE)
df <- data.frame(stringsAsFactors=FALSE,
                 machine_name = c("A", "B", "C", "A", "D", "H", "L", "O", "E", "Q"),
                 x = c("4", "4", "2", "56", "2", "87", "45", "89", "4", "5"),
                 y = c(NA, "8", "8", "63", "85", "44", "5", "5", "85", "56"),
                 average_time = c(2.3, 2.3, 50, 6, 56, 2.5, 8.9, 8.9, 8.9, 10.5)
)
df %>% 
    ggplot(aes(x, y, fill = average_time)) +
    geom_tile()

Created on 2019-01-16 by the reprex package (v0.2.1)

1 Like

Many Thanks for this. Is there away to change the default colour? Thanks

You can check this scale functions

1 Like

Thanks very much that's very helpful as awlays.

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