1

I have data about a thing which periodically happens, and it either succeeds or fails.

I would like to visualise this in a bar, so for example it may pass at 11am, then fail at 2pm, then pass again at 3pm. I'd like this in a single horizontal bar, with red for failing and green for passing... I cannot think quite where to start with this!

So far I've got this but it really just isn't at all close:

tm1 <- as.POSIXct("2018-03-24 11:00:00")
tm2 <- as.POSIXct("2018-03-24 14:00:00")
tm3 <- as.POSIXct("2018-03-24 15:00:00")

testData <- data.frame(
  time = c(tm1, tm2, tm3),
  status = c("pass", "fail", "pass"),
  thing = c("a", "a", "a"),
  timeSinceLast = c(0, tm2-tm1, tm3-tm2)
  )

ggplot(testData, aes(x = timeSinceLast, y = thing, fill=status)) + geom_bar(stat = "identity")

And I'd like it to look a bit like this, but with only red and green and only one row: https://netbeez.net/wp-content/uploads/2017/01/pub_dash_screenshot_alert_lane_graph.png

Froom2
  • 1,269
  • 2
  • 13
  • 26
  • 1
    You should describe in more detail the desired output. One bar and one day? Green for pass and red for fail, but what about the rest of the time? – Julius Vainora Mar 26 '18 at 11:57
  • Ah, sorry yes, one bar, one day (for now at least), and the bar essentially filling in the colour up to the next time it happens... does that make sense? so it would be green all the way from 11am to 2pm then red for a bit, then green again. Sorry I'm not sure if I'm explaining very well :/ – Froom2 Mar 26 '18 at 12:02
  • If it starts and ends at 00:00, then what's the color until 11:00? – Julius Vainora Mar 26 '18 at 12:07
  • Arr it's just an example :S we have continuous data for months, I just wanted to explain a small sample of data to work with sorry! – Froom2 Mar 27 '18 at 10:24

1 Answers1

1

I hope that geom_raster() gets you started. I don't know how your data looks exactly, but I think it will be easiest if you code for each hour (or whatever) whether the event is positive or negative.

library(ggplot2)
tm1 <- as.POSIXct("2018-03-24 11:00:00")
tm3 <- as.POSIXct("2018-03-25 11:00:00")
x <- seq(tm1, tm3, by = 60^2)

set.seed(123)
df <- data.frame(x = x,
                 y = 0,
                 z = factor(sample(2, length(x), replace = T)))
head(df)
#>                     x y z
#> 1 2018-03-24 11:00:00 0 1
#> 2 2018-03-24 12:00:00 0 2
#> 3 2018-03-24 13:00:00 0 1
#> 4 2018-03-24 14:00:00 0 2
#> 5 2018-03-24 15:00:00 0 2
#> 6 2018-03-24 16:00:00 0 1

ggplot(df, aes(x, y, fill = z)) + 
  geom_raster() + 
  ylim(-10, 10) +
  scale_fill_manual(values = 2:3)

hplieninger
  • 3,214
  • 27
  • 32
  • I've nearly got this working... I've split it by minute, as i needed higher resolution, but it isn't filling the colour in between them - i'm just getting vertical lines. I can't figure out where it is that it fills the colour between them? :S – Froom2 Mar 28 '18 at 13:22
  • `geom_raster()` won't fill in anything, because it doesn't know what to fill (which color). As I said, you should code for each hour/second whether you want it to be 1 or 2 (red or green). If you need further help, you should show via `dput()` some example data and describe the desired result for the "gaps". – hplieninger Mar 30 '18 at 09:31