在螺旋图中区分天数

时间:2016-10-12 18:24:03

标签: r ggplot2

所以有任何关于如何做到这一点的想法,拜托? 提前谢谢。

这是我的data差不多5周

head(All.smry)
Source: local data frame [6 x 7]
Groups: day [1]

         day hour.group   meanTT          spiralTime    Speed DayName   Monthes
      <date>      <dbl>    <dbl>              <dttm>    <dbl>  <fctr>    <fctr>
1 2016-09-04      13.00 7.340000 2016-09-04 13:00:00 29.82016  Sunday September
2 2016-09-04      13.25 6.580000 2016-09-04 13:15:00 33.26444  Sunday September
3 2016-09-04      13.50 5.731111 2016-09-04 13:30:00 38.19155  Sunday September
4 2016-09-04      13.75 5.764444 2016-09-04 13:45:00 37.97070  Sunday September
5 2016-09-04      14.00 5.915556 2016-09-04 14:00:00 37.00075  Sunday September
6 2016-09-04      14.25 6.012222 2016-09-04 14:15:00 36.40584  Sunday September

这里是我的数据总结

 summary(All.smry)
      day               hour.group        meanTT         spiralTime                 
 Min.   :2016-09-04   Min.   : 0.00   Min.   : 3.950   Min.   :2016-09-04 13:00:00  
 1st Qu.:2016-09-14   1st Qu.: 6.00   1st Qu.: 4.533   1st Qu.:2016-09-14 04:37:30  
 Median :2016-09-23   Median :12.00   Median : 5.552   Median :2016-09-23 17:45:00  
 Mean   :2016-09-23   Mean   :11.92   Mean   : 5.910   Mean   :2016-09-23 17:42:08  
 3rd Qu.:2016-10-03   3rd Qu.:18.00   3rd Qu.: 6.738   3rd Qu.:2016-10-03 06:52:30  
 Max.   :2016-10-12   Max.   :23.75   Max.   :26.476   Max.   :2016-10-12 20:00:00  

     Speed             DayName         Monthes    
 Min.   : 8.267   Friday   :480   October  :1137  
 1st Qu.:32.486   Monday   :566   September:2530  
 Median :39.423   Saturday :480                   
 Mean   :39.546   Sunday   :524                   
 3rd Qu.:48.282   Thursday :480                   
 Max.   :55.413   Tuesday  :576                   
                  Wednesday:561    

这是我使用的代码

Title <- "SpiralGraph From GoogleTraffic"
SubTitle <- paste("From",min(as.Date(All$spiralTime)),"To", max(as.Date(All$spiralTime)),sep = " ")
RoadName <- paste("For",name,sep = " ")


ggplot(All.smry, aes(x=as.numeric(hour.group), xend=as.numeric(hour.group) + 0.25, 
                           y=spiralTime, yend=spiralTime, colour=meanTT)) +
  geom_segment(size=1.1) +
  scale_x_continuous(limits=c(0,24), breaks=0:23, minor_breaks=0:24,
                     labels=paste0(rep(c(12,1:11),2), rep(c("AM","PM"),each=12))) +
  scale_y_datetime(limits=range(All.smry$spiralTime) + c(-3*24*3600,0), 
                   breaks=seq(min(All.smry$spiralTime), max(All.smry$spiralTime),"1 day"),
                   date_labels="%b %e") +
  scale_colour_gradientn(colours=c("#009966","orange","#FF0000","#660000")) +
  coord_polar() +
  theme_bw(base_size=10) + 
  labs(x="Hour",y="Day",color="Mean Travel Time") +
  theme(panel.grid.minor.x=element_line(colour="grey60", size=0.3))+
  theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()
        ,axis.title.x=element_blank())+
  ggtitle(bquote(atop(bold(.(Title)), atop(italic(.(SubTitle)),italic(.(RoadName)) )))) +
  theme(axis.title.y=element_blank(),axis.text.y=element_blank(),axis.ticks.y=element_blank()
        ,axis.title.x=element_blank())+theme(text = element_text(size=7))

这是我图表的图片

enter image description here

那么,有什么帮助吗?..我希望这些线条涵盖我想展示的日子并将它们展示到传说栏中

2 个答案:

答案 0 :(得分:1)

尝试删除coord_polar()以更好地了解所发生的情况。

您可以在现有的彩色对角线条之间添加对角线,就像我在我的评论中所说的那样。这需要一些调整,但它应该有效。

另一个解决方案是使用linetype直接在彩色条上操作(请参阅http://sape.inf.usi.ch/quick-reference/ggplot2/linetype了解哪些线型可用)

All.smry$weekDayNumber <- strftime(All.smry$spiralTime, "%u")
All.smry[which(All.smry$weekDayNumber <=5), "momentOfWeek"] <- "weekDay"
All.smry[which(All.smry$weekDayNumber > 5), "momentOfWeek"] <- "weekEnd"

ggplot(All.smry, aes(x=as.numeric(hour.group), 
                     xend=as.numeric(hour.group) + 0.25, 
                     y=spiralTime, 
                     yend=spiralTime, 
                     colour=meanTT)) +
  geom_segment(aes(linetype = momentOfWeek), size=1.1) +
  scale_linetype_manual(name = "Moment of week", values = c("solid","dashed"), labels = c("Week day", "Week-end"), breaks = c("weekDay", "weekEnd")) +
...

答案 1 :(得分:1)

你真的需要螺旋吗? 因为如果没有,你可以这样做:

library("scales")    
ggplot(All.smry, aes(x = as.Date(day), y = hour.group)) +
  geom_raster(aes(fill = meanTT), na.rm = TRUE) +  
  scale_fill_gradientn(colours=c("#009966","orange","#FF0000","#660000")) +
  scale_x_date(breaks = date_breaks("weeks")) +
  scale_y_continuous(name = "Time", limits = c(0,24), breaks = 0:24, expand = c(0,0.2)) 

这会给你:

enter image description here

它需要一些调整,但它可以完成这项工作。