如何使geom_segment在达到限制后“重新开始”

huangapple go评论65阅读模式
英文:

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 &lt;- tibble(
  shift = c(&quot;Shift 1&quot;, &quot;Shift 2&quot;, &quot;Shift 3&quot;),
  start_time = ymd_hms(c(&quot;2023-03-03 08:00:00&quot;, &quot;2023-03-03 08:00:00&quot;, &quot;2023-03-03 16:00:00&quot;)),
  end_time = ymd_hms(c(&quot;2023-03-03 20:00:00&quot;, &quot;2023-03-03 16:00:00&quot;, &quot;2023-03-04 04:00:00&quot;))
)

答案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] &lt;- 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 = &quot;gold&quot;, size = 3) +
  scale_x_continuous(limits = c(0, 23)) +
  labs(x = &quot;Hour of the day&quot;, y = &quot;&quot;) +
  theme_classic()

如何使geom_segment在达到限制后“重新开始”

答案2

得分: 0

一种选择是进行一些数据处理,将跨越两天的班次拆分为两个部分或观察结果。请注意,这还需要重置日期,所以我从坐标轴标签中删除了日期:

shift_info &lt;- shift_info |&gt;
  filter(day(start_time) &lt; day(end_time)) |&gt;
  mutate(
    start_time = ceiling_date(start_time, &quot;day&quot;),
    across(c(end_time, start_time), ~ .x - days(1))
  ) |&gt;
  bind_rows(shift_info) |&gt;
  mutate(end_time = if_else(
    day(start_time) &lt; day(end_time),
    floor_date(end_time, &quot;day&quot;),
    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 = &quot;%H:%M&quot;)

如何使geom_segment在达到限制后“重新开始”


<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>



huangapple
  • 本文由 发表于 2023年3月4日 02:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定