library(tidyverse) library(TFTSA)
tml <- jdzzl[[10]] tml_loess <- jdzzl_loess[[10]]
tml_long <- reshape2::melt(tml) names(tml_long) <- c("Date","Timestamp","Traffic_volume") tml_long <- arrange(tml_long,Date,Timestamp) head(tml_long)
holiday <- unique(tml_long$Date)[9:16]
tml_long$Holiday <- ifelse(tml_long$Date %in% holiday, TRUE, FALSE) head(tml_long)
ggplot(tml_long,aes(x=Timestamp,y=Traffic_volume,group=Date,color=Holiday))+ geom_line()
by_holiday <- group_by(tml_long,Holiday,Timestamp) summarise(by_holiday,Mean_traffic_volume=mean(Traffic_volume)) %>% ggplot(aes(x=Timestamp,y=Mean_traffic_volume,group=Holiday,color=Holiday))+ geom_line(size=1)+ scale_x_continuous(breaks = seq(0,288,24))+ scale_y_continuous(breaks = seq(0,120,20))+ theme_bw()
ggsave(file="plot/tml_holidayandnot.jpg",width=7.29,height=4.5,dpi=600)
filter(tml_long,Date=="Oct-01") %>% ggplot(aes(x=Timestamp,y=Traffic_volume))+ geom_point(color="black")+ geom_line(color="steelblue",size=0.8)+ geom_smooth(span=0.1,size=1.5)+ scale_x_continuous(breaks = seq(0,288,24))+ scale_y_continuous(breaks = seq(0,120,20))+ theme_bw()
ggsave(file="plot/03_tml_Oct-01.jpg",width=7.29,height=4.5,dpi=600)
filter(tml_long,Holiday == T) %>% ggplot(aes(x=Timestamp,y=Traffic_volume,group=Date,color=Date))+ geom_line(alpha=2/5)+ geom_smooth(method = "loess",span=0.1)+ scale_x_continuous(breaks = seq(0,288,24))+ scale_y_continuous(breaks = seq(0,120,20))+ theme_bw()
ggsave(file="plot/tml_holiday.jpg",width=7.29,height=4.5,dpi=600)
filter(tml_long,Holiday == F) %>% ggplot(aes(x=Timestamp,y=Traffic_volume,group=Date,color=Date))+ geom_line(alpha=2/5)+ geom_smooth(method = "loess",span=0.1)+ scale_x_continuous(breaks = seq(0,288,24))+ scale_y_continuous(breaks = seq(0,120,20))+ theme_bw()
ggsave(file="plot/tml_notholiday.jpg",width=7.29,height=4.5,dpi=600)
Melt every data frame in list.
jdzzl_long <- lapply(jdzzl, function(x){ x <- reshape2::melt(x) names(x) <- c("Date","Timestamp","Traffic_volume") x <- arrange(x,Date,Timestamp) x$Holiday <- ifelse(x$Date %in% holiday, T, F) by_holiday <- group_by(x,Holiday,Timestamp) x <- summarise(by_holiday,Mean_traffic_volume=mean(Traffic_volume)) return(x) })
merge_list <- function(bywhat){ function(df1,df2){ merge(df1,df2,by=bywhat) } } tep_list <- lapply(jdzzl_long,filter,Holiday == T) tep_list <- lapply(tep_list,`[`,c(2,3)) merged_list <- Reduce(merge_list(bywhat = "Timestamp"),tep_list) names(merged_list)[2:11] <- names(jdzzl_long) merged_df <- merged_list[,c(1,2,3,5,6,9,10,11)] names(merged_df)[c(5:6,8)] <- c("平关","松坎","桐木岭") reshape2::melt(merged_df, id.vars="Timestamp", variable.name="Station", value.name="Mean_traffic_volume") %>% ggplot(aes(x=Timestamp,y=Mean_traffic_volume,group=Station,color=Station))+ geom_line(size=1)+ scale_x_continuous(breaks = seq(0,288,24))+ scale_y_continuous(breaks = seq(0,200,20))+ theme_bw()
ggsave(file="plot/holiday_station.jpg",width=7.29,height=4.5,dpi=600)
rm(tep_list,merged_list,merged_df)
tep_list <- lapply(jdzzl_long,filter,Holiday == F) tep_list <- lapply(tep_list,`[`,c(2,3)) merged_list <- Reduce(merge_list(bywhat = "Timestamp"),tep_list) names(merged_list)[2:11] <- names(jdzzl_long) merged_df <- merged_list[,c(1,2,3,5,6,9,10,11)] names(merged_df)[c(5:6,8)] <- c("平关","松坎","桐木岭") reshape2::melt(merged_df, id.vars="Timestamp", variable.name="Station", value.name="Mean_traffic_volume") %>% ggplot(aes(x=Timestamp,y=Mean_traffic_volume,group=Station,color=Station))+ geom_line(size=1)+ scale_x_continuous(breaks = seq(0,288,24))+ scale_y_continuous(breaks = seq(0,120,20))+ theme_bw()
ggsave(file="plot/notholiday_station.jpg",width=7.29,height=4.5,dpi=600)
selectedstation <- c("丙妹","黑石","南宁","平关","松坎","台盘","桐木岭")
merged <- Reduce(merge_list(bywhat = c("Timestamp","Holiday")),jdzzl_long) names(merged)[3:12] <- names(jdzzl_long) merged <- arrange(merged,Holiday,Timestamp) melted <- reshape2::melt(merged,id.vars=c("Timestamp","Holiday"),variable.name="Station",value.name="Mean_traffic_volume") melted <- filter(melted,Station %in% selectedstation) ggplot(melted,aes(x=Timestamp,y=Mean_traffic_volume,Group=Holiday,color=Holiday))+ geom_line(alpha=0.8)+ facet_wrap(~ Station, nrow = 2, scales = "free")+ theme_bw()
ggsave(file="plot/holiday_station_facet.jpg",width=7.29,height=4.5,dpi=600)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.