'tile plot for continuous variable in r
Solution 1:[1]
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).
Solution 2:[2]
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:
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 | Andrea M |
| Solution 2 | Quinten |


