I have produced a time series plot that plots the duration of different activities over a 12-hour period for different days. However, at the end of the period for which I have data, instead of plotting the last activity's duration and finishing, my graph fills all the time for which I have no data with a random activity. This is probably best illustrated with my plot: here, the last activity is 'Active Rest', then for the remaining time (15:45 onwards) the plot is filled with a colour corresponding to 'Inactivity', despite the fact that there is no data after 15:45.
The code I'm using is as follows:
cbbPalette <- c("#FF3300", "#FFCC00", "#99FF33", "#0099CC", "#FFCCFF", "#FFFFFF")
p <- ggplot(df, aes(xmin=as.Date(date),xmax=as.Date(date)+1,
ymin=as.POSIXct(start,format='%H:%M:%S'),
ymax=as.POSIXct(start,format='%H:%M:%S')+duration,
fill=event))
p <- p+geom_rect(alpha = I(8/10))
p + ylab("Time of Day") + xlab("Date") + scale_x_date(labels = date_format("%d/%m"), breaks = date_breaks("1 day")) +
scale_y_datetime(labels = date_format("%H:%M"), breaks = date_breaks("2 hour")) +
coord_flip() +
scale_fill_manual(values=cbbPalette, name="Behaviour",
breaks=c("GL", "IA", "P", "R", "SS"),
labels=c("General Movement", "Inactive",
"Pacing", "Rapid Locomotion", "Active Rest"),
guide = guide_legend(reverse=TRUE)) +
theme_bw() +
theme(panel.border = element_rect(linetype = "dashed", colour = "black"))
Here is some of the data from the very end of my dataframe (i.e. where the problem is occurring). Hopefully, it is clear how the abbreviations in 'event' line up with the colours in the plot from my code:
date start duration event
2017-03-06 15:40:00 120 SS
2017-03-06 15:42:00 10 GL
2017-03-06 15:42:10 60 SS
2017-03-06 15:43:10 10 R
2017-03-06 15:43:20 20 SS
2017-03-06 15:43:40 30 IA
2017-03-06 15:44:10 10 SS
2017-03-06 15:44:20 30 IA
2017-03-06 15:44:50 10 SS
I have tried tacking on a few lines of NAs to the end of my dataframe, but that doesn't make a difference. I'm not really sure what is actually going wrong.
I honestly couldn't find any other question that matched my problem - for example, questions on eliminating NAs from ggplot
don't help me, but do let me know if you think you've found a similar problem.