Heat chart calendar chart

So, I read this post , and I fell in love a little with the calendar heatmap with monthly Tetris-style breaks.

However, the ggplot example ggplot not implement Tetris breaks, which are arguably the best.

So, FTFY, gist here :

results

The procedure for this:

  • create appropriate cracks for your data
  • left_join your data for Tetris breaks created in (1)
  • deflate above using ggplot using custom geom

The methodology for (1) is quite simple, implemented in the calendar_tetris_data(...) function in gist , although it would be nice to make it a little more flexible.

My question is mainly around (3): how do I collect the 7 geom needed to take breaks in one procedure or geom ?

If I do this:

 calendar_tetris_geoms <- function() { geom_segment(aes(x=x, xend=x, y=ymin, yend=ymax)) + # (a) geom_segment(aes(x=xmin, xend=xmax, y=y, yend=y)) + # (b) geom_segment(aes(x=dec.x, xend=dec.x, y=dec.ymin, yend=dec.ymax)) + # (c) geom_segment(aes(x=nye.xmin, xend=nye.xmax, y=nye.y, yend=nye.y)) + # (d) geom_segment(x=-0.5, xend=51.5, y=7.5, yend=7.5) + # put a line along the top geom_segment(x=0.5, xend=52.5, y=0.5, yend=0.5) + # put a line along the bottom geom_text(aes(x=month.x, y=month.y, label=month.l), hjust=0.25) # (e) } 

And then try adding this to my ggplot , this will not work:

 > ggplot(data) + calendar_tetris_geoms() Error in calendar_tetris_geoms() : argument "plot" is missing, with no default 

I clearly donโ€™t understand how this works. How it works?

+5
source share
1 answer

Modification of @baptiste's suggestion if I do this:

 calendar_tetris_geoms <- function() { list( geom_segment(aes(x=x, xend=x, y=ymin, yend=ymax)), # (a) geom_segment(aes(x=xmin, xend=xmax, y=y, yend=y)), # (b) geom_segment(aes(x=dec.x, xend=dec.x, y=dec.ymin, yend=dec.ymax)), # (c) geom_segment(aes(x=nye.xmin, xend=nye.xmax, y=nye.y, yend=nye.y)), # (d) geom_segment(x=-0.5, xend=51.5, y=7.5, yend=7.5), # put a line along the top geom_segment(x=0.5, xend=52.5, y=0.5, yend=0.5), # put a line along the bottom geom_text(aes(x=month.x, y=month.y, label=month.l), hjust=0.25) # (e) ) } 

Then it works:

 calendar_tetris_data(min(stock.data$date), max(stock.data$date)) %>% left_join(stock.data) %>% ggplot() + geom_tile(aes(x=week, y=wday2factor(wday), fill = Adj.Close), colour = "white") + calendar_tetris_geoms() + facet_wrap(~ year, ncol = 1) 
+1
source

Source: https://habr.com/ru/post/1207144/


All Articles