我为此练习苦了一段时间。我选择发布另一个问题,由于机密性原因,正在复制和修改另一种类型的数据。
我正在尝试根据以下示例绘制此数据:
可重现的示例:
test <- structure(list(Rounded_date = structure(c(1420383600, 1420387200,
1420390800, 1420401600, 1420405200, 1420408800, 1420412400, 1420416000,
1420419600, 1420423200, 1420426800, 1420430400, 1420434000, 1420437600,
1420441200, 1420444800, 1420448400, 1420452000, 1420455600, 1420459200,
1420462800, 1420466400, 1420470000, 1420473600, 1420477200, 1420480800,
1420484400, 1420488000, 1420491600, 1420495200, 1420498800, 1420502400,
1420506000, 1420509600, 1420513200, 1420516800, 1420520400, 1420524000,
1420527600, 1420531200, 1420534800, 1420538400, 1420542000, 1420545600,
1420549200, 1420552800, 1420556400, 1420560000, 1420563600, 1420567200,
1420570800, 1420574400, 1420578000, 1420581600, 1420585200, 1420588800,
1420592400, 1420596000, 1420599600, 1420603200, 1420606800, 1420610400,
1420614000, 1420617600, 1420621200, 1420624800, 1420628400, 1420632000,
1420635600, 1420639200, 1420642800, 1420646400, 1420650000, 1420653600,
1420657200, 1420660800, 1420664400, 1420668000, 1420671600, 1420675200,
1420678800, 1420682400, 1420686000, 1420689600, 1420693200, 1420696800,
1420700400, 1420704000, 1420707600, 1420711200, 1420714800, 1420718400,
1420722000, 1420725600, 1420729200, 1420732800, 1420736400, 1420740000,
1420743600, 1420747200, 1420750800, 1420754400, 1420758000, 1420761600,
1420765200, 1420768800, 1420772400, 1420776000, 1420779600, 1420783200,
1420786800, 1420790400, 1420794000, 1420797600, 1420801200, 1420804800,
1420808400, 1420812000, 1420815600, 1420819200, 1420822800, 1420826400,
1420830000, 1420833600, 1420837200, 1420840800, 1420844400, 1420848000,
1420851600, 1420855200, 1420858800, 1420862400, 1420866000, 1420869600,
1420873200, 1420876800, 1420880400, 1420884000, 1420887600, 1420891200,
1420894800, 1420898400, 1420902000, 1420905600, 1420909200, 1420912800,
1420916400, 1420920000, 1420923600, 1420927200, 1420930800, 1420934400,
1420938000, 1420941600, 1420945200, 1420948800, 1420952400, 1420956000,
1420959600, 1420963200, 1420966800, 1420970400, 1420974000, 1420977600,
1420981200, 1420984800, 1420988400, 1420992000, 1420995600, 1420999200,
1421002800, 1421006400, 1421010000, 1421013600, 1421017200, 1421020800,
1421024400, 1421028000, 1421031600, 1421035200, 1421038800, 1421042400,
1421046000, 1421049600, 1421053200, 1421056800, 1421060400, 1421064000,
1421067600, 1421071200, 1421074800, 1421078400, 1421082000, 1421085600,
1421089200, 1421092800, 1421096400, 1421100000, 1421103600, 1421107200,
1421110800, 1421114400, 1421118000, 1421121600, 1421125200, 1421128800,
1421132400, 1421139600, 1421143200, 1421146800, 1421150400, 1421154000,
1421157600, 1421161200, 1421164800, 1421168400, 1421172000, 1421175600,
1421179200, 1421182800, 1421186400, 1421190000, 1421193600, 1421197200,
1421200800, 1421204400, 1421208000, 1421211600, 1421215200, 1421218800,
1421222400, 1421226000, 1421229600, 1421233200, 1421236800, 1421240400,
1421244000, 1421247600, 1421251200, 1421254800, 1421258400, 1421262000,
1421265600, 1421269200, 1421272800, 1421276400, 1421280000, 1421283600,
1421287200, 1421290800, 1421294400, 1421298000, 1421301600, 1421305200,
1421308800, 1421312400, 1421316000, 1421319600, 1421323200, 1421326800,
1421330400, 1421334000, 1421337600, 1421341200, 1421344800, 1421348400,
1421352000, 1421355600, 1421359200, 1421362800, 1421366400, 1421370000,
1421373600, 1421377200, 1421380800, 1421384400, 1421388000, 1421391600,
1421395200, 1421398800, 1421402400, 1421406000, 1421409600, 1421413200,
1421416800, 1421420400, 1421424000, 1421427600, 1421431200, 1421434800,
1421438400, 1421442000, 1421445600, 1421449200, 1421452800, 1421456400,
1421460000, 1421463600, 1421467200, 1421470800, 1421478000, 1421481600,
1421485200, 1421488800, 1421492400, 1421496000, 1421499600, 1421503200,
1421506800, 1421510400, 1421514000, 1421517600, 1421521200, 1421524800,
1421528400, 1421532000, 1421535600, 1421539200, 1421542800, 1421546400,
1421550000, 1421553600, 1421557200, 1421560800, 1421564400, 1421568000,
1421571600, 1421575200, 1421578800, 1421582400, 1421586000, 1421589600,
1421593200, 1421596800, 1421600400, 1421604000, 1421607600, 1421611200,
1421614800, 1421618400, 1421622000, 1421625600, 1421629200, 1421632800,
1421636400, 1421640000, 1421643600, 1421647200, 1421650800, 1421654400,
1421658000, 1421661600, 1421665200, 1421668800, 1421672400, 1421676000,
1421679600, 1421683200, 1421686800, 1421690400, 1421694000, 1421697600,
1421701200, 1421704800, 1421708400, 1421712000, 1421715600, 1421719200,
1421722800, 1421726400, 1421730000, 1421733600, 1421737200, 1421740800,
1421744400, 1421748000, 1421751600, 1421755200, 1421758800, 1421762400,
1421766000, 1421769600, 1421773200, 1421776800, 1421780400, 1421784000,
1421787600, 1421791200, 1421794800, 1421798400, 1421802000, 1421805600,
1421809200, 1421812800, 1421816400, 1421820000, 1421823600, 1421827200,
1421830800, 1421834400, 1421838000, 1421841600, 1421845200, 1421848800,
1421852400, 1421856000, 1421859600, 1421863200, 1421866800, 1421870400,
1421874000, 1421877600, 1421881200, 1421884800, 1421888400, 1421892000,
1421895600, 1421899200, 1421902800, 1421906400, 1421910000, 1421913600,
1421917200, 1421920800, 1421924400, 1421928000, 1421931600, 1421935200,
1421938800, 1421942400, 1421946000, 1421949600, 1421953200, 1421956800,
1421960400, 1421964000, 1421967600, 1421971200, 1421974800, 1421978400,
1421982000, 1421985600, 1421989200, 1421992800, 1421996400, 1.422e+09,
1422003600, 1422007200, 1422010800, 1422014400, 1422018000, 1422021600,
1422025200, 1422028800, 1422032400, 1422036000, 1422039600, 1422043200,
1422046800, 1422050400, 1422054000, 1422057600, 1422061200, 1422064800,
1422068400, 1422072000, 1422075600, 1422079200, 1422082800, 1422086400,
1422090000, 1422093600, 1422097200, 1422100800, 1422104400, 1422108000,
1422111600, 1422115200, 1422118800, 1422122400, 1422126000, 1422129600,
1422133200, 1422136800, 1422140400, 1422144000, 1422147600, 1422151200,
1422154800, 1422158400, 1422162000, 1422165600, 1422169200, 1422172800,
1422176400, 1422180000, 1422183600, 1422187200, 1422190800, 1422194400,
1422198000, 1422201600, 1422205200, 1422208800, 1422212400, 1422216000,
1422219600, 1422223200, 1422226800, 1422230400, 1422234000, 1422237600,
1422241200, 1422244800, 1422248400, 1422252000, 1422255600, 1422259200,
1422262800, 1422266400, 1422270000, 1422273600, 1422277200, 1422280800,
1422284400, 1422288000, 1422291600, 1422295200, 1422298800, 1422302400,
1422306000, 1422309600, 1422313200, 1422316800, 1422320400, 1422324000,
1422327600, 1422331200, 1422334800, 1422338400, 1422342000, 1422345600,
1422349200, 1422352800, 1422356400, 1422360000, 1422363600, 1422367200,
1422370800, 1422374400, 1422378000, 1422381600, 1422385200, 1422388800,
1422392400, 1422396000, 1422399600, 1422403200, 1422406800, 1422410400,
1422414000, 1422417600, 1422421200, 1422424800, 1422428400, 1422432000,
1422435600, 1422439200, 1422442800, 1422446400, 1422450000, 1422453600,
1422457200, 1422460800, 1422464400, 1422468000, 1422471600, 1422475200,
1422478800, 1422482400, 1422486000, 1422489600, 1422493200, 1422496800,
1422500400, 1422504000, 1422507600, 1422511200, 1422514800, 1422518400,
1422522000, 1422525600, 1422529200, 1422532800, 1422536400, 1422540000,
1422543600, 1422547200, 1422550800, 1422554400), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), n = c(2L, 2L, 2L, 2L, 4L, 4L, 5L,
8L, 8L, 10L, 5L, 8L, 7L, 4L, 4L, 5L, 9L, 15L, 10L, 19L, 13L, 19L,
18L, 19L, 21L, 14L, 14L, 15L, 12L, 8L, 10L, 10L, 7L, 6L, 6L,
4L, 4L, 4L, 3L, 5L, 6L, 7L, 10L, 15L, 13L, 18L, 16L, 20L, 18L,
14L, 14L, 14L, 14L, 16L, 12L, 12L, 11L, 7L, 7L, 2L, 8L, 5L, 2L,
6L, 5L, 16L, 9L, 18L, 17L, 11L, 14L, 12L, 17L, 13L, 12L, 10L,
23L, 8L, 11L, 8L, 9L, 6L, 3L, 5L, 2L, 4L, 2L, 3L, 9L, 4L, 15L,
23L, 12L, 12L, 21L, 23L, 15L, 11L, 18L, 18L, 7L, 11L, 16L, 11L,
9L, 14L, 10L, 1L, 5L, 3L, 4L, 6L, 6L, 8L, 16L, 20L, 12L, 19L,
20L, 14L, 16L, 22L, 18L, 13L, 15L, 10L, 18L, 16L, 8L, 9L, 14L,
7L, 3L, 4L, 4L, 6L, 9L, 6L, 6L, 18L, 14L, 11L, 8L, 17L, 11L,
13L, 12L, 12L, 12L, 7L, 8L, 11L, 6L, 10L, 6L, 7L, 4L, 4L, 7L,
7L, 4L, 12L, 18L, 14L, 10L, 18L, 15L, 15L, 15L, 12L, 14L, 12L,
15L, 11L, 12L, 10L, 11L, 10L, 7L, 6L, 5L, 2L, 4L, 5L, 7L, 12L,
15L, 23L, 12L, 22L, 14L, 16L, 19L, 18L, 16L, 14L, 9L, 18L, 11L,
12L, 6L, 8L, 3L, 6L, 3L, 6L, 3L, 5L, 5L, 6L, 11L, 7L, 6L, 14L,
15L, 6L, 11L, 10L, 13L, 11L, 13L, 9L, 8L, 7L, 6L, 5L, 2L, 4L,
4L, 7L, 1L, 5L, 10L, 16L, 13L, 13L, 11L, 17L, 14L, 19L, 13L,
11L, 14L, 13L, 17L, 7L, 13L, 6L, 11L, 9L, 2L, 3L, 3L, 2L, 4L,
4L, 9L, 10L, 17L, 16L, 18L, 18L, 17L, 17L, 13L, 15L, 19L, 11L,
10L, 8L, 12L, 6L, 4L, 5L, 8L, 8L, 2L, 4L, 2L, 8L, 7L, 11L, 15L,
14L, 11L, 9L, 16L, 13L, 11L, 14L, 11L, 12L, 6L, 8L, 7L, 7L, 5L,
3L, 4L, 3L, 4L, 4L, 7L, 9L, 12L, 8L, 11L, 12L, 16L, 8L, 11L,
16L, 7L, 18L, 8L, 12L, 6L, 7L, 3L, 5L, 2L, 9L, 3L, 4L, 3L, 7L,
6L, 2L, 9L, 14L, 12L, 12L, 13L, 16L, 14L, 13L, 17L, 12L, 10L,
16L, 6L, 7L, 8L, 2L, 5L, 2L, 1L, 1L, 6L, 2L, 4L, 12L, 20L, 18L,
24L, 12L, 13L, 20L, 18L, 19L, 18L, 27L, 13L, 16L, 12L, 9L, 8L,
10L, 3L, 3L, 1L, 6L, 3L, 2L, 4L, 8L, 12L, 8L, 13L, 11L, 12L,
13L, 15L, 9L, 17L, 17L, 11L, 15L, 14L, 14L, 10L, 6L, 9L, 4L,
4L, 3L, 4L, 3L, 7L, 8L, 8L, 16L, 13L, 18L, 13L, 17L, 21L, 7L,
14L, 17L, 19L, 8L, 10L, 11L, 9L, 1L, 3L, 3L, 3L, 3L, 2L, 7L,
6L, 5L, 17L, 16L, 11L, 16L, 17L, 19L, 23L, 17L, 22L, 12L, 12L,
14L, 15L, 6L, 8L, 10L, 3L, 5L, 5L, 2L, 4L, 1L, 6L, 11L, 10L,
16L, 16L, 21L, 13L, 20L, 18L, 16L, 24L, 12L, 21L, 6L, 12L, 15L,
9L, 5L, 6L, 9L, 5L, 5L, 7L, 8L, 8L, 13L, 14L, 11L, 15L, 14L,
15L, 12L, 10L, 13L, 13L, 7L, 11L, 10L, 11L, 8L, 17L, 6L, 6L,
8L, 4L, 4L, 5L, 7L, 7L, 10L, 10L, 10L, 10L, 15L, 10L, 17L, 14L,
12L, 18L, 16L, 9L, 17L, 15L, 9L, 9L, 2L, 9L, 3L, 2L, 3L, 4L,
6L, 9L, 15L, 23L, 17L, 20L, 17L, 25L, 23L, 18L, 8L, 19L, 15L,
18L, 11L, 16L, 13L, 7L, 8L, 4L, 5L, 5L, 5L, 1L, 2L, 5L, 11L,
11L, 6L, 20L, 21L, 6L, 19L, 11L, 18L, 15L, 17L, 14L, 12L, 10L,
8L, 8L, 2L, 4L, 3L, 2L, 1L, 2L, 3L, 10L, 10L, 14L, 12L, 14L,
13L, 15L, 14L, 22L, 15L, 12L, 17L, 7L, 12L, 13L, 5L, 5L, 6L,
1L, 3L, 3L, 3L, 4L, 5L, 7L, 13L, 11L, 10L, 15L, 21L, 12L, 14L,
18L, 16L)), row.names = c(NA, -600L), class = c("tbl_df", "tbl",
"data.frame"))
在提供的示例中观察到,我需要基于小时和工作日,最小值和最大值以及置信区间的平均值。
我尝试使用此代码“在不同类型的数据上”实现均值,最大和置信区间。如前所述,这是因为同样,我的数据是机密的,无法披露。但是正如您所看到的,我已经计算了占用率,即复制数据中的n(计数)。这样可以获取最小,最大和置信区间。
#extracting year, month, day and hour from Time column and count = occupancy
dt_sep <- dt_hrs %>%
mutate(
Time = ymd_hms(Time),
Rounded_date = round_date(Time, "hour"),
month = month(Rounded_date),
day = wday(Rounded_date, label = TRUE, abbr = TRUE),
hour = hms::as.hms(Rounded_date)
) %>%
dplyr::count(Rounded_date)
dput(head(dt_sep, 100)) # THIS ENABLED THE REPRODUCIBLE DATA provided above(with changes due to confidentiality)
# getting the averages, min, max, interquartile ranges (Q1 and Q3)
tbl_avg_occ <- dt_sep %>%
group_by(hour, day) %>%
dplyr::summarize(Average_occ = mean(n),
Q1 = quantile(n, 0.025),
Q3 = quantile(n, 0.975),
Min_n = min(n),
Max_n = max(n)) %>%
ungroup()
,然后下令将工作日从星期一开始,因为欧洲将一周的开始算为星期一,而不是星期日。
library(plyr)
Weekdays <- c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
tbl_avg_occ_2 <-arrange(transform(tbl_avg_occ, day = factor(day, levels = Weekdays)), day)
然后使用此代码绘制,由于我使用了facet_wrap,但由于没有使用该函数,因此当然不会像提供的图中那样获得输出。
plt <- tbl_avg_occ_2 %>%
ggplot(aes(x = hour, y = Average_occ, group = day, colour = day)) +
geom_line(size = 0.5, colour = "blue") +
facet_wrap( ~ day, nrow = 1) +
scale_x_continuous(limits = c(0, 23), ) +
scale_y_continuous(limits = c(0, 80)) +
geom_ribbon(aes(ymin = Q1, ymax = Q3), alpha = 0.2) +
geom_ribbon(aes(ymin = Min_n, ymax = Max_n), alpha = 0.1) +
theme_bw()
有人可以帮助我吗? tidyverse要完成此任务。