ggplot Heatmap make cells square with unkown number of unique y-values

Viewed 51

I know there is at least one very similar question here: Getting geom_tile to draw square rather than rectangular cells

However, I could not figure out how to make it.

I have this dataframe:

structure(list(fake_date = structure(c(18331, 18333, 18334, 18338, 
18339, 18340, 18341, 18345, 18346, 18347, 18348, 18352, 18353, 
18354, 18355, 18358, 18359, 18360, 18361, 18366, 18367, 18368, 
18369, 18372, 18373, 18374, 18375, 18376, 18379, 18380, 18381, 
18382, 18386, 18387, 18388, 18389, 18390, 18393, 18394, 18395, 
18397, 18400, 18402, 18404, 18407, 18410, 18415, 18421, 18425, 
18428, 18431, 18436, 18451, 18473, 18480, 18501, 18526, 18535, 
18540, 18543, 18549, 18555, 18563, 18568, 18576, 18582, 18591, 
18592, 18604, 18607, 18609, 18610, 18612, 18617, 18619, 18266, 
18280, 18290, 18293, 18302, 18308, 18311, 18314, 18324, 18325, 
18329, 18337, 18340, 18343, 18344, 18352, 18353, 18358, 18373, 
18378, 18392, 18409, 18445, 18484, 18567, 18581, 18584, 18592, 
18596, 18598, 18605, 18609, 18617, 18268, 18278, 18285, 18308, 
18310, 18338, 18358), class = "Date"), year = c(2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2022, 2022, 2022, 2022, 2022, 
2022, 2022), q = c(18.8, 20.5, 15.1, 17.8, 23.5, 22.5, 17.2, 
16.2, 20.4, 20.3, 17.4, 17.4, 17.2, 19.2, 15.1, 11.8, 16.5, 18.1, 
21.6, 18.1, 18.9, 21.2, 15.1, 12, 13.2, 16.3, 19.9, 17.8, 12.6, 
15.1, 19.4, 20, 12.5, 16.1, 10, 20.5, 12.8, 11.5, 15.9, 17.2, 
12.8, 11.3, 14.4, 15, 10.5, 19.2, 14.6, 9.9, 14.5, 12.2, 17.6, 
14.4, 7.7, 21.3, 18.2, 16.8, 11.8, 11.4, 10.2, 20.6, 16.8, 17, 
19.8, 13.7, 17.7, 15.1, 12.7, 15, 16.6, 17.5, 31.4, 14, 14.8, 
11.4, 14.6, 19.2, 19.2, 14.4, 11.6, 16.4, 16, 11.6, 10.5, 16.8, 
16.5, 14.6, 17.6, 13.3, 15, 17.4, 13.8, 15.4, 18.1, 17.4, 13.8, 
12.4, 19.5, 9, 16.5, 10.5, 11.5, 20.3, 17, 15.5, 22.7, 21.5, 
11.6, 17.3, 17.6, 10.4, 11.2, 15.2, 16.1, 19.2, 0)), row.names = c(NA, 
-115L), class = c("tbl_df", "tbl", "data.frame"))

which looks like this:

  fake_date   year     q
  <date>     <dbl> <dbl>
1 2020-03-10  2020  18.8
2 2020-03-12  2020  20.5
3 2020-03-13  2020  15.1
4 2020-03-17  2020  17.8
5 2020-03-18  2020  23.5
6 2020-03-19  2020  22.5

I want to plot the fake_date on the x-axis and the year (numeric in this case but could be a factor) on the y-axis. I also want to fill the boxes with the q column. So I tried to do this (knowing that it would not work):

ggplot(df, aes(fake_date, year)) +
  geom_tile(aes(fill=q))

enter image description here

So I figured I should use the coord_equal coordinate system and set a value ratio. But I just cant figure out how to set it. I also would prefer to not set a fixed value, because the number of unique values on the y-axis (the number of unique years) might vary.

1 Answers
Related