0

I have a data set that runs for 7 days producing cumulative counts for each of those days broken up into 15 min periods

The time period starts from 12 o'clock in the morning and runs until 12 o clock the following day. The order is important.

Below is a made up sample (apologies for the messy nature)

library(tidyverse)

add_break <- function(check) {
      Zero_break <- paste(check, '00', sep=":") %>% as_tibble()
      fifteen_break <- paste(check, '15', sep=":") %>% as_tibble()
      thirty_break <- paste(check, '30', sep=":") %>% as_tibble()
      fortyfive_break <- paste(check, '45', sep=":") %>% as_tibble()
      bind_rows(Zero_break, fifteen_break, thirty_break, fortyfive_break)
}

# Create the Levels for the Time for every 15 mins
pm <- paste(seq(12,23), sep='') %>% as_tibble()
am <- paste(0, seq(00,09), sep='') %>% as_tibble()
am_2 <- paste(seq(10,11), sep='') %>% as_tibble()
clock <- pm %>% bind_rows(am, am_2)
intervals <- map_df(clock$value, add_break)

# Create Random data for cumsum
mydf <- intervals %>% 
  mutate(MON = cumsum(sample(1:100,size = 96,replace = TRUE)),
         TUE = cumsum(sample(1:100,size = 96,replace = TRUE)),
         WED = cumsum(sample(1:100,size = 96,replace = TRUE)),
         THUR = cumsum(sample(1:100,size = 96,replace = TRUE)),
         FRI = cumsum(sample(1:100,size = 96,replace = TRUE)),
         SAT = cumsum(sample(1:100,size = 96,replace = TRUE)),
         SUN = cumsum(sample(1:100,size = 96,replace = TRUE)))

mydf$AVG <- round(rowMeans(mydf[,2:8]),2)

ggplot(mydf, aes(x=reorder(value, MON), y = MON)) +
geom_line() +
geom_line(aes(x=reorder(value, TUE), y = TUE), colour = 'red')

I then try and create a line graph where i want to create very light colours for the days of the week and then a pretty strong colour for the average

Unfortunately when i run the code i get the following error

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

Can Anyone help?

2 Answers 2

3

Another suggestion with a Date format for x axis, and different alpha levels for each variable value:

plot

# Add a new x variable (supposed to be an hour)
mydf$hour <- as.POSIXct(ifelse(as.double(substr(mydf$value, 1, 2)) >= 12, 
                           paste("2018-01-30 ", mydf$value, ":00", sep = ""), 
                           paste("2018-01-31 ", mydf$value, ":00", sep = "")))


# change to long data format
mydf_m <- gather(mydf, "day", "val", c(2:9))

# reorder variable values
mydf_m$day <- factor(mydf_m$day, levels=c("MON", "TUE", "WED", "THUR", "FRI", "SAT", "SUN", "AVG"))

library(scales) # for date_format in scale_x_datetime
ggplot(mydf_m, aes(x = hour, y = val, color = day, alpha = day, size = day)) +
  theme_bw() + # 
  geom_line() +
  scale_x_datetime(labels = date_format("%H:%M", tz = "CET")) + # x axis
  scale_alpha_manual(values = c("MON" = 0.3, "TUE" = 0.3, "WED" = 0.3, "THUR" = 0.3, "FRI" = 0.3, "SAT" = 0.3, "SUN" = 0.3, "AVG" = 0.8), guide = "none") + 
  scale_size_manual(values = c("MON" = 1, "TUE" = 1, "WED" = 1, "THUR" = 1, "FRI" = 1, "SAT" = 1, "SUN" = 1, "AVG" = 3)) 
Sign up to request clarification or add additional context in comments.

5 Comments

Hi @bVa, thank you for your answer. Unfortunately the order of the hours is important. It needs to be from 12 mid day to 12 mid day. I tried converting everything to a time using lubridate hm function but ggplot (quite rightly) sorts it based on ascending time
@JohnSmith I edited my answer with a little trick to have a correct x scale.
Hi @bVa, when running the code to create the long dataframe i get Error: Column "value" must have a unique name
@JohnSmith Sorry, I forgot to change a variable name : change value to val in gather, I edited it.
Perfect, exactly what i was looking for. Im going to be digesting this for the afternoon. Thanks again, you really have gotten me out of a hole
2

with some help from this answer https://stackoverflow.com/questions/35586520/when-creating-a-multiple-line-plot-in-ggplot2-how-do-you-make-one-line-thicker

I got the following, which rounds the x-axis to hours:

mydf0=melt(ungroup(mydf))
names(mydf0)[1]="Time"
mydf0$Time=as.factor(as.character(hour(ymd_hm(paste("2018-01-01",mydf0$Time,sep=" ")))))
mydf0$Time <- factor(mydf0$Time,levels=c(as.character(seq(12,23,1)),as.character(seq(0,11,1))))
levels(mydf0$Time)
mydf0$size <- rep(.5, nrow(mydf0))
mydf0$size[mydf0$variable=="AVG"] <- 2
ggplot(mydf0, aes(x=Time, y = value,col=variable,group=variable,size=size))+geom_line() +
  scale_size(range = c(0.5, 2), guide="none")

enter image description here

hope it is helpful

if we do not round the hour the x-axis is not really visible, while lines are smoother

    mydf0=melt(ungroup(mydf))
names(mydf0)[1]="Time"
mydf0$Time <- ordered(mydf0$Time,levels=mydf0$Time[1:96])
mydf0$size <- rep(.5, nrow(mydf0))
mydf0$size[mydf0$variable=="AVG"] <- 2
ggplot(mydf0, aes(x=Time, y = value,col=variable,group=variable,size=size))+geom_line() +
  scale_size(range = c(0.5, 2), guide="none")+ scale_x_discrete(breaks=as.character(seq(0,23,1)))

enter image description here

2 Comments

Hi @Antonis, Thank you for answering so quickly. The problem with this method is that the time orders itself from mid night to mid night, i need it to run from mid day to mid day, so basically the order in the mydf$value portion of the dataframe mydf
Hi @John Smith. sorry i did not understand that part. I changed my code and i think now it works as you want. If you find a way to display time values in a visible way in graph2 please let me know :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.