Making Calendar with ggplot + Moon Phase Calendar for fun

(This article was first published on R on Chi's Impe[r]fect Blog, and kindly contributed to R-bloggers)

Making Calendar with ggplot2

I recently discovered font called Moon Phase. I also recently discovered R package, suncalc, and I can get Moon Illumination data by day, so I thought I’d use both to create a simple calendar using ggplot2.

Prepping The Data

To make calendar, I need to strip out weekday, month, day, week number within a month. So I can use weekday as x-axis, week number within a month as y-axis, and facet by month.

## use getMoonIllusmination function to get moon fraction, phase, and angle for 2018.
Moon_2018 <-getMoonIllumination(date = seq.Date(as.Date("2018-01-01"), as.Date("2018-12-31"), by = 1), 
                                 keep = c("fraction", "phase", "angle"))

## Append Extra Info, so I can use ggplot to make calendar. 
Moon_2018_df <- Moon_2018 %>% 
  mutate(wkdy = fct_inorder(weekdays(date, abbreviate=T)), ## 2018 worked well because it starts on Monday
         wkn = week(date), ## Week Number
         mo = month(date, label=T, abbr=T), ## Month
         day = day(date), ## Day
         moon.phase = cut(#phase,breaks=seq(0,1,by=1/28), ## this doesn't work...
                          ordered_result=T)) %>%
  group_by(mo) %>% 
  mutate( = dense_rank(wkn)) %>%

## Simply cutting into 28 blocks didn't work out..  
moon.font.tb <- tibble(
  moon.font = c(NA, letters[rep(1:13,rep(2,13))], 0,0, letters[rep(14:26,rep(2,13))],NA),
  moon.phase = levels(Moon_2018_df$moon.phase)

Moon_2018_df <- Moon_2018_df %>% left_join(moon.font.tb)

## Show the data around today to see what data above looks like.
Moon_2018_df %>% filter(between(date,today()-3,today()+3)) %>% kable()
date fraction phase angle wkdy wkn mo day moon.phase moon.font
2018-05-23 0.5928637 0.2797321 -1.1692842 Wed 21 May 23 (0.268,0.286] 4 h
2018-05-24 0.7013470 0.3159631 -1.1353234 Thu 21 May 24 (0.304,0.321] 4 i
2018-05-25 0.7972031 0.3513064 -1.1143088 Fri 21 May 25 (0.339,0.357] 4 j
2018-05-26 0.8764075 0.3856523 -1.1008454 Sat 21 May 26 (0.375,0.393] 4 k
2018-05-27 0.9364869 0.4189058 -1.0823715 Sun 21 May 27 (0.411,0.429] 4 l
2018-05-28 0.9763419 0.4508451 -1.0183864 Mon 22 May 28 (0.446,0.464] 5 m
2018-05-29 0.9959399 0.4797038 -0.6465735 Tue 22 May 29 (0.464,0.482] 5 m

Just Simple Calendar

First I just made simple calendar with below code.

##  Here's Just Calendar Part
Moon_2018_df %>% ggplot(aes(x=wkdy, + 
  geom_point(alpha=0.3, aes(color=wkdy), size=8) + 
  geom_text(aes(label=day),family="Roboto Condensed") +
  facet_wrap(~mo, scales="free_x", ncol=3) +  ## So that Each Line is Quarter!
  scale_x_discrete() +
  scale_y_reverse(breaks=NULL) +
  scale_colour_viridis_d(guide="none") +
  theme_ipsum_rc() +
  labs(title="2018", x="Start of week is Monday", y="") +

Moon Phase Calendar

This time, I tried using geom_tile function to create tiles. I’ve coloured sell using fraction (illuminated fraction of the moon). 0 is New Moon, and 1 is Full Moon. When it’s Full moon sky is lighter, so I’ve coloured cell little bit lighter.

Moon_2018_df %>% ggplot(aes(x=wkdy, + 
  geom_tile(alpha=0.8, aes(fill=fraction)) + 
  geom_text(aes(label=paste0(" ",day)),size=3,family="Roboto Condensed",hjust=0, color="white") +
  geom_text(aes(label=moon.font), family="Moon Phases", hjust=1, color="white", size=5) +
  facet_wrap(~mo, scales="free_x", ncol=3) +  ## So that Each Line is Quarter!
  scale_x_discrete() +
  scale_y_reverse(breaks=NULL) +
  scale_fill_viridis_c(name="Moon Illumination", option="inferno",  begin=0, end=0.25, guide="none") +  ## so that sky is darkest when there's new moon
  theme_ipsum_rc() +
  labs(title="2018 Moon Phase Calendar", x="Start of week is Monday", y="") +
  expand_limits(y=c(0.5,6.5)) +

Yet Another Calendar

Moon_2018_df %>% 
  ggplot(aes(x=day, y=fct_rev(mo))) + 
  theme_minimal(base_family="Roboto Condensed") + 
  scale_x_continuous(breaks=c(1:31), position="top") +
  geom_text(aes(label=moon.font, color=fraction),family="Moon Phases", hjust=0.5, vjust=0.5, size=8, alpha=0.5) +
  geom_text(aes(label=paste0(day,"\n",wkdy)), family="Roboto Condensed", lineheight=0.8, size=4) + 
  labs(x="", y="", title="Another Type of Calendar") +

To leave a comment for the author, please follow the link and comment on their blog: R on Chi's Impe[r]fect Blog. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)