Q:

Adjust area from geom_area to a line from geom_line

I'm trying to make a hourly dispatch curve with generation and energy consumpsion data, which have the characteristic that when we do a power balance (generation minus consumpsion) we get values nearly to zero.
Into the generation data there are also net interchange values, that be negative when de power system are exporting energy and positive when the system are importing energy to complete the consumption.

 Thus,  to the plot created with geom_area and geom_line be ok, the black line (consumption) needs be adjusted with the generation area, so that there's no gap between the area and the black line.  But, in my attempts I couldn't do it.   How you can see,  same the energy balence resulting in zero, there are a gap beetwen 19 and 20 hours.  I don't know what is wrong. Someone have idea how to do that?
Thanks in advance.

Data to the plot:

generation <-
  data.frame('dayHour' = c('18/11/2018 18:00','18/11/2018 19:00','18/11/2018 20:00','18/11/2018 21:00','18/11/2018 18:00','18/11/2018 19:00','18/11/2018 20:00','18/11/2018 21:00','18/11/2018 18:00','18/11/2018 19:00','18/11/2018 20:00','18/11/2018 21:00','18/11/2018 18:00','18/11/2018 19:00','18/11/2018 20:00','18/11/2018 21:00'),
             'power' = c(-1364.290, -433.110, 1132.39, 749.48, 463.75, 467.8, 469.35, 436.51, 2025.5, 2133.07, 2306.85, 2304.91, 211.52, 213.16, 214.33, 214.59),
             'label' = c('net interchange', 'net interchange', 'net interchange', 'net interchange', 'gas', 'gas', 'gas', 'gas', 'hydro', 'hydro', 'hydro', 'hydro', 'biomass', 'biomass', 'biomass', 'biomass'))

generation$label <- factor(generation$label, levels = c('net interchange', 'gas', 'hydro', 'biomass'))

net.load <- 
  data.frame('dayHour' = c('18/11/2018 18:00', '18/11/2018 19:00', '18/11/2018 20:00', '18/11/2018 21:00'), 'power' = c(1336.48, 2380.91, 4122.91, 3705.49), 'label' = c('net load', 'net load', 'net load', 'net load'))

generation$dayHour <-
  as.POSIXct(strptime(generation$dayHour,format='%d/%m/%Y %H:%M'))

net.load$dayHour <- 
  as.POSIXct(strptime(net.load$dayHour,format='%d/%m/%Y %H:%M'))

Power balance

pb <- 
filter(generation, label == "biomass")$power +
filter(generation, label == "hydro")$power +
filter(generation, label == "gas")$power +
filter(generation, label == "net interchange")$power -
net.load$power

summary(pb)

Dispatch curve

ggplot() + 

  geom_area(data = generation,
            aes(y = power,
                x = dayHour,
                fill = label)) + 

  geom_line(data = net.load,
            aes(y = power,
                x = dayHour,
                colour = label),
            size = 1.2,
            colour = "black")  +

      labs(fill = "generation",
         colour = 'net load')

A:

It looks like position_stack is getting confused when the interpolation crosses the x-axis.
To fix it, you can interpolate manually before plotting (e.g. with approx):
library(tidyverse)

generation <- data.frame(
    dayHour = structure(c(1542585600, 1542589200, 1542592800, 1542596400, 1542585600, 1542589200, 1542592800, 1542596400, 1542585600, 1542589200, 1542592800, 1542596400, 1542585600, 1542589200, 1542592800, 1542596400), class = c("POSIXct", "POSIXt"), tzone = ""),
    power = c(-1364.29, -433.11, 1132.39, 749.48, 463.75, 467.8, 469.35, 436.51, 2025.5, 2133.07, 2306.85, 2304.91, 211.52, 213.16, 214.33, 214.59),
    label = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("net interchange", "gas", "hydro", "biomass"), class = "factor")
)

generation_interpolated <- generation %>% 
    group_by(label) %>% 
    summarise(data = list(as_tibble(approx(dayHour, power, n = 501)))) %>% 
    unnest() %>% 
    mutate(x = as.POSIXct(x, origin = '1970-01-01', tz = 'UTC')) 

net_power_interpolated <- generation_interpolated %>% 
    group_by(x) %>% 
    summarise(y = sum(y))

ggplot(generation_interpolated, aes(x, y)) + 
    geom_area(aes(fill = label)) + 
    geom_line(data = net_power_interpolated)

To see how approx works, a simpler, ungrouped example:
df <- data.frame(x = c(0, 5, 10), y = c(0, 20, 10))
interpolated <- approx(df$x, df$y, n = 11)
str(interpolated)
#> List of 2
#>  $ x: int [1:11] 0 1 2 3 4 5 6 7 8 9 ...
#>  $ y: num [1:11] 0 4 8 12 16 20 18 16 14 12 ...

ggplot(as.data.frame(interpolated), aes(x, y)) + 
    geom_line() + 
    geom_point() + 
    geom_point(data = df, color = 'dodgerblue', size = 4)

