Home > Blockchain >  ggplot2 geom_linerange remove whitespace between rows
ggplot2 geom_linerange remove whitespace between rows

Time:01-30

Am attempting to create a plot similar to a strip chart recorder showing outage data. Outage Severity is Major and Minor. Plot has a large amount of vertical white space between the two rows and before and after that I would like to remove to create a compact two-row chart.

dataframe is:

> head(dfsub)
                StartDateTime                 EndDateTime Outage.DUR Outage.Severity
1 2021-07-01T00:23:33.0000000 2021-07-01T00:25:26.0000000  1.8833333           Minor
2 2021-07-01T00:25:26.0000000 2021-07-01T00:31:33.0000000  6.1166667           Major
3 2021-07-01T00:31:33.0000000 2021-07-01T00:40:34.0000000  9.0166667           Major
4 2021-07-01T00:40:34.0000000 2021-07-01T00:42:57.0000000  2.3833333           Minor
5 2021-07-01T00:42:57.0000000 2021-07-01T00:43:49.0000000  0.8666667           Minor
6 2021-07-01T00:43:49.0000000 2021-07-01T00:45:35.0000000  1.7666667           Minor

R Code I am running

ggplot(dfsub)  
  geom_linerange(aes(y = Outage.Severity, 
                     xmin = StartDateTime,
                     xmax = EndDateTime,
                     colour = as.factor(Outage.Severity)
                     ),
                 show.legend = FALSE,
                 size = 50)  
  scale_color_manual(values = c("red", "yellow"))  
  theme(legend.position = "none")  
  theme_test()

generates this plot linerange plot - lots of whitespace

CodePudding user response:

Two suggestions.

  1. You didn't ask about this, but your x-axis is broken, using time (which is a continuous thing) in a categorical sense. Note that R and ggplot2 are treating the current columns as strings not timestamps. This is easily resolved:

    dfsub[c("StartDateTime", "EndDateTime")] <-
      lapply(dfsub[c("StartDateTime", "EndDateTime")], as.POSIXct, format="%Y-%m-%dT%H:%M:%OS", tz="UTC")
    
  2. I don't think you're going to get the fine control over blank space between the reds and yellows using geom_linerange, I suggest geom_rect as an option. With that, remove size=, and we'll need to control ymin= and ymax=. This benefits from setting Outage.Severity to a factor; while not completely necessary, it's common for this work to then come back with "how do I change the order of the y-axis categories?", for which the only (sane) response is to convert them to factors and control their levels=. We also need to add fill=, which geom_linerange did not need.

    dfsub$Outage.Severity <- factor(dfsub$Outage.Severity) # add 'levels=' if you want to control the order
    

    From here, knowing that categorical data are plotted on integers, we'll fill the gap between them by extending their rectangles /- 0.48 (arbitrary, but should likely be close to but not at/beyond 0.5).

    ggplot(dfsub)  
      geom_rect(aes(ymin = as.numeric(Outage.Severity)-0.48,
                    ymax = as.numeric(Outage.Severity) 0.48,
                    xmin = StartDateTime,
                    xmax = EndDateTime,
                    colour = Outage.Severity,
                    fill = Outage.Severity),
                show.legend = FALSE)  
      scale_y_continuous(breaks = unique(as.numeric(dfsub$Outage.Severity)), labels = unique(dfsub$Outage.Severity))  
      scale_color_manual(values = c("Major"="red", "Minor"="yellow"))  
      scale_fill_manual(values = c("Major"="red", "Minor"="yellow"))  
      theme(legend.position = "none")  
      theme_test()
    

enter image description here

  •  Tags:  
  • Related