I have a data set that runs for 7 days producing cumulative counts for each of those days broken up into 15 min periods
我有一个运行7天的数据集,产生分解为15分钟的每一天的累积计数
The time period starts from 12 o'clock in the morning and runs until 12 o clock the following day. The order is important.
时间段从早上12点开始,一直持续到第二天的12点钟。订单很重要。
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?
geom_path:每组只包含一个观察。你需要调整群体审美吗? geom_path:每组只包含一个观察。你需要调整群体审美吗?
Can Anyone help?
有人可以帮忙吗?
2 个解决方案
#1
1
Another suggestion with a Date
format for x axis, and different alpha
levels for each variable value:
另一个建议是x轴的日期格式,以及每个变量值的不同alpha级别:
# 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))
#2
1
with some help from this answer https://*.com/questions/35586520/when-creating-a-multiple-line-plot-in-ggplot2-how-do-you-make-one-line-thicker
在这个答案的帮助下https://*.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:
我得到以下内容,它将x轴旋转到几小时:
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
如果我们不围绕小时,x轴不是真的可见,而线条更平滑
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)))
#1
1
Another suggestion with a Date
format for x axis, and different alpha
levels for each variable value:
另一个建议是x轴的日期格式,以及每个变量值的不同alpha级别:
# 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))
#2
1
with some help from this answer https://*.com/questions/35586520/when-creating-a-multiple-line-plot-in-ggplot2-how-do-you-make-one-line-thicker
在这个答案的帮助下https://*.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:
我得到以下内容,它将x轴旋转到几小时:
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
如果我们不围绕小时,x轴不是真的可见,而线条更平滑
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)))