2

I have a big days.data made of energy consumption observations over several weeks. That df is a rbind of several days, where each day has the same amount of rows (60*60*24). Where each row is an energy observation. From that big df, I selected two days i.data, to investigate the consumption.

df <- days.data[i.data,] %>%
  mutate(date = as.factor(as.Date(time,tz="CET")))%>%
  select( time, kWh, date)

df$csum <- ave(df$kWh, df$date, FUN=cumsum)

ggplot(df, aes(x = time, y = csum)) + 
  geom_line(aes(color = date, linetype = date)) +
  scale_color_manual(values = c("darkred", "steelblue")) +
  scale_x_datetime(
    breaks = seq(df[1,1],
                 df[nrow(df),1], 36000),
    labels = date_format(format="%H:%M", tz = "CET"),
    expand = c(0, 0))  

So far so good, the plot looks not so bad.

enter image description here

The thing is, I would like to have both graphs together, not with one week in between... As both days have the same size this should be possible. As soon as I try to use #df$time <- as.numeric(strftime(df$time,"%H:%M", tz = "CET")) ggplot tells me I have discret values, … I need something like that, but with both graphs:

enter image description here

Here a summary of my data (df made of 172800 rows...)

> summary(df)
      time                          kWh                    date            csum      
 Min.   :2020-03-20 00:00:00   Min.   :0.0000000   2020-03-20:86400   Min.   : 0.00  
 1st Qu.:2020-03-20 11:59:59   1st Qu.:0.0000000   2020-03-27:86400   1st Qu.: 0.00  
 Median :2020-03-23 23:59:59   Median :0.0000000                      Median :47.15  
 Mean   :2020-03-23 23:59:59   Mean   :0.0008111                      Mean   :36.78  
 3rd Qu.:2020-03-27 11:59:59   3rd Qu.:0.0000000                      3rd Qu.:69.43  
 Max.   :2020-03-27 23:59:59   Max.   :0.0881141                      Max.   :70.73  
> head(df)
                 time kWh       date csum
1 2020-03-20 00:00:00   0 2020-03-20    0
2 2020-03-20 00:00:01   0 2020-03-20    0
3 2020-03-20 00:00:02   0 2020-03-20    0
4 2020-03-20 00:00:03   0 2020-03-20    0
5 2020-03-20 00:00:04   0 2020-03-20    0
6 2020-03-20 00:00:05   0 2020-03-20    0
> 

What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
Julien
  • 139
  • 13
  • 2
    Please add data as stated [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Apr 07 '20 at 13:52
  • How about facetting: "+ facet_wrap(~date)". https://ggplot2.tidyverse.org/reference/facet_wrap.html – bs93 Apr 07 '20 at 13:59
  • @NelsonGon I tried but data set is so big that it triggered an error from the site when saving. Anyway, thank you for your proposal (facet_wrap). – Julien Apr 07 '20 at 15:36

1 Answers1

1

The better way to do what you want is to use facets. That creates a clear and easy to visually read distinction between the two days. On an apart, if you're going to compare more than one pair of days, it can be useful to make the selection of days inside ggplot.

exdf <- data.frame(time = seq(as.POSIXct("2020-03-20"), as.POSIXct("2020-03-27"), 60))
exdf$kWh <- rlnorm(nrow(exdf))
exdf$date <- as.factor(as.Date(exdf$time))
exdf$csum <- ave(exdf$kWh, exdf$date, FUN = cumsum)

i.data <- c("2020-03-21", "2020-03-25")

ggplot(subset(exdf, date %in% i.data), aes(time, csum)) + 
  geom_line() +
  facet_wrap(~date, scales = "free_x")

enter image description here