ggplot仅用于POSIXct日期时间时的时间比例尺

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

ggplot scale for time of date only, when using POSIXct datetimes

问题

在ggplot2中,我有一个关于将POSIXct日期时间转换为轴上的时刻的适当刻度的问题。考虑以下代码:

library(tidyverse)
library(lubridate)
library(hms)
library(patchwork)

test <- tibble(
  dates = c(ymd_hms("2022-01-01 6:00:00"),
            ymd_hms("2023-01-01 19:00:00")),
  x = c(1, 2),
  hms_dates = as_hms(dates)
)

plot1 <- ggplot(test) + geom_point(aes(x = x, y = dates)) +
  scale_y_time()

plot2 <- ggplot(test) + geom_point(aes(x = x, y = hms_dates)) +
  scale_y_time()

plot1 + plot2
  1. 图1的y轴包括日期和时间,而图2只显示了一天中的时间。这就是我想要的!我想生成类似图2的图像,而不必使用hms::as_hms方法。这似乎意味着我可以找不到的scale_y_datetime(或类似的)的一些选项。我欢迎建议。

  2. 有人有关于如何在scale_*_time中使用limits选项的示例,或者(参见问题#1)有关于如何为指定一天内的小时的scale_y_datetime设置限制的示例,例如.. limits(c(8,22)) 预测性地失败。

英文:

In ggplot2, I have a question about appropriate scales for making POSIXct datetimes into time-of-day in an axis. Consider:

library(tidyverse)
library(lubridate)
library(hms)
library(patchwork)

test &lt;- tibble(
  dates = c(ymd_hms(&quot;2022-01-01 6:00:00&quot;),
            ymd_hms(&quot;2023-01-01 19:00:00&quot;)),
  x = c(1, 2),
  hms_dates = as_hms(dates)
)

plot1 &lt;- ggplot(test) + geom_point(aes(x = x, y = dates)) +
  scale_y_time()

plot2 &lt;- ggplot(test) + geom_point(aes(x = x, y = hms_dates)) +
  scale_y_time()

plot1 + plot2
  1. Plot 1 y axis includes dates and time, but Plot 2 shows just time of day. That's what I want! I'd like to generate plot 2 like images without having to use the hms::as_hms approach. This seems to imply some options for scale_y_datetime (or similar) that I can't discover. I'd welcome suggestions.

  2. Does someone have an example of how to use the limits option in scale_*_time, or (see question #1) limits for a scale_y_datetime that specifies hours within the day, e.g. .. limits(c(8,22)) predictably fails.

答案1

得分: 1

对于你的第二个问题,在处理日期、日期时间或时间时,你必须将限制和/或分隔设置为日期、日期时间或时间,即使用 limits = as_hms(c("8:00:00", "22:00:00"))

library(tidyverse)
library(lubridate)
library(hms)

ggplot(test) + geom_point(aes(x = x, y = hms_dates)) +
  scale_y_time(limits = as_hms(c("8:00:00", "22:00:00")))
#&gt; 警告:删除了包含缺失值的 1 行 (`geom_point()`)。

关于你的第一个问题。据我所知,无法通过 scale_..._datetime 来实现。如果你只想显示日期的时间部分,那么将其转换为 hms 对象是最简单的方法。当然,你也可以通过 date_labels 参数来设置要显示的单位作为轴标签,例如,date_labels="%H:%M:%S" 以仅显示每天的时间部分。然而,由于你的 dates 变量仍然是日期时间,刻度、分隔和限制仍然会反映这一点,即你只更改了标签的格式,对于你的示例数据,最终会得到一个显示相同时间的坐标轴分隔,即一天的开始。

ggplot(test) + geom_point(aes(x = x, y = dates)) +
  scale_y_datetime(date_labels = "%H:%M:%S")

请注意,上述内容只是翻译,不包含任何额外的信息或回答。

英文:

For your second question, when dealing with dates or datetimes or times you have to set the limits and/or breaks as dates, datetimes or times too, i.e. use limits = as_hms(c(&quot;8:00:00&quot;, &quot;22:00:00&quot;):

library(tidyverse)
library(lubridate)
library(hms)

ggplot(test) + geom_point(aes(x = x, y = hms_dates)) +
  scale_y_time(limits = as_hms(c(&quot;8:00:00&quot;, &quot;22:00:00&quot;)))
#&gt; Warning: Removed 1 rows containing missing values (`geom_point()`).

ggplot仅用于POSIXct日期时间时的时间比例尺<!-- -->

Concerning your first question. TBMK this could not be achieved via scale_..._datetime. And if you just want to show the time part of your dates then converting to an has object is IMHO the easiest way to achieve that. You could of course set the units to be shown as axis text via the date_labels argument, e.g. date_labels=&quot;%H:%M:%S&quot; to show only the time of day. However, as your dates variable is still a datetime the scale, breaks and limits will still reflect that, i.e. you only change the format of the labels and for your example data you end up with an axis showing the same time for each break, i.e. the start of the day.

ggplot(test) + geom_point(aes(x = x, y = dates)) +
  scale_y_datetime(date_labels = &quot;%H:%M:%S&quot;)

ggplot仅用于POSIXct日期时间时的时间比例尺<!-- -->

huangapple
  • 本文由 发表于 2023年2月10日 12:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406853.html
匿名

发表评论

匿名网友

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

确定