ゴールggplot2:規模_ * _時間のフォーマットが間違って
使用ggplot2::scale_*_time
(ggplot2
の最新版での)特定の方法で時間軸をフォーマットします。 format = "%R"
とggplot2::scale_*_time
を使用して
最小Reprex
# Example data
tib1 <- tibble::tibble(
x = 1:10,
y = lubridate::as.duration(seq(60, 600, length.out = 10))
)
は動作しません - 軸は%H:%M:%S
を使用してフォーマットされています
# This doesn't work
ggplot2::ggplot(tib1) +
ggplot2::geom_point(ggplot2::aes(x,y)) +
ggplot2::scale_y_time(labels = scales::date_format(format = "%R"))
しかし、私は時間変数(y
)を追加することができます任意の日付を入力し、結果のdatetime
オブジェクトをフォーマットします。
# This works
tib2 <- tib1 %>%
dplyr::mutate(z = lubridate::ymd_hms("2000-01-01 00:00:00") + y)
ggplot2::ggplot(tib2) +
ggplot2::geom_point(ggplot2::aes(x,z)) +
ggplot2::scale_y_datetime(labels = scales::date_format(format = "%R"))
軸を正しくフォーマットするために私の時間に任意の日付を追加したくないのは明らかです(これは、ggplot2::scale_*_time
が導入されてからは避けることを望みます)。