Reputation: 2282
Please see below the dataset that I am working with:
index d1_t1 d1_t2 d1_t3 d1_t4 d2_t1 d2_t2 d2_t3 d2_t4 d3_t1 d3_t2 d3_t3 d3_t4 d4_t1 d4_t2 d4_t3 d4_t4 d5_t1 d5_t2 d5_t3 d5_t4 d6_t1 d6_t2 d6_t3 d6_t4 d7_t1 d7_t2 d7_t3 d7_t4
1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 1 0 1 1 0 1 0 1 0 1 1 1 1 1 1
2 2 1 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 1 0 0
3 3 1 1 1 0 1 1 0 1 1 0 1 0 1 0 1 1 0 1 1 1 0 1 1 1 1 1 1 1
4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
A short explanation of the variables:
d1t1=Day 1 time 1
d1t2=Day 1 time 2
....
d2t1=Day2 time 1
d2t2=Day2 time 2
0,1= different types of measurements taken at a specific time
I would like to create a line graph using ggplot to show the number of measurements per minute during a week. So basically x-axis to have time, y-axis to have the highest number of measures taken during the week. And the lines show the number of measures taken by the day.
Sample data:
df<-structure(list(index=c (101,200,200,101), d1_t1 = c(1, 1, 1, 1),
d1_t2 = c(1, 1, 1, 1),
d1_t3 = c(1, 1, 1, 1),
d1_t4 = c(1, 1, 0, 1),
d2_t1 = c(1, 1, 1, 1),
d2_t2 = c(1, 1, 1, 1),
d2_t3 = c(1, 0, 1 ,1),
d2_t4 =c(1,0,1,1),
d3_t1 = c(1, 1, 1, 1),
d3_t2 = c(1, 1, 1, 1),
d3_t3 = c(1, 1, 1, 1),
d3_t4 = c(1, 0, 1, 1),
d4_t1 = c(1, 1, 1, 1),
d4_t2 = c(1, 1, 1, 1),
d4_t3 = c(1, 1, 1 ,1),
d4_t4 =c(1,1,1,1),
d5_t1 = c(1, 1, 1, 1),
d5_t2 = c(1, 1, 1, 1),
d5_t3 = c(1, 1, 1, 1),
d5_t4 = c(1, 1, 1, 1),
d6_t1 = c(1, 1, 1, 1),
d6_t2 = c(1, 1, 1, 1),
d6_t3 = c(1, 0, 1 ,1),
d6_t4 =c(1,0,1,1),
d7_t1 = c(1, 1, 1, 1),
d7_t2 = c(1, 1, 1, 1),
d7_t3 = c(1, 0, 1 ,1),
d7_t4 =c(1,0,1,1)), row.names = c(NA,4L), class = "data.frame")
Basically I would like to reduce the above plot to a geom_line graph with days showing the patterns.
Basically I would like to have lines colored by day.
Upvotes: 0
Views: 56
Reputation: 389047
Perhaps, you are looking for :
library(tidyverse)
df %>%
pivot_longer(cols = -index,
names_to = c('day', 'time'),
names_sep = "_") %>%
group_by(day,time) %>%
summarise(value=sum(value)) %>%
ggplot() + aes(time, value, fill = day) +
geom_bar(stat='identity')
Upvotes: 1