Reputation: 2876
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?
Upvotes: 0
Views: 5238
Reputation: 1939
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")
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)))
Upvotes: 2
Reputation: 3938
Another suggestion with a Date
format for x axis, and different alpha
levels for each variable value:
# 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))
Upvotes: 3