title: Plotting the duration of exceedances of a pollutant standard in ggplot2
body: <p>I have a data frame with a Date column and a column of calculated concentrations of a parameter. I am trying to plot a time-series plot that has scatter points of all the concentrations and then have a horizontal line showing the standard of the pollutant(which is 500). I can do this no problem. The problem I am having is trying to plot a line showing the duration of the exceedances > 500. I can't seem to find anything to solve my question. I would appreciate any guidance.</p>
  
  <p><strong>Sample Data:</strong></p>
  
  <pre><code>df&lt;-structure(list(Date_Time = structure(c(1480093200, 1482660000, 
  1395651343, 1329823800, 1326929400, 1331233200, 1490130000, 1476138600, 
  1474070400, 1489393800, 1483272000, 1393515068, 1480471200, 1332680400, 
  1471226400, 1470853800, 1396124591, 1496250000, 1394581991, 1438177553, 
  1332108000, 1493051400, 1475949600, 1491024600, 1488832200, 1473697800, 
  1475404200, 1488511800, 1490212800, 1477040400, 1494793740, 1389346885, 
  1473933600, 1390611191, 1486551600, 1476475200, 1473593400, 1388854543, 
  1327012200, 1493611140), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
      Calculated_TDS = c(271.3692, 634.3604, 634.246, 219.546, 
      674.286, 169.21, 506.118, 452.6932, 314.8412, 4640.3052, 
      358.0844, 734.918, 97.71, 460.358, 385.998, 283.9532, 370.554, 
      309.2356, 296.766, 137.079616, 24.494, 383.996, 321.2476, 
      784.6248, 642.1396, 1320.7032, 213.254, 462.1884, 547.6452, 
      376.274, 195.1216, 595.35, 320.1608, 411.166, 882.5512, 288.5292, 
      533.574, 1000.326, 124.022, 256.6116)), row.names = c(NA, 
  -40L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Date_Time", 
  "Calculated_TDS"))
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>library(tidyverse)
  
  test_df&lt;-df%&gt;%
      mutate(greater = Calculated_TDS &gt; 500)%&gt;%
      group_by(Date_Time,Calculated_TDS)%&gt;%
      summarize(n_greater = sum(greater), duration = length(Date_Time))
  
  
  plot&lt;-ggplot() +
    geom_point(data = test_df , aes(x = Date_Time, y = Calculated_TDS))+
    geom_line(data= test_df,aes(x=Date_Time, y = duration),stat="identity")+
    geom_hline(aes(yintercept = 500,color="red"),size=1.3)
  
  plot
  </code></pre>
  
  <p>I know what I have doesn't make sense but I don't understand how to find the duration of exceedances.</p>
  
  <p><a href="https://i.stack.imgur.com/nCuuw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nCuuw.png" alt="enter image description here"></a></p>