2

I followed the example of a Calendar Heatmap shown in this website (you have to scroll down to the Calendar Heat Map section)

Using example from website:

library(ggplot2)
library(plyr)
library(scales)
library(zoo)

df <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/yahoo.csv")
df$date <- as.Date(df$date)  # format date
df <- df[df$year >= 2012, ]  # filter reqd years

# Create Month Week
df$yearmonth <- as.yearmon(df$date)
df$yearmonthf <- factor(df$yearmonth)
df <- ddply(df,.(yearmonthf), transform, monthweek=1+week-min(week))  # compute week number of month
df <- df[, c("year", "yearmonthf", "monthf", "week", "monthweek", "weekdayf", "VIX.Close")]

# Plot
ggplot(df, aes(monthweek, weekdayf, fill = VIX.Close)) + 
  geom_tile(colour = "white") + 
  facet_grid(year~monthf) + 
  scale_fill_gradient(low="red", high="green") +
  labs(x="Week of Month",
   y="",
   title = "Time-Series Calendar Heatmap", 
   subtitle="Yahoo Closing Price", 
   fill="Close")

This is the calendar heatmap you get.

enter image description here

I may be being exceptionally picky here, but is there a way that the bars can fit within the grid lines of the facets while keeping the labels at the midpoints?

I've tried using axis(), following advice from posts like this but I think something gets lost when I try and translate it for the calendar heatmap.

Any advice would be brilliant, thank you!

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • Is `+ coord_cartesian(expand = FALSE)` what you are after? I'm not sure exactly what you think isn't fitting.. Note that `axis` does not work in ggplot2. – Axeman Jan 17 '18 at 15:48
  • Perhaps you want to turn of major grid lines and set minor breaks, but you need to convert to continuous scales then. – Axeman Jan 17 '18 at 15:56
  • Thanks for your suggestion @Axeman, unfortunately that didn't do the trick though. The Y grid lines go through the half way point of the coloured sections, and the X grid lines are too frequent (not the same width of the coloured section) and also go through the half way point. I expect it is something to do with the major/minor breaks but can't seem to solve it. – TheNerdyCat Jan 19 '18 at 08:20
  • I've turned the grid lines off completely using `+ theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())` That works but doesn't look as satisfying as if the grid lines fitted! :) – TheNerdyCat Jan 19 '18 at 08:23

0 Answers0