英文:
How to make geom_segment "start over" once a limit has been reached
问题
I have data on shift lengths. Right now, I am trying to visualize which shifts overlap. As an example, consider three shifts, a 12-hour shift beginning at 8 am and ending at 8 pm, an 8-hour shift beginning at 8 am and ending at 4 pm, and a final 12-hour shift beginning at 4 pm and ending at 4 am.
I am trying to create a geom_segment
graph that essentially plots these three shifts with three segments—one for each shift. The x-axis would be the hour of the day (00:00-23:59).
The difficulty I am having is figuring out how to make the shift beginning at 4 pm "start over" once it goes past the 23rd hour. Essentially, I would want a solid line beginning at 4 pm (i.e., 16:00), going on until 11:59 pm (i.e., 23:59), and then starting back over at 12 am (00:00) and going until 4 am (04:00).
Here is a sample tibble:
# Load the necessary packages
library(lubridate)
library(tibble)
# Define shift information as a tibble
shift_info <- tibble(
shift = c("Shift 1", "Shift 2", "Shift 3"),
start_time = ymd_hms(c("2023-03-03 08:00:00", "2023-03-03 08:00:00", "2023-03-03 16:00:00")),
end_time = ymd_hms(c("2023-03-03 20:00:00", "2023-03-03 16:00:00", "2023-03-04 04:00:00"))
)
英文:
I have data on shift lengths. Right now, I am trying to visualize which shifts overlap. As an example, consider three shifts, a 12 hour shift beginning at 8am and ending at 8pm, an 8 hour shift, beginning at 8am and ending at 4pm, and a final 12 hour shift beginning at 4pm and ending at 4am.
I am trying to create a geom_segment
graph that essentially plots these three shifts with three segments—one for each shift. The x-axis would be the hour of the day (00:00-23:59).
The difficulty I am having is figuring out how to make the shift beginning at 4pm "start over" once it goes past the 23rd hour. Essentially, I would want a solid line beginning at 4pm (ie 16:00), going on until 11:59pm (ie 23:59), and then starting back over at 12am (00:00) and going until 4am (04:00).
Here is a sample tibble:
# Load the necessary packages
library(lubridate)
library(tibble)
# Define shift information as a tibble
shift_info <- tibble(
shift = c("Shift 1", "Shift 2", "Shift 3"),
start_time = ymd_hms(c("2023-03-03 08:00:00", "2023-03-03 08:00:00", "2023-03-03 16:00:00")),
end_time = ymd_hms(c("2023-03-03 20:00:00", "2023-03-03 16:00:00", "2023-03-04 04:00:00"))
)
答案1
得分: 0
以下是您要翻译的代码部分:
library(lubridate)
library(ggplot2)
shift_info$end_time[3] <- shift_info$end_time[3] + days(1)
ggplot(shift_info, aes(x = hour(start_time):hour(end_time), y = shift)) +
geom_segment(aes(x = hour(start_time), xend = hour(end_time), yend = shift), color = "gold", size = 3) +
scale_x_continuous(limits = c(0, 23)) +
labs(x = "一天中的小时", y = "") +
theme_classic()
希望这有所帮助!
英文:
Something like this:
library(lubridate)
library(ggplot2)
shift_info$end_time[3] <- shift_info$end_time[3] + days(1)
ggplot(shift_info, aes(x = hour(start_time):hour(end_time), y = shift)) +
geom_segment(aes(x = hour(start_time), xend = hour(end_time), yend = shift), color = "gold", size = 3) +
scale_x_continuous(limits = c(0, 23)) +
labs(x = "Hour of the day", y = "") +
theme_classic()
答案2
得分: 0
一种选择是进行一些数据处理,将跨越两天的班次拆分为两个部分或观察结果。请注意,这还需要重置日期,所以我从坐标轴标签中删除了日期:
shift_info <- shift_info |>
filter(day(start_time) < day(end_time)) |>
mutate(
start_time = ceiling_date(start_time, "day"),
across(c(end_time, start_time), ~ .x - days(1))
) |>
bind_rows(shift_info) |>
mutate(end_time = if_else(
day(start_time) < day(end_time),
floor_date(end_time, "day"),
end_time
))
ggplot(shift_info, aes(start_time, shift,
xend = end_time,
yend = shift, color = shift
)) +
geom_segment(linewidth = 3) +
scale_x_datetime(date_labels = "%H:%M")
<details>
<summary>英文:</summary>
One option would be to do some data wrangling you split the shift extending over two days into two parts or observations. Note that this also requires to reset the date, so I dropped the date from the axis label:
library(ggplot2)
library(lubridate)
library(dplyr)
shift_info <- shift_info |>
filter(day(start_time) < day(end_time)) |>
mutate(
start_time = ceiling_date(start_time, "day"),
across(c(end_time, start_time), ~ .x - days(1))
) |>
bind_rows(shift_info) |>
mutate(end_time = if_else(
day(start_time) < day(end_time),
floor_date(end_time, "day"),
end_time
))
ggplot(shift_info, aes(start_time, shift,
xend = end_time,
yend = shift, color = shift
)) +
geom_segment(linewidth = 3) +
scale_x_datetime(date_labels = "%H:%M")
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/Hv34x.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论