Search code examples
rdataframeggplot2plotcharts

How do I create a fitting graph in R to visually represent time series datasets?


I have a data.frame in R which contains data about droughts from three sites:

Droughts <- data.frame("site_id" = c(1,1,1,2,2,2,2,3,3),
                       "Drought_start" = c(1962,1970,1980,1960,1970,1978,1990,1965,1988),
                       "Drought_end" = c(1964,1975,1984,1964,1974,1980,1993,1980,1993))

I now want to represent this data in a graph which shows both the droughts and the hiatuses inbetween per site. Something along the lines like this:

enter image description here

Does anyone know how to accomplish this in R? The graph doesn't have to look exactly like the example, as long as it visually represents the data along those lines

I don't know what the type of graph from the example is exactly called and as such wasn't able to properly research it's implementation.


Solution

  • Probably not the prettiest solution, but this will get you close to what you want.

    library(ggplot2)
    
    Droughts$site_id <- factor(paste("Site", Droughts$site_id))
    
    ggplot(Droughts) +
      geom_segment(aes(x = 1950, xend = 2000, y = site_id), lwd = 10, col = "#00a2ff") +
      geom_segment(aes(x = Drought_start, xend = Drought_end, y = site_id), lwd = 10, col = "#ffff00") +
      scale_x_continuous(NULL) +
      scale_y_discrete(NULL, limits = rev(levels(Droughts$site_id))) +
      theme_minimal()
    

    enter image description here