Fyll See
Fyll See

Reputation: 49

Charting Event Sequence Data with R

I'm new to R's charting tools and I have a task that I suspect would be easily achieved with R. I've produced a step line chart of an event sequence using the following script:

p = ggplot(data=NULL, aes(stepStartTime, index, group=robot, color=effStatus))+
  geom_step(data=robots)+
  scale_y_reverse(lim=c(65,2))+ 
   theme(
     legend.position="none",
     axis.ticks = element_blank(), 
     axis.text.x = element_blank(), 
     axis.text.y = element_blank(), 
     axis.title.x = element_blank(), 
     axis.title.y = element_blank(),
     panel.background = element_rect(fill = 'transparent', colour = NA),
     plot.background = element_rect(fill = 'transparent', colour = NA)
 )
p + scale_color_manual(values=c("#00ff00", "#0080ff", "#ff0000" )) 

It turns out like this:

.

What I want it to show is each event as a discreet point on the chart like this. The X axis is a timeline: .

The data for the chart is as per the following table. Inefficient events should show up as a red marker:

.

Upvotes: 2

Views: 529

Answers (1)

Jon Spring
Jon Spring

Reputation: 66490

This sounds like a job for geom_point instead of geom_step, since you're looking to show each data point as a mark.

Some fake data:

library(dplyr); library(lubridate)

df <- tibble(
  robot = sample(2*1:33, 1E4, replace = TRUE),
  stepStartTime = ymd_hm(201809090000) +
    runif(1E4, 0, 60*60*24),
  effStatus = sample(c("Efficient", "Inefficient"),
                     1E4, replace = TRUE)
)

Plot them:

ggplot(df, aes(stepStartTime, robot, color = effStatus)) +
  geom_point(size = 2, shape = 'I') +
  scale_y_reverse(breaks = 2*1:33) + 
  theme_minimal() +
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank())

enter image description here

Addendum re: manual colors question: To add specific color for each robot (when efficient) and a special color for inefficient, you could make a new variable beforehand, eg mutate(my_color = if_else(effStatus == "Inefficient", "Inefficient", robot). Then reference my_color in place of robot when you specify color.

To get specific colors, use scale_color_manual:

https://ggplot2.tidyverse.org/reference/scale_manual.html

Upvotes: 2

Related Questions