Why I cannot get heatmap only for the dates in my data, R?

Viewed 81

I am trying to create a calendar heatmap but get all the days plotted for 2021. Yet, want to plot only for the days I have in the data set. From 2020 whole year and until 2021 Feb. This is done in R.

This is a fake data I have:

I am using calendarHeat from github:

source("https://raw.githubusercontent.com/iascchen/VisHealth/master/R/calendarHeat.R")

And the actual code is this:

r2g <- c("#D61818", "#FFAE63", "#FFFFBD", "#B5E384")

calendarHeat(occ_abv_18$date, occ_abv_18$oc_18, ncolors = 99, color = "r2g", varname="Occupancy over 18")

I do not understand why I do not get the heatmap? Is there a way to fix this or any other solutions are welcome? Thank you for your help.

1 Answers

The column name is 'Date' and not date (based on the OP's code)

calendarHeat(dates = occ_abv_18$Date, values = occ_abv_18$oc_18, 
      ncolors = 99, color = "r2g", varname="Occupancy over 18")

-output

enter image description here


Regarding the OP'c comments about date range, it is the line in the calendarHeat function

...
min.date <- as.Date(paste(format(min(dates), "%Y"),
                    "-1-1",sep = ""))
max.date <- as.Date(paste(format(max(dates), "%Y"),
                     "-12-31", sep = ""))
...

does the floor/ceiling by year for min/max


Therefore, if we change the function by changing those lines to

body(calendarHeat)[[7]] <- quote(min.date <- min(dates))
body(calendarHeat)[[8]] <- quote(max.date <- max(dates))

it would work

calendarHeat(dates = occ_abv_18$Date, values = occ_abv_18$oc_18, 
      ncolors = 99, color = "r2g", varname="Occupancy over 18")

-output

enter image description here


If we need to change the order of colors in 'r2g', then change the body element 22

body(calendarHeat)[[22]] <- quote(r2g <- rev(c("#D61818", "#FFAE63", "#FFFFBD", "#B5E384")))

applying the function

calendarHeat(dates = occ_abv_18$Date, values = occ_abv_18$oc_18, 
      ncolors = 99, color = "r2g", varname="Occupancy over 18")

-output

enter image description here

Related