I'm having difficulties subsetting data within a loop to create multiple ggplots. The objective is to create a plot for each unique value of id. The problem I believe is in fill = reason which I believe needs subsetting too.
## Calendar plot loop
id.calendar <- function(daily, na.rm = TRUE, ...){
myid <- unique(daily$id)
for (i in seq_along(myid)) {
calendar <-
ggplot(subset(dat, dat$id==myid[i]),
aes(monthweek, weekdayf, fill = reason)) +
geom_tile(colour = "grey80") +
facet_grid(year~monthf)
print(calendar)
}
}
Error: Insufficient values in manual scale. 9 needed but only 6 provided.
dat$id is a string (though it could be converted to numeric). I read somewhere in this forum that subsetting is best done outside of ggplot. Regardless, I need the loop to be fast.