tile plot for continuous variable in r

272 Views Asked by At

I want to see the average departure delay in flights dataset from nycflights13 by distance and month with tile plot. I plotted it and I got this: enter image description here

How can I see it better? I can't understand anything.

2

There are 2 best solutions below

0
On

This is because the distance column is continuous. A tile plot needs the two axes to be categorical. So you first need to categorise the distance column; one way to do this is with cut_number from ggplot2.

library(ggplot2) 
ggplot(nycflights13::flights,
   aes(x = cut_number(distance, n = 5),
       y = factor(month))) +
    geom_tile(aes(fill = dep_delay))

(A tip: next time you ask a question, it is helpful for us to see the code you have written - otherwise it is more difficult to help you. I needed to check which package the flights dataset was from, and what its variables were called).

0
On

Maybe you want something like this. I divide the 'average_delay` in 5 categories so that you get more different colors. You can use this code:

library(nycflights13)
nycflights13::flights

flights %>%
  group_by(month) %>%
  mutate(average_delay = mean(dep_delay, na.rm=TRUE)) %>%
  ggplot(aes(x = distance, y = month)) +
  geom_tile(aes(fill = cut_number(average_delay, n = 5))) +
  scale_colour_gradientn(colours = terrain.colors(10)) +
  scale_fill_discrete(name = "Average delay")

Output:

enter image description here