Home > Blockchain >  Plotting dodged periodic time series
Plotting dodged periodic time series

Time:09-12

I have some data about events happening at some hours of the day in certain conditions. The data_frame looks somehow like this :

> tibble(event_id = 1:1000, hour = rep_len(0:23, 1000), conditions = rep_len(c("Non", "Oui"), 1000))
# A tibble: 1,000 × 3
   event_id  hour conditions
      <int> <int> <chr>     
 1        1     0 Non       
 2        2     1 Oui       
 3        3     2 Non       
 4        4     3 Oui       
 5        5     4 Non       
 6        6     5 Oui       
 7        7     6 Non       
 8        8     7 Oui       
 9        9     8 Non       
10       10     9 Oui       

Somehow I have managed to represent it using geom_bar this way :

mydataframe %>% 
group_by(hour, conditions) %>% 
  count() %>% 
  ggplot()  
  geom_bar(aes(x = hour, y = n, fill = conditions), stat = "identity", position = "dodge")

With my actual data, I get a figure looking like this :enter image description here

But I would like to get something like 2 dodged smoothlines or geom_density which I can't seem to get. Do you have some ideas to help me ?

Thank you

CodePudding user response:

library(tidyverse)
set.seed(42)
mydataframe <- tibble(event_id = 1:1000, hour = rep_len(0:23, 1000), conditions = sample(c("Non", "Oui"), 1000, replace = TRUE))

mydataframe %>% 
  count(hour, conditions) %>% 
  ggplot()  
  geom_smooth(aes(hour, n, color = conditions), se = FALSE, span = 0.3)

enter image description here

Or if you want to dodge them, you could do this and tweak the amount of width between the series:

mydataframe %>% 
  count(hour, conditions) %>% 
  ggplot()  
  geom_smooth(aes(hour, n, color = conditions), se = FALSE, span = 0.3,
              position = position_dodge(width = 1))

enter image description here

  • Related