我正在制作车站自行车可用性的时间序列图。到目前为止,我能够以10分钟的间隔绘制可用性。请注意created_at.x
采用POSIXct格式。
> head(Pershing, n =20) id status available_bike_count available_dock_count created_at.x station_summary_id citibike_station_id latitude longitude 41473 293 Active 2 57 2015-10-01 05:20:08 82792 519 40.75188 -73.9777 41474 293 Active 3 56 2015-10-01 09:20:10 82816 519 40.75188 -73.9777 41475 293 Active 4 52 2015-10-01 12:00:06 82832 519 40.75188 -73.9777 41476 293 Active 3 56 2015-10-01 04:50:06 82789 519 40.75188 -73.9777 41477 293 Active 3 56 2015-10-01 06:30:08 82799 519 40.75188 -73.9777 41478 293 Active 2 57 2015-10-01 05:10:07 82791 519 40.75188 -73.9777 41479 293 Active 9 47 2015-10-01 12:40:06 82836 519 40.75188 -73.9777 41480 293 Active 2 57 2015-10-01 07:10:06 82803 519 40.75188 -73.9777 41481 293 Active 1 57 2015-10-01 00:00:12 82760 519 40.75188 -73.9777 41482 293 Active 3 56 2015-10-01 05:00:07 82790 519 40.75188 -73.9777 41483 293 Active 1 56 2015-10-01 02:50:07 82777 519 40.75188 -73.9777 41484 293 Active 3 56 2015-10-01 08:40:06 82812 519 40.75188 -73.9777 41485 293 Active 49 7 2015-10-01 22:50:07 82897 519 40.75188 -73.9777 41486 293 Active 11 44 2015-10-01 16:40:07 82860 519 40.75188 -73.9777 41487 293 Active 3 54 2015-10-01 01:40:06 82770 519 40.75188 -73.9777 41488 293 Active 5 52 2015-10-01 11:10:07 82827 519 40.75188 -73.9777 41489 293 Active 9 47 2015-10-01 18:40:13 82872 519 40.75188 -73.9777 41490 293 Active 8 48 2015-10-01 12:20:08 82834 519 40.75188 -73.9777 41491 293 Active 2 56 2015-10-01 00:40:06 82764 519 40.75188 -73.9777 41492 293 Active 4 52 2015-10-01 12:50:06 82837 519 40.75188 -73.9777
ggplot(data=Pershing,aes(x= created_at.x, y=available_bike_count)) +
geom_point(colour="red") +
ylab("Available Bikes") +
xlab("time")
我想添加显示时间范围的垂直线(实际上是条形)。时间范围应从Pershing_trucks$stoptime
开始,到Pershing_trucks$starttime
> Pershing_trucks X bikeid end.station.id start.station.id diff.time 10 2785 19911 359 519 930 12 3052 23301 359 519 1406 14 3496 23818 359 519 1164 15 3778 23185 359 519 1515 17 3795 21630 359 519 1563 21 5046 22144 359 519 884 22 5063 19132 359 519 1396 24 5461 22994 359 519 1574 25 5861 22176 359 519 1394 27 5956 14716 359 519 1932 32 6341 18099 359 519 1267 33 6415 19432 359 519 1143 34 6763 17121 359 519 827 35 6800 22616 359 519 1693 38 7694 20754 359 519 1690 39 8224 15323 359 519 2465 40 8626 23472 359 519 1046 43 8903 19348 359 519 1724 45 8952 24236 359 519 1289 49 9664 24246 359 519 2723 50 9729 18330 359 519 2030 51 9730 18330 359 519 2438 55 10460 22067 359 519 1069 56 10461 22067 359 519 1635 59 11906 22430 359 519 617 62 12310 17727 359 519 1749 64 12557 15174 359 519 1280 65 12568 24123 359 519 2843 72 13572 20687 359 519 1541 77 15530 22913 359 519 1052 80 15863 16252 359 519 893 82 16901 24265 359 519 3316 85 17731 23792 359 519 751 87 19130 17144 359 519 2162 89 21064 15553 359 519 1817 91 21572 22077 359 519 719 92 21789 19456 359 519 3313 95 22822 19639 359 519 1837 98 24345 20860 359 519 1682 102 25520 17632 359 519 1553 112 28679 20514 359 519 3297 120 30827 19110 359 519 3118 121 31258 16744 359 519 3081 stoptime starttime midtime 10 2015-10-01 08:30:16 2015-10-01 08:45:46 2015-10-01 08:38:01 12 2015-10-01 08:20:30 2015-10-01 08:43:56 2015-10-01 08:32:13 14 2015-10-01 06:23:14 2015-10-01 06:42:38 2015-10-01 06:32:56 15 2015-10-01 07:04:10 2015-10-01 07:29:25 2015-10-01 07:16:47 17 2015-10-01 07:40:59 2015-10-01 08:07:02 2015-10-01 07:54:00 21 2015-10-01 06:51:46 2015-10-01 07:06:30 2015-10-01 06:59:08 22 2015-10-01 07:43:37 2015-10-01 08:06:53 2015-10-01 07:55:15 24 2015-10-01 06:20:39 2015-10-01 06:46:53 2015-10-01 06:33:46 25 2015-10-01 06:46:05 2015-10-01 07:09:19 2015-10-01 06:57:42 27 2015-10-01 08:12:16 2015-10-01 08:44:28 2015-10-01 08:28:22 32 2015-10-01 06:23:19 2015-10-01 06:44:26 2015-10-01 06:33:52 33 2015-10-01 06:25:58 2015-10-01 06:45:01 2015-10-01 06:35:29 34 2015-10-01 06:33:12 2015-10-01 06:46:59 2015-10-01 06:40:05 35 2015-10-01 07:23:30 2015-10-01 07:51:43 2015-10-01 07:37:36 38 2015-10-01 06:41:24 2015-10-01 07:09:34 2015-10-01 06:55:29 39 2015-10-01 06:52:20 2015-10-01 07:33:25 2015-10-01 07:12:52 40 2015-10-01 06:47:52 2015-10-01 07:05:18 2015-10-01 06:56:35 43 2015-10-01 07:37:51 2015-10-01 08:06:35 2015-10-01 07:52:13 45 2015-10-01 06:48:08 2015-10-01 07:09:37 2015-10-01 06:58:52 49 2015-10-01 07:01:33 2015-10-01 07:46:56 2015-10-01 07:24:14 50 2015-10-01 07:00:07 2015-10-01 07:33:57 2015-10-01 07:17:02 51 2015-10-01 07:55:18 2015-10-01 08:35:56 2015-10-01 08:15:37 55 2015-10-01 07:30:05 2015-10-01 07:47:54 2015-10-01 07:38:59 56 2015-10-01 08:16:32 2015-10-01 08:43:47 2015-10-01 08:30:09 59 2015-10-01 07:17:48 2015-10-01 07:28:05 2015-10-01 07:22:56 62 2015-10-01 07:25:07 2015-10-01 07:54:16 2015-10-01 07:39:41 64 2015-10-01 07:25:30 2015-10-01 07:46:50 2015-10-01 07:36:10 65 2015-10-01 09:34:44 2015-10-01 10:22:07 2015-10-01 09:58:25 72 2015-10-01 08:40:39 2015-10-01 09:06:20 2015-10-01 08:53:29 77 2015-10-01 07:47:57 2015-10-01 08:05:29 2015-10-01 07:56:43 80 2015-10-01 08:49:59 2015-10-01 09:04:52 2015-10-01 08:57:25 82 2015-10-01 08:59:52 2015-10-01 09:55:08 2015-10-01 09:27:30 85 2015-10-01 07:51:10 2015-10-01 08:03:41 2015-10-01 07:57:25 87 2015-10-01 08:06:23 2015-10-01 08:42:25 2015-10-01 08:24:24 89 2015-10-01 08:15:09 2015-10-01 08:45:26 2015-10-01 08:30:17 91 2015-10-01 08:50:32 2015-10-01 09:02:31 2015-10-01 08:56:31 92 2015-10-01 08:53:47 2015-10-01 09:49:00 2015-10-01 09:21:23 95 2015-10-01 08:20:05 2015-10-01 08:50:42 2015-10-01 08:35:23 98 2015-10-01 08:34:41 2015-10-01 09:02:43 2015-10-01 08:48:42 102 2015-10-01 08:37:07 2015-10-01 09:03:00 2015-10-01 08:50:03 112 2015-10-01 08:51:15 2015-10-01 09:46:12 2015-10-01 09:18:43 120 2015-10-01 08:56:43 2015-10-01 09:48:41 2015-10-01 09:22:42 121 2015-10-01 09:43:33 2015-10-01 10:34:54 2015-10-01 10:09:13
> dput(Pershing_trucks) structure(list(X = c(2785L, 3052L, 3496L, 3778L, 3795L, 5046L, 5063L, 5461L, 5861L, 5956L, 6341L, 6415L, 6763L, 6800L, 7694L, 8224L, 8626L, 8903L, 8952L, 9664L, 9729L, 9730L, 10460L, 10461L, 11906L, 12310L, 12557L, 12568L, 13572L, 15530L, 15863L, 16901L, 17731L, 19130L, 21064L, 21572L, 21789L, 22822L, 24345L, 25520L, 28679L, 30827L, 31258L), bikeid = c(19911L, 23301L, 23818L, 23185L, 21630L, 22144L, 19132L, 22994L, 22176L, 14716L, 18099L, 19432L, 17121L, 22616L, 20754L, 15323L, 23472L, 19348L, 24236L, 24246L, 18330L, 18330L, 22067L, 22067L, 22430L, 17727L, 15174L, 24123L, 20687L, 22913L, 16252L, 24265L, 23792L, 17144L, 15553L, 22077L, 19456L, 19639L, 20860L, 17632L, 20514L, 19110L, 16744L), end.station.id = c(359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L, 359L), start.station.id = c(519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L, 519L), diff.time = c(930L, 1406L, 1164L, 1515L, 1563L, 884L, 1396L, 1574L, 1394L, 1932L, 1267L, 1143L, 827L, 1693L, 1690L, 2465L, 1046L, 1724L, 1289L, 2723L, 2030L, 2438L, 1069L, 1635L, 617L, 1749L, 1280L, 2843L, 1541L, 1052L, 893L, 3316L, 751L, 2162L, 1817L, 719L, 3313L, 1837L, 1682L, 1553L, 3297L, 3118L, 3081L), stoptime = structure(c(1443706216, 1443705630, 1443698594, 1443701050, 1443703259, 1443700306, 1443703417, 1443698439, 1443699965, 1443705136, 1443698599, 1443698758, 1443699192, 1443702210, 1443699684, 1443700340, 1443700072, 1443703071, 1443700088, 1443700893, 1443700807, 1443704118, 1443702605, 1443705392, 1443701868, 1443702307, 1443702330, 1443710084, 1443706839, 1443703677, 1443707399, 1443707992, 1443703870, 1443704783, 1443705309, 1443707432, 1443707627, 1443705605, 1443706481, 1443706627, 1443707475, 1443707803, 1443710613 ), class = c("POSIXct", "POSIXt"), tzone = "EST"), starttime = structure(c(1443707146, 1443707036, 1443699758, 1443702565, 1443704822, 1443701190, 1443704813, 1443700013, 1443701359, 1443707068, 1443699866, 1443699901, 1443700019, 1443703903, 1443701374, 1443702805, 1443701118, 1443704795, 1443701377, 1443703616, 1443702837, 1443706556, 1443703674, 1443707027, 1443702485, 1443704056, 1443703610, 1443712927, 1443708380, 1443704729, 1443708292, 1443711308, 1443704621, 1443706945, 1443707126, 1443708151, 1443710940, 1443707442, 1443708163, 1443708180, 1443710772, 1443710921, 1443713694 ), class = c("POSIXct", "POSIXt"), tzone = "EST"), midtime = structure(c(1443706681, 1443706333, 1443699176, 1443701807.5, 1443704040.5, 1443700748, 1443704115, 1443699226, 1443700662, 1443706102, 1443699232.5, 1443699329.5, 1443699605.5, 1443703056.5, 1443700529, 1443701572.5, 1443700595, 1443703933, 1443700732.5, 1443702254.5, 1443701822, 1443705337, 1443703139.5, 1443706209.5, 1443702176.5, 1443703181.5, 1443702970, 1443711505.5, 1443707609.5, 1443704203, 1443707845.5, 1443709650, 1443704245.5, 1443705864, 1443706217.5, 1443707791.5, 1443709283.5, 1443706523.5, 1443707322, 1443707403.5, 1443709123.5, 1443709362, 1443712153.5), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("X", "bikeid", "end.station.id", "start.station.id", "diff.time", "stoptime", "starttime", "midtime"), row.names = c(10L, 12L, 14L, 15L, 17L, 21L, 22L, 24L, 25L, 27L, 32L, 33L, 34L, 35L, 38L, 39L, 40L, 43L, 45L, 49L, 50L, 51L, 55L, 56L, 59L, 62L, 64L, 65L, 72L, 77L, 80L, 82L, 85L, 87L, 89L, 91L, 92L, 95L, 98L, 102L, 112L, 120L, 121L), class = "data.frame")
理想情况下,情节看起来像这样:
答案 0 :(得分:0)
我认为这是一个相当复杂的问题。但是,我可以给你部分内容和解决它的想法。
这个想法是创建一个循环函数来创建这样的东西。它可能无法正常工作,但通过一些调整,它可能会。
vertBars <- lapply(seq_along(Pershing_trucks), function(x) {
# Start with a vector that is empty
vertBars <- 0
# Add a ggplot call that creates a grey box with the desired
# start and stop times for a single line number "x"
a <- geom_rect(data=Pershing_trucks, aes(xmin=stoptime[x],
xmax=starttime[x], ymin=-Inf, ymax=+Inf), fill='gray80',
alpha=0.02)
# Add the plot "a" to the empty vector you created. This will loop and
# add all of the bars that you require without having to manually specify them
vertBars <- vertBars + a
# Return the vector "vertBars" that should be able to be added to your plot
return(vertBars)
}
此外,为这样的情节添加+ theme_bw()
可能是个明智之举,因为它看起来更清晰。
答案 1 :(得分:0)
您提出的问题非常简单,我们只需要在您的geom_rect
数据中添加Pershing_trucks
图层。
我不想打扰导入您的Pershing
数据框,因此在从Pershing_trucks
获取dput()
数据后,我采样/模拟了Pershing
数据像这样的框架:
Pershing = data.frame(
created.at.x = sample(Pershing_trucks$starttime, 20),
available_bike_count = rpois(20, 20)
)
让我们创建一个基本图,我们可以用不同的方式添加矩形:
persh_plot =
ggplot(
data = Pershing,
aes(x = created.at.x, y = available_bike_count)
) +
geom_point(color = "red") +
ylab("Available Bikes") +
xlab("time") + theme_bw()
我喜欢其他答案的geom_rect
,但lapply
是不必要的。此外,美学常数(如ymin = -Inf
)应始终设置在aes()
的外。我们还需要设置inherit.aes = FALSE
以防止早期的x
和y
映射被继承:
persh_plot +
geom_rect(
data = Pershing_trucks,
aes(xmin = stoptime,
xmax = starttime),
ymin = -Inf,
ymax = Inf,
fill = 'gray80',
alpha = 0.2,
inherit.aes = FALSE
)
我不确定你想要展示什么 - 但是这里矩形的alpha透明度给出了相当好的重叠指示。请记住,我的Pershing
数据与您的数据结构相似,但是会进行模拟。您可能希望在矩形图层之后添加geom_point()
图层,使点位于顶部。
这并不能很好地匹配您想要的输出,所以这里有一个没有任何填充的轮廓矩形的版本,y min和max在0和20处就像你的情节一样。对我来说看起来很可怕,但对每个人来说都很糟糕:
persh_plot +
geom_rect(
data = Pershing_trucks,
aes(xmin = stoptime,
xmax = starttime),
ymin = 0,
ymax = 20,
color = "black",
fill = NA,
size = 1,
inherit.aes = FALSE
) +
expand_limits(y = 0) # just to force y down to 0 with my simulated data