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 facet 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 measurement taken during the week. What I managed to do is, to sum up the data frame based on columns but I don't know how to plot this.
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")
df
Upvotes: 0
Views: 159
Reputation: 39595
I would suggest an approach like this:
library(tidyverse)
#Melt
df2 <- pivot_longer(df,cols = -index) %>% separate(col = name,sep = '_',into = c('day','time')) %>%
group_by(day,time) %>% summarise(Total=sum(value))
#Plot
ggplot(df2,aes(x=time,y=Total))+
geom_bar(stat='identity',color='black',fill='pink')+
facet_wrap(.~day,scales = 'free')
With next output:
Upvotes: 2