r - ggplot2 time series between business hours only -


i have ggplot2 chart looks like:

enter image description here

the dataframe looks like:

           city weekday     time avg_wait_time           timestamp 1511 boston tuesday 09:06          0.20 2001-01-04 09:06:00 1512 boston tuesday 09:07          0.20 2001-01-04 09:07:00 1513 boston tuesday 09:08          0.20 2001-01-04 09:08:00 1514 boston tuesday 09:09          3.17 2001-01-04 09:09:00 1515 boston tuesday 09:10          3.17 2001-01-04 09:10:00 1516 boston tuesday 09:11          3.17 2001-01-04 09:11:00 1517 boston tuesday 09:12          3.17 2001-01-04 09:12:00 1518 boston tuesday 09:13          5.87 2001-01-04 09:13:00 1519 boston tuesday 09:14          5.87 2001-01-04 09:14:00 1520 boston tuesday 09:15          5.87 2001-01-04 09:15:00 1521 boston tuesday 09:16          5.87 2001-01-04 09:16:00 1522 boston tuesday 09:17          9.17 2001-01-04 09:17:00 1523 boston tuesday 09:18          9.17 2001-01-04 09:18:00 1524 boston tuesday 09:19         12.20 2001-01-04 09:19:00 1525 boston tuesday 09:20         12.20 2001-01-04 09:20:00 1526 boston tuesday 09:21         12.10 2001-01-04 09:21:00 1527 boston tuesday 09:23         13.70 2001-01-04 09:23:00 1528 boston tuesday 09:24         13.70 2001-01-04 09:24:00 1529 boston tuesday 09:25         15.30 2001-01-04 09:25:00 1530 boston tuesday 09:26         15.30 2001-01-04 09:26:00 1531 boston tuesday 09:27         16.90 2001-01-04 09:27:00 1532 boston tuesday 09:28         16.90 2001-01-04 09:28:00 1533 boston tuesday 09:29         18.33 2001-01-04 09:29:00 1534 boston tuesday 09:30         18.33 2001-01-04 09:30:00 1535 boston tuesday 09:31         16.90 2001-01-04 09:31:00 1536 boston tuesday 09:32         16.90 2001-01-04 09:32:00 1537 boston tuesday 09:33         18.57 2001-01-04 09:33:00 1538 boston tuesday 09:34         18.57 2001-01-04 09:34:00 1539 boston tuesday 09:35         21.73 2001-01-04 09:35:00 1540 boston tuesday 09:36         21.73 2001-01-04 09:36:00 

the classes dataframe are:

> sapply(x_output, class) $city [1] "factor"  $weekday [1] "factor"  $time [1] "character"  $avg_wait_time [1] "numeric"  $timestamp [1] "posixct" "posixt"  

as can see graphs, there whitespace because ggplot2 plotting 24 hour day. in dataset, data 1 business-week time period (2001-01-01 2001-01-05) (an "average" week these specific dates arbitrarily set) , business hours between 9am , 6pm.

how can make ggplot2 plot between 9am , 6pm exclusively?

here's attempt:

p <- ggplot(x_output, aes(x=timestamp, y=avg_wait_time, group=city)) +   geom_line(aes(color=city), size=1.5) +    theme(axis.text.x = element_text(angle = 90, hjust=1),          legend.position = "bottom") +    labs(x=null, y="waiting time (minutes)") +    facet_wrap( ~ weekday, ncol=5) +    scale_x_datetime(breaks = date_breaks("1 hour")),                     limits = c(as.posixct("9:00"), as.posixct("18:00"))  print(p) 

i think if can configure limits part correctly, ggplot2 plot properly. help.

update: got step closer using (see scales part):

p <- ggplot(x_output, aes(x=timestamp, y=avg_wait_time, group=city)) +   geom_line(aes(color=city), size=1.5) +    theme(axis.text.x = element_text(angle = 90, hjust=1),         legend.position = "bottom") +    labs(x=null, y="waiting time (minutes)") +    facet_wrap( ~ weekday, ncol=5, **scales="free"**) +   scale_x_datetime(breaks = date_breaks("1 hour"),                     labels=date_format("%h:%m")) 

now output looks like:

enter image description here

however, can tell image, axis wrong , doesn't reflect data. data looks (i've put copy here: http://pastebin.com/sdhzkhpc):

> r <- r[order(r$timestamp),] > head(r,15)        city  weekday     time avg_wait_time           timestamp 2714 boston thursday 10:04          0.00 2001-01-06 10:04:00 2715 boston thursday 10:05          0.00 2001-01-06 10:05:00 2716 boston thursday 10:06          0.23 2001-01-06 10:06:00 2717 boston thursday 10:07          0.23 2001-01-06 10:07:00 2718 boston thursday 10:08          3.33 2001-01-06 10:08:00 2719 boston thursday 10:09          3.33 2001-01-06 10:09:00 2720 boston thursday 10:10          4.80 2001-01-06 10:10:00 2721 boston thursday 10:11          4.80 2001-01-06 10:11:00 2722 boston thursday 10:12          6.33 2001-01-06 10:12:00 2723 boston thursday 10:13          6.33 2001-01-06 10:13:00 2724 boston thursday 10:14          7.90 2001-01-06 10:14:00 2725 boston thursday 10:15          7.90 2001-01-06 10:15:00 2726 boston thursday 10:16          9.50 2001-01-06 10:16:00 2727 boston thursday 10:17          9.50 2001-01-06 10:17:00 2728 boston thursday 10:18         12.17 2001-01-06 10:18:00 > tail(r,15)        city  weekday     time avg_wait_time           timestamp 2699 boston thursday 05:41 pm         22.07 2001-01-06 17:41:00 2700 boston thursday 05:42 pm         23.47 2001-01-06 17:42:00 2701 boston thursday 05:43 pm         23.47 2001-01-06 17:43:00 2702 boston thursday 05:44 pm         24.90 2001-01-06 17:44:00 2703 boston thursday 05:45 pm         24.90 2001-01-06 17:45:00 2704 boston thursday 05:46 pm         26.43 2001-01-06 17:46:00 2705 boston thursday 05:47 pm         19.33 2001-01-06 17:47:00 2706 boston thursday 05:49 pm         12.23 2001-01-06 17:49:00 2707 boston thursday 05:50 pm          0.00 2001-01-06 17:50:00 2708 boston thursday 05:52 pm          0.23 2001-01-06 17:52:00 2709 boston thursday 05:54 pm          1.80 2001-01-06 17:54:00 2710 boston thursday 05:55 pm          1.80 2001-01-06 17:55:00 2711 boston thursday 05:56 pm          0.00 2001-01-06 17:56:00 2712 boston thursday 05:57 pm          0.00 2001-01-06 17:57:00 2713 boston thursday 05:58 pm          0.00 2001-01-06 17:58:00 

update 2: timezone issue.

i solved using resource: plotting times ggplot: added hour in plot maybe due daylight saving?

more specifically, did:

tz(x_output$timestamp) <- "gmt" # "america/new_york" 

the comment included trial test several different time zones see matched (they can found here: http://www.inside-r.org/packages/cran/lubridate/docs/tz).

i keep question posterity.

my approach not use timestamp x axis rather use time.

x_output$time <- strftime(timestamp, format="%h:%m") 

then setting ggplot using call same weekday facet should work.

p <- ggplot(x_output, aes(x=time, y=avg_wait_time, group=city)) 

nb: don't have access r environment right cannot directly verify if code works in principle should :)


Comments