gpt4 book ai didi

r - ggplot2 scale_x_datetime 造成烦恼

转载 作者:行者123 更新时间:2023-12-02 20:02:12 27 4
gpt4 key购买 nike

sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] WriteXLS_3.6.1 gdata_2.17.0 tidyr_0.3.1 scales_0.3.0 gridExtra_2.0.0 ggplot2_1.0.1 RPostgreSQL_0.4 DBI_0.3.1

loaded via a namespace (and not attached):
[1] Rcpp_0.12.1 gtools_3.5.0 assertthat_0.1 dplyr_0.4.3 digest_0.6.8 MASS_7.3-43 R6_2.1.1 grid_3.2.2
[9] plyr_1.8.3 gtable_0.1.2 magrittr_1.5 stringi_0.5-5 lazyeval_0.1.10 reshape2_1.4.1 proto_0.3-10 tools_3.2.2
[17] stringr_1.0.0 munsell_0.4.2 parallel_3.2.2 colorspace_1.2-6

可重复的数据

pd <- structure(list(hours = structure(c(1443585600, 1443589200, 1443592800, 
1443596400, 1443600000, 1443603600, 1443607200, 1443610800, 1443614400,
1443618000, 1443621600, 1443625200, 1443628800, 1443632400, 1443636000,
1443639600, 1443643200, 1443646800, 1443650400, 1443654000, 1443657600,
1443661200, 1443664800, 1443668400, 1443585600, 1443589200, 1443592800,
1443596400, 1443600000, 1443603600, 1443607200, 1443610800, 1443614400,
1443618000, 1443621600, 1443625200, 1443628800, 1443632400, 1443636000,
1443639600, 1443643200, 1443646800, 1443650400, 1443654000, 1443657600,
1443661200, 1443664800, 1443668400, 1443585600, 1443589200, 1443592800,
1443596400, 1443600000, 1443603600, 1443607200, 1443610800, 1443614400,
1443618000, 1443621600, 1443625200, 1443628800, 1443632400, 1443636000,
1443639600, 1443643200, 1443646800, 1443650400, 1443654000, 1443657600,
1443661200, 1443664800, 1443668400, 1443585600, 1443589200, 1443592800,
1443596400, 1443600000, 1443603600, 1443607200, 1443610800, 1443614400,
1443618000, 1443621600, 1443625200, 1443628800, 1443632400, 1443636000,
1443639600, 1443643200, 1443646800, 1443650400, 1443654000, 1443657600,
1443661200, 1443664800, 1443668400, 1443585600, 1443589200, 1443592800,
1443596400, 1443600000, 1443603600, 1443607200, 1443610800, 1443614400,
1443618000, 1443621600, 1443625200, 1443628800, 1443632400, 1443636000,
1443639600, 1443643200, 1443646800, 1443650400, 1443654000, 1443657600,
1443661200, 1443664800, 1443668400), class = c("POSIXct", "POSIXt"
), tzone = ""), samples = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L), .Label = c("sample 1", "sample 2", "sample 3", "sample 4",
"sample 5"), class = "factor"), count = c(12, 21, 28, 27, 8,
13, 24, 16, 31, 32, 22, 33, 29, 30, 31, 32, 28, 20, 31, 30, 31,
27, 21, 33, 21, 32, 28, 28, 27, 29, 33, 33, 17, 26, 23, 12, 28,
38, 37, 26, 20, 24, 30, 35, 27, 22, 30, 30, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 2, NA, NA, 1, 23, 31, 15, 11, 29, 33, 31,
33, 30, 22, 31, 31, 30, 19, 26, 34, 31, 29, 30, 24, 28, 26, 27,
31, 31, 32, 24, 29, 24, 28, 28, 29, 18, 35, 34, 32, 27, 32, 34,
37, 37, 30, 30, 28, 28, 33, 20, 22, 34, 22, 25, 28, 20, 25, 23,
24, 20, 25)), row.names = c(NA, -120L), .Names = c("hours", "samples",
"count"), class = "data.frame")

head(pd)
# hours samples count
# 1 2015-09-30 06:00:00 sample 1 12
# 2 2015-09-30 07:00:00 sample 1 21
# 3 2015-09-30 08:00:00 sample 1 28
# 4 2015-09-30 09:00:00 sample 1 27
# 5 2015-09-30 10:00:00 sample 1 8
# 6 2015-09-30 11:00:00 sample 1 13

R 脚本

library(ggplot2)
library(gridExtra)
library(scales)
blue.bold.italic.16.text <- element_text(face = "bold", color = "black", size = 12)

q <- ggplot(pd, aes(x = hours, y = count)) + geom_bar(stat = "identity") + theme(legend.position = "none")+
xlab("Time") + ylab("No.Of collected samples") +
ggtitle("Sample Collection")+
scale_y_continuous(breaks=seq(0,45, by = 5))+
theme(axis.text = blue.bold.italic.16.text) +
theme(axis.text.x=element_text(angle=90))+
facet_grid(~ samples)

输出图 enter image description here

我想更改 x 轴标签。我只想知道时间(06:00 07:00 等等..),而不是月份日期和时间(9 月 30 日 06:00)

q <- ggplot(pd, aes(x = hours, y = count)) + geom_bar(stat = "identity") + theme(legend.position = "none")+
xlab("Time") + ylab("No.Of collected samples") +
ggtitle("Sample Collection")+
scale_y_continuous(breaks=seq(0,45, by = 5))+
theme(axis.text = blue.bold.italic.16.text) +
scale_x_datetime(breaks=date_breaks("3 hour"),minor_breaks=date_breaks("3 hour"),labels=date_format("%H")) +
theme(axis.text.x=element_text(angle=0))+
facet_grid(~ samples)

enter image description here

这里我不明白为什么时间从 04:00 变成 04:00,每个样本应该是 06:00 到 06:00,如下所示。 enter image description here

使用相同的脚本,我曾经在 R 版本 3.2.0 中获得所需的输出最近我买了一台新笔记本电脑并安装了最新版本的 R,然后对于相同的脚本,它给出了令人讨厌的输出。

有人知道吗?是因为 R 中的任何错误还是其他原因?

最佳答案

我认为您的问题可能与时区有关。您的日期没有附加正确的时区:

lubridate::tz(pd$hours)
## [1] ""

如果将时区设置为“UTC”,如下所示:

lubridate::tz(pd$hours) <- "UTC"

然后使用与您相同的命令进行绘图,我得到一个带有 x 轴标签 071013 等的绘图.要获得在所需绘图中显示的 x 轴,您还必须更改 x 轴分隔符:

scale_x_datetime(breaks=date_breaks("2 hour"),minor_breaks=date_breaks("2 hour"),labels=date_format("%H"))

我使用的完整代码(从pd已经定义`开始)是这样的

lubridate::tz(pd$hours) <- "UTC"

q <- ggplot(pd, aes(x = hours, y = count)) + geom_bar(stat = "identity") + theme(legend.position = "none")+
xlab("Time") + ylab("No.Of collected samples") +
ggtitle("Sample Collection")+
scale_y_continuous(breaks=seq(0,45, by = 5))+
theme(axis.text = blue.bold.italic.16.text) +
scale_x_datetime(breaks=date_breaks("2 hour"),minor_breaks=date_breaks("2 hour"),labels=date_format("%H")) +
theme(axis.text.x=element_text(angle=0))+
facet_grid(~ samples)

这会导致绘图

enter image description here

关于r - ggplot2 scale_x_datetime 造成烦恼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32887239/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com