简体   繁体   English

如何用R创建时间螺旋图

[英]How to Create A Time-Spiral Graph Using R

is there any way to plot a graph like this in R and have the same 12 axes on it with thier name ? 有没有办法在R中绘制这样的图形并且在它上面有相同的12个轴?

here's a pic for the graph. 这是图表的图片。 照片为图表

here's a piece of my data 这是我的一个数据

        Date1  Time TravelTime
1  2016-09-04 13:11         34
2  2016-09-04 13:12         34
3  2016-09-04 13:13         33
4  2016-09-04 13:14         33
5  2016-09-04 13:15         33
6  2016-09-04 13:16         43
7  2016-09-04 13:17         44
8  2016-09-04 13:18         44
9  2016-09-04 13:19         40
10 2016-09-04 13:20         39

here's the output from dput 这是dput的输出

structure(list(Date1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = "2016-09-04", class = "factor"), Time = structure(1:10, .Label = c("13:11", 
"13:12", "13:13", "13:14", "13:15", "13:16", "13:17", "13:18", 
"13:19", "13:20"), class = "factor"), TravelTime = c(34L, 34L, 
33L, 33L, 33L, 43L, 44L, 44L, 40L, 39L)), .Names = c("Date1", 
"Time", "TravelTime"), row.names = c(NA, -10L), class = "data.frame")

here's my data for 5 days 这是我5天的数据

Data1 数据1


here's another graph that shows the Time-Spiral ... May you please change your graph to spiral, not a circular ? 这是另一张显示Time-Spiral的图表...请你把图表改成螺旋形而不是圆形?

在此输入图像描述

i got the graph from this link here 我在这里从这个链接得到了图表

The overall approach is to summarize the data into time bins (I used 15-minute bins), where each bin's value is the average travel time for values within that bin. 总体方法是将数据汇总到时间箱(我使用15分钟箱),其中每个箱的值是该箱内值的平均旅行时间。 Then we use the POSIXct date as the y-value so that the graph spirals outward with time. 然后我们使用POSIXct日期作为y值,以便图形随时间向外螺旋。 Using geom_rect , we map average travel time to bar height to create a spiral bar graph. 使用geom_rect ,我们将平均行程时间映射到条形高度,以创建螺旋条形图。

First, load and process the data: 首先,加载并处理数据:

library(dplyr)
library(readxl)
library(ggplot2)

dat = read_excel("Data1.xlsx")

# Convert Date1 and Time to POSIXct
dat$time = with(dat, as.POSIXct(paste(Date1, Time), tz="GMT"))

# Get hour from time
dat$hour = as.numeric(dat$time) %% (24*60*60) / 3600

# Get date from time
dat$day =  as.Date(dat$time)

# Rename Travel Time and convert to numeric
names(dat)[grep("Travel",names(dat))] = "TravelTime"
dat$TravelTime = as.numeric(dat$TravelTime)

Now, summarize the data into 15-minute time-of-day bins with the mean travel time for each bin and create a "spiral time" variable to use as the y-value: 现在,将数据汇总到15分钟的时间箱中,每个箱的平均行程时间,并创建一个“螺旋时间”变量用作y值:

dat.smry = dat %>% 
  mutate(hour.group = cut(hour, breaks=seq(0,24,0.25), labels=seq(0,23.75,0.25), include.lowest=TRUE),
         hour.group = as.numeric(as.character(hour.group))) %>%
  group_by(day, hour.group) %>%
  summarise(meanTT = mean(TravelTime)) %>%
  mutate(spiralTime = as.POSIXct(day) + hour.group*3600)

Finally, plot the data. 最后,绘制数据。 Each 15-minute hour-of-day bin gets its own segment, and we use travel time for the color gradient and the height of the bars. 每个15分钟的小时垃圾箱都有自己的段,我们使用行程时间来显示颜色渐变和条形的高度。 You could of course map fill color and bar height to two different variables if you wish (in your example, fill color is mapped to month; with your data, you could map fill color to date, if that is something you want to highlight). 如果您愿意,您当然可以将填充颜色和条形高度映射到两个不同的变量(在您的示例中,填充颜色映射到月份;使用您的数据,您可以将填充颜色映射到日期,如果这是您要突出显示的内容) 。

ggplot(dat.smry, aes(xmin=as.numeric(hour.group), xmax=as.numeric(hour.group) + 0.25, 
                     ymin=spiralTime, ymax=spiralTime + meanTT*1500, fill=meanTT)) +
  geom_rect(color="grey40", size=0.2) +
  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(dat.smry$spiralTime) + c(-2*24*3600,3600*19), 
                   breaks=seq(min(dat.smry$spiralTime),max(dat.smry$spiralTime),"1 day"),
                   date_labels="%b %e") +
  scale_fill_gradient2(low="green", mid="yellow", high="red", midpoint=35) +
  coord_polar() +
  theme_bw(base_size=13) + 
  labs(x="Hour",y="Day",fill="Mean Travel Time") +
  theme(panel.grid.minor.x=element_line(colour="grey60", size=0.3))

在此输入图像描述

Below are two other versions: The first uses geom_segment and, therefore, maps travel time only to fill color. 以下是另外两个版本:第一个使用geom_segment ,因此,仅将旅行时间映射到填充颜色。 The second uses geom_tile and maps travel time to both fill color and tile height. 第二个使用geom_tile并将旅行时间映射到填充颜色和图块高度。

geom_segment version geom_segment版本

ggplot(dat.smry, aes(x=as.numeric(hour.group), xend=as.numeric(hour.group) + 0.25, 
                     y=spiralTime, yend=spiralTime, colour=meanTT)) +
  geom_segment(size=6) +
  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(dat.smry$spiralTime) + c(-3*24*3600,0), 
               breaks=seq(min(dat.smry$spiralTime), max(dat.smry$spiralTime),"1 day"),
               date_labels="%b %e") +
  scale_colour_gradient2(low="green", mid="yellow", high="red", midpoint=35) +
  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))

在此输入图像描述

geom_tile version geom_tile版本

ggplot(dat.smry, aes(x=as.numeric(hour.group) + 0.25/2, xend=as.numeric(hour.group) + 0.25/2, 
                     y=spiralTime, yend=spiralTime, fill=meanTT)) +
  geom_tile(aes(height=meanTT*1800*0.9)) +
  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(dat.smry$spiralTime) + c(-3*24*3600,3600*9), 
                   breaks=seq(min(dat.smry$spiralTime),max(dat.smry$spiralTime),"1 day"),
                   date_labels="%b %e") +
  scale_fill_gradient2(low="green", mid="yellow", high="red", midpoint=35) +
  coord_polar() +
  theme_bw(base_size=12) + 
  labs(x="Hour",y="Day",color="Mean Travel Time") +
  theme(panel.grid.minor.x=element_line(colour="grey60", size=0.3))

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM