使用 %p 将 24 小时制转换为 12 小时制以查看 AM/PM:R 中 OS 的问题
Transforming 24 hour time into 12-hour format with %p to see AM/PM: issue with OS in R
对于一份期刊,我被要求将我的 24 小时 x 轴转换为 AM/PM 格式的 12 小时格式。我将 R 与 ggplot2 包一起使用来创建我的绘图。为此,我确实使用了允许查看 AM/PM 的 %p 选项。当我使用它时,它根本不起作用。在网上查看并与同事讨论后,我意识到 %p 不是在我的 Linux 机器上工作,而是在我的 Mac 机器上工作。因此,我将非常感谢任何反馈为什么。
这是一个代表:
library(tidyverse)
library(lubridate)
library(ggplot2)
df <- tibble::tibble(ID = sample(seq(1, 3, 1), 289, replace = T),
time = seq(as.POSIXct("2017-01-01", tz = "UTC"),
as.POSIXct("2017-01-02", tz = "UTC"),
by = "5 min"),
date = lubridate::as_date(time),
outcome = sample(seq(70, 120, 1), 289, replace = T))
ggplot(df, aes(x = time,
y = outcome,
group = ID)) +
geom_line(aes(color = factor(ID))) +
scale_y_continuous(limits = c(0, 150)) +
scale_x_datetime(date_labels = ("%I %p"),
timezone = "GMT",
date_breaks = "4 hours",
expand = c(0, 0))
Mac机器上的输出是:
Linux 机器上的输出是:
最后,两者的 sessionInfo():
- Mac:
sessionInfo(package = NULL)
# R version 3.5.3 (2019-03-11)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS High Sierra 10.13.6
#
# Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
#
# locale:
# [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] ggplot2_3.2.1 lubridate_1.7.4
#
# loaded via a namespace (and not attached):
# [1] Rcpp_1.0.3 rstudioapi_0.10 magrittr_1.5 tidyselect_0.2.5 munsell_0.5.0 colorspace_1.4-1 R6_2.4.1 rlang_0.4.2 stringr_1.4.0
# [10] dplyr_0.8.1 tools_3.5.3 grid_3.5.3 gtable_0.3.0 withr_2.1.2 digest_0.6.23 lazyeval_0.2.2 assertthat_0.2.1 tibble_2.1.3
# [19] lifecycle_0.1.0 crayon_1.3.4 purrr_0.3.2 farver_2.0.1 glue_1.3.1 labeling_0.3 stringi_1.4.3 compiler_3.5.3 pillar_1.4.2
# [28] scales_1.1.0 pkgconfig_2.0.3
- Linux:
sessionInfo(package = NULL)
# R version 3.6.2 (2019-12-12)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 18.04.4 LTS
#
# Matrix products: default
# BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
# LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
#
# Random number generation:
# RNG: Mersenne-Twister
# Normal: Inversion
# Sample: Rounding
#
# locale:
# [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=de_CH.UTF-8
# [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=de_CH.UTF-8 LC_MESSAGES=en_US.UTF-8
# [7] LC_PAPER=de_CH.UTF-8 LC_NAME=C LC_ADDRESS=C
# [10] LC_TELEPHONE=C LC_MEASUREMENT=de_CH.UTF-8 LC_IDENTIFICATION=C
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] ggplot2_3.2.1 lubridate_1.7.4
#
# loaded via a namespace (and not attached):
# [1] Rcpp_1.0.3 rstudioapi_0.10 magrittr_1.5 tidyselect_0.2.5 munsell_0.5.0
# [6] colorspace_1.4-1 R6_2.4.1 rlang_0.4.4 stringr_1.4.0 dplyr_0.8.3
# [11] tools_3.6.2 grid_3.6.2 gtable_0.3.0 withr_2.1.2 digest_0.6.23
# [16] lazyeval_0.2.2 assertthat_0.2.1 tibble_2.1.3 lifecycle_0.1.0 crayon_1.3.4
# [21] purrr_0.3.3 farver_2.0.2 glue_1.3.1 labeling_0.3 stringi_1.4.5
# [26] compiler_3.6.2 pillar_1.4.3 scales_1.1.0 pkgconfig_2.0.3
提前感谢您的指导!
您的问题似乎出在以下设置上:LC_TIME=de_CH.UTF-8
。我假设瑞士德语时间格式不知道 AM/PM (因为德语不使用那个 nonese 而是正确的 24h 时间)。我看到您有可用的语言环境可以处理这个问题。所以你必须为时间区域设置其他之一:
Sys.setlocale("LC_TIME", "en_US.UTF-8")
测试:
format.Date(df$time, "%I %p")
然后看看您的其余代码现在是否可以正常工作。您必须在每次 R
重新启动后设置语言环境或进行切换 .
对于一份期刊,我被要求将我的 24 小时 x 轴转换为 AM/PM 格式的 12 小时格式。我将 R 与 ggplot2 包一起使用来创建我的绘图。为此,我确实使用了允许查看 AM/PM 的 %p 选项。当我使用它时,它根本不起作用。在网上查看并与同事讨论后,我意识到 %p 不是在我的 Linux 机器上工作,而是在我的 Mac 机器上工作。因此,我将非常感谢任何反馈为什么。
这是一个代表:
library(tidyverse)
library(lubridate)
library(ggplot2)
df <- tibble::tibble(ID = sample(seq(1, 3, 1), 289, replace = T),
time = seq(as.POSIXct("2017-01-01", tz = "UTC"),
as.POSIXct("2017-01-02", tz = "UTC"),
by = "5 min"),
date = lubridate::as_date(time),
outcome = sample(seq(70, 120, 1), 289, replace = T))
ggplot(df, aes(x = time,
y = outcome,
group = ID)) +
geom_line(aes(color = factor(ID))) +
scale_y_continuous(limits = c(0, 150)) +
scale_x_datetime(date_labels = ("%I %p"),
timezone = "GMT",
date_breaks = "4 hours",
expand = c(0, 0))
Mac机器上的输出是:
Linux 机器上的输出是:
最后,两者的 sessionInfo():
- Mac:
sessionInfo(package = NULL)
# R version 3.5.3 (2019-03-11)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS High Sierra 10.13.6
#
# Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
#
# locale:
# [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] ggplot2_3.2.1 lubridate_1.7.4
#
# loaded via a namespace (and not attached):
# [1] Rcpp_1.0.3 rstudioapi_0.10 magrittr_1.5 tidyselect_0.2.5 munsell_0.5.0 colorspace_1.4-1 R6_2.4.1 rlang_0.4.2 stringr_1.4.0
# [10] dplyr_0.8.1 tools_3.5.3 grid_3.5.3 gtable_0.3.0 withr_2.1.2 digest_0.6.23 lazyeval_0.2.2 assertthat_0.2.1 tibble_2.1.3
# [19] lifecycle_0.1.0 crayon_1.3.4 purrr_0.3.2 farver_2.0.1 glue_1.3.1 labeling_0.3 stringi_1.4.3 compiler_3.5.3 pillar_1.4.2
# [28] scales_1.1.0 pkgconfig_2.0.3
- Linux:
sessionInfo(package = NULL)
# R version 3.6.2 (2019-12-12)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 18.04.4 LTS
#
# Matrix products: default
# BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
# LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
#
# Random number generation:
# RNG: Mersenne-Twister
# Normal: Inversion
# Sample: Rounding
#
# locale:
# [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=de_CH.UTF-8
# [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=de_CH.UTF-8 LC_MESSAGES=en_US.UTF-8
# [7] LC_PAPER=de_CH.UTF-8 LC_NAME=C LC_ADDRESS=C
# [10] LC_TELEPHONE=C LC_MEASUREMENT=de_CH.UTF-8 LC_IDENTIFICATION=C
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] ggplot2_3.2.1 lubridate_1.7.4
#
# loaded via a namespace (and not attached):
# [1] Rcpp_1.0.3 rstudioapi_0.10 magrittr_1.5 tidyselect_0.2.5 munsell_0.5.0
# [6] colorspace_1.4-1 R6_2.4.1 rlang_0.4.4 stringr_1.4.0 dplyr_0.8.3
# [11] tools_3.6.2 grid_3.6.2 gtable_0.3.0 withr_2.1.2 digest_0.6.23
# [16] lazyeval_0.2.2 assertthat_0.2.1 tibble_2.1.3 lifecycle_0.1.0 crayon_1.3.4
# [21] purrr_0.3.3 farver_2.0.2 glue_1.3.1 labeling_0.3 stringi_1.4.5
# [26] compiler_3.6.2 pillar_1.4.3 scales_1.1.0 pkgconfig_2.0.3
提前感谢您的指导!
您的问题似乎出在以下设置上:LC_TIME=de_CH.UTF-8
。我假设瑞士德语时间格式不知道 AM/PM (因为德语不使用那个 nonese 而是正确的 24h 时间)。我看到您有可用的语言环境可以处理这个问题。所以你必须为时间区域设置其他之一:
Sys.setlocale("LC_TIME", "en_US.UTF-8")
测试:
format.Date(df$time, "%I %p")
然后看看您的其余代码现在是否可以正常工作。您必须在每次 R
重新启动后设置语言环境或进行切换