0

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 plot in question

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.

John Tate
  • 1
  • 2
  • 2
    I cannot reproduce your problem with the data provided - perhaps because there is only data from one day. – Richard Telford Mar 13 '17 at 14:47
  • 1
    Can you give us a reproducible example? – Axeman Mar 13 '17 at 14:55
  • There is a considerable amount of data, would you be able to advise how I could provide it? – John Tate Mar 13 '17 at 15:32
  • 1
    You don't need to provide all data, just a small subset that shows your problem when we plot it. When we plot your current data everything looks just fine! See [here](http://stackoverflow.com/questions/5963269). – Axeman Mar 13 '17 at 16:35
  • I'm a complete beginner in R, so I'm not sure of the code to produce this, but verbally the data set is as follows: date <- as.date, each day between 02/03/2017 and 06/03/2017 time <- as.POSIXct, every 10 seconds from 07:00:00 to 19:00:00 for each day duration <- difftime, any number that is a multiple of 10 (for realism, up to 500). Represents seconds event <- as.character, "GL", "IA", "SS", "P", "R" in any order and with any frequency, but the same letters sequentially I hope that makes sense - apologies for my lack of knowledge – John Tate Mar 13 '17 at 17:18
  • I think you need to take the data you have and reduce it - fewer days, shorter time window - until you have the minimal dataset that reproduces your problem. Without a reproducible example, it is hard to help – Richard Telford Mar 14 '17 at 09:35
  • The minimal dataset I need to reproduce the problem is 1 day, but that still encompasses over 1000 observations - is there any repositry where I could just upload the data? – John Tate Mar 14 '17 at 11:50

0 Answers0