這是我現在擁有的:
graph_POST %>%
mutate(day = lubridate::day(Date)) %>%
pivot_longer(Standing:New_Sitting, names_to = "Posture") %>%
ggplot(aes(x = Time, y = value, color = Posture))
geom_point(size=0.5)
scale_y_continuous(limits = c(0.5,4.5), expand = expansion(0))
facet_wrap(~day, scales = "free_x")
labs(title = "Posture vs. Time (POST)")
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank())
對于我的“方面”(上午 8 點至下午 5 點)中的所有繪圖,我需要 x 軸相同,并且每小時打勾。我試圖做與我的 y 軸類似的事情,但由于某種原因它似乎不喜歡這樣:scale_x_continuous(limits = c(8:00,5:00))
到目前為止我的輸出:
如果您有任何問題或需要澄清,請告訴我
> dput(head(graph_POST,30))
structure(list(Date = structure(c(19145, 19145, 19145, 19145,
19145, 19145, 19145, 19145, 19145, 19145, 19145, 19145, 19145,
19145, 19145, 19145, 19145, 19145, 19145, 19145, 19145, 19145,
19145, 19145, 19145, 19145, 19145, 19145, 19145, 19145), class = "Date"),
Time = structure(c(1654182900, 1654182901, 1654182902, 1654182903,
1654182904, 1654182905, 1654182906, 1654182907, 1654182908,
1654182909, 1654182910, 1654182911, 1654182912, 1654182913,
1654182914, 1654182915, 1654182916, 1654182917, 1654182918,
1654182919, 1654182920, 1654182921, 1654182922, 1654182923,
1654182924, 1654182925, 1654182926, 1654182927, 1654182928,
1654182929), class = c("POSIXct", "POSIXt"), tzone = ""),
Axis1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Axis2 = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0), Axis3 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0), VM = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Standing = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Stepping = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), Cycling = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0), New_Sitting = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Counter = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(NA,
30L), class = "data.frame")
編輯
graph_POST %>%
mutate(day = lubridate::day(Date)) %>%
pivot_longer(Standing:New_Sitting, names_to = "Posture") %>%
ggplot(aes(x = Time, y = value, color = Posture))
geom_point(data = data.frame(
Time = as.POSIXct(rep(unique(graph_POST$Date), 2))
rep(c(8, 17) * 3600, each = length(unique(graph_POST$Date))),
value = 1, Posture = "Cycling",
day = rep(lubridate::day(unique(graph_POST$Date)), 2)), alpha = 0)
geom_point(size=0.5)
scale_y_continuous(limits = c(0.5,4.5), expand = expansion(0))
scale_x_datetime(date_labels = "%H:%M",
breaks = rep(as.POSIXct(unique(graph_POST$Date)), 4)
rep(c(8, 11, 14, 17) * 3600,
each = length(unique(graph_POST$Date))))
facet_wrap(~day, scales = "free_x")
labs(title = "Posture vs. Time (POST)")
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank())
uj5u.com熱心網友回復:
您可以使用舊的“在上午 8 點和下午 5 點在每個方面添加不可見點”技巧。在這種情況下,這有點麻煩,因為 faceting 變數與 x 軸完全相關。代碼也比它必須的要長一些,因為我認為將標簽準確地放在 08:00 和 17:00 會更好。
請注意,您的示例資料僅包含一天的資料,這意味著該問題不可重現。因此,我創建了一個兼容的資料集以供說明(見下文)
graph_POST %>%
mutate(day = lubridate::day(Date)) %>%
pivot_longer(Standing:New_Sitting, names_to = "Posture") %>%
ggplot(aes(x = Time, y = value, color = Posture))
geom_point(data = data.frame(
Time = as.POSIXct(rep(unique(graph_POST$Date), 2))
rep(c(8, 17) * 3600, each = length(unique(graph_POST$Date))),
value = 1, Posture = "Cycling",
day = rep(lubridate::day(unique(graph_POST$Date)), 2)), alpha = 0)
geom_point(size=0.5)
scale_y_continuous(limits = c(0.5,4.5), expand = expansion(0))
scale_x_datetime(date_labels = "%H:%M",
breaks = rep(as.POSIXct(unique(graph_POST$Date)), 4)
rep(c(8, 11, 14, 17) * 3600,
each = length(unique(graph_POST$Date))))
facet_wrap(~day, scales = "free_x")
labs(title = "Posture vs. Time (POST)")
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank())
可重現的資料
set.seed(1)
graph_POST <- data.frame(Date = rep(as.Date(
c("2022-06-02", "2022-06-03", "2022-06-06", "2022-06-07", "2022-06-08")),
each = 100),
Time = as.POSIXct(rep(as.Date(
c("2022-06-02", "2022-06-03", "2022-06-06", "2022-06-07", "2022-06-08")),
each = 100)) sample(32000:61200, 500),
Standing = sample(c(0, 4), 500, TRUE, prob = c(4, 1)),
Stepping = sample(c(0, 3), 500, TRUE, prob = c(4, 1)),
Cycling = sample(c(0, 2), 500, TRUE, prob = c(4, 1)),
New_Sitting = sample(0:1, 500, TRUE, prob = c(4, 1)))
uj5u.com熱心網友回復:
如果您無論如何都是按天分面,并且只在 x 軸上顯示時間,那么時間變數不需要在 datetime 類中。您可以先將其轉換為連續資料,然后整個 shebang 變得非常簡單。
使用艾倫的資料。(謝謝)
library(tidyverse)
set.seed(1)
graph_POST <- data.frame(Date = rep(as.Date(
c("2022-06-02", "2022-06-03", "2022-06-06", "2022-06-07", "2022-06-08")),
each = 100),
Time = as.POSIXct(rep(as.Date(
c("2022-06-02", "2022-06-03", "2022-06-06", "2022-06-07", "2022-06-08")),
each = 100)) sample(32000:61200, 500),
Standing = sample(c(0, 4), 500, TRUE, prob = c(4, 1)),
Stepping = sample(c(0, 3), 500, TRUE, prob = c(4, 1)),
Cycling = sample(c(0, 2), 500, TRUE, prob = c(4, 1)),
New_Sitting = sample(0:1, 500, TRUE, prob = c(4, 1)))
graph_POST %>%
mutate(day = lubridate::day(Date),
hour = as.integer(format(Time,"%H")),
## If you need it precise to the minute
minute = as.integer(format(Time,"%M")) ,
hour_minute = hour minute/60) %>%
pivot_longer(Standing:New_Sitting, names_to = "Posture") %>%
ggplot(aes(x = hour_minute, y = value, color = Posture))
geom_point()
facet_wrap(~day)
## now setting the limits is easy.
scale_x_continuous(breaks= 8:17, limits = c(8, 17))
#> Warning: Removed 252 rows containing missing values (geom_point).
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491039.html