Search code examples
rggplot2geom

Ggplot2 treating missing data for daily and hourly results as geom_line


I am plotting a graph with both hourly and daily data. I want both lines to be a geom_line, but because there are NAs in the dataframe because of the hourly and daily nature of the data. The line for the daily data is not produced and I can only get it to plot with geom_point, which I don't want.

This is my code:

scaleFUN <- function(x) sprintf("%.2f", x)
plotlist_mAODdate <- list()

j = 1   # counter for plot title and index
for (datTime in mAODdata){
  plotName <- names(mAODdata)[j]
  j = j+1
  plot <- 
    datTime %>%
    ggplot(aes(x=DateTime)) +
    geom_point(aes(y=Rel_mAOD), col='grey') +
    geom_smooth(aes(y=Rel_mAOD), col='black') +
    geom_point(aes(y=(prcp_amt/150)+72), col='blue') +
    theme_classic() +
    labs(y='Water Depth (mAOD)', x=NULL) +
    ggtitle(plotTitles[[plotName]][1]) + 
    scale_x_datetime(
      breaks=seq(min(datTime$DateTime), max(datTime$DateTime), 
                 by= "6 months"), date_labels="%b-%y") +
    scale_y_continuous(labels=scaleFUN, sec.axis = sec_axis(~((.-72)*150), name='Precipitation (mm)')) +
    geom_vline(xintercept=as.POSIXct('2020-11-03 01:00:00'), col='red') +
    geom_vline(xintercept=as.POSIXct('2021-11-01 01:00:00'), col='red', linetype='dashed') +
    theme(text=element_text(size=20, family='Calibri Light')) +
    theme(plot.margin = unit(c(1, 1, 1, 1), 'cm')) +
    theme(axis.title.y=element_text(margin=margin(t=0, r=20, b=0, l=0))) +
    theme(axis.title.x=element_text(margin=margin(t=20, r=0, b=0, l=0)))
  
  plotlist_mAODdate[[plotName]] <- plot
}

This is the line that I want to use geom_line() for:

geom_point(aes(y=(prcp_amt/150)+72), col='blue')

This is what it currently looks like, but I'd like the blue dots to be a line:

enter image description here

This is what the head of the data looks like. There are several locations hence the for loop:

               DateTime Rel_mAOD prcp_amt
1   2020-03-05 17:00:00  74.0054       NA
2   2020-03-05 18:00:00  74.0064       NA
3   2020-03-05 19:00:00  74.0048       NA
4   2020-03-05 20:00:00  74.0054       NA
5   2020-03-05 21:00:00  74.0055       NA
6   2020-03-05 22:00:00  74.0045       NA
7   2020-03-05 23:00:00  74.0049       NA
8   2020-03-06 00:00:00  74.0057      0.0
9   2020-03-06 01:00:00  74.0059       NA
10  2020-03-06 02:00:00  74.0053       NA
11  2020-03-06 03:00:00  74.0057       NA
12  2020-03-06 04:00:00  74.0049       NA
13  2020-03-06 05:00:00  74.0038       NA
14  2020-03-06 06:00:00  74.0044       NA
15  2020-03-06 07:00:00  74.0056       NA
16  2020-03-06 08:00:00  74.0039       NA
17  2020-03-06 09:00:00  74.0025       NA
18  2020-03-06 10:00:00  74.0023       NA
19  2020-03-06 11:00:00  74.0019       NA
20  2020-03-06 12:00:00  74.0041       NA
21  2020-03-06 13:00:00  74.0046       NA
22  2020-03-06 14:00:00  74.0040       NA
23  2020-03-06 15:00:00  74.0045       NA
24  2020-03-06 16:00:00  74.0019       NA
25  2020-03-06 17:00:00  74.0007       NA
26  2020-03-06 18:00:00  74.0009       NA
27  2020-03-06 19:00:00  74.0004       NA
28  2020-03-06 20:00:00  74.0008       NA
29  2020-03-06 21:00:00  74.0009       NA
30  2020-03-06 22:00:00  74.0019       NA
31  2020-03-06 23:00:00  73.9998       NA
32  2020-03-07 00:00:00  74.0004      0.0

Solution

  • As suggested in the comments. This is the solution and works when added into the code found within the question:

    geom_line(data=datTime %>% filter(!is.na(prcp_amt)), aes(y=(prcp_amt/150)+72), col='blue')