ggplot2 scale_x_datetime 制造烦恼
ggplot2 scale_x_datetime creating annoyance
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)
输出图
我想更改 x 轴标签。而不是月份日期和时间(9 月 30 日 06:00)我只想有时间(06:00 07: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)
这里我不明白为什么小时数从04:00变成了04:00,每个样本应该是06:00到06:00如下图。
使用相同的脚本,我曾经在 R 版本中获得所需的输出 3.2.0
最近我买了一台新笔记本电脑并安装了最新版本的 R,然后对于同一个脚本,它给出了一个烦人的输出。
有人知道吗?是因为 R 中的任何错误还是其他原因?
我认为您的问题可能与时区有关。您的日期没有附加正确的时区:
lubridate::tz(pd$hours)
## [1] ""
如果设置时区为"UTC"
如下:
lubridate::tz(pd$hours) <- "UTC"
然后使用与您相同的命令进行绘图,我得到一个带有 x 轴标签 07
、10
、13
等的绘图。要获取 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)
这导致情节
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)
输出图
我想更改 x 轴标签。而不是月份日期和时间(9 月 30 日 06:00)我只想有时间(06:00 07: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)
这里我不明白为什么小时数从04:00变成了04:00,每个样本应该是06:00到06:00如下图。
使用相同的脚本,我曾经在 R 版本中获得所需的输出 3.2.0
最近我买了一台新笔记本电脑并安装了最新版本的 R,然后对于同一个脚本,它给出了一个烦人的输出。
有人知道吗?是因为 R 中的任何错误还是其他原因?
我认为您的问题可能与时区有关。您的日期没有附加正确的时区:
lubridate::tz(pd$hours)
## [1] ""
如果设置时区为"UTC"
如下:
lubridate::tz(pd$hours) <- "UTC"
然后使用与您相同的命令进行绘图,我得到一个带有 x 轴标签 07
、10
、13
等的绘图。要获取 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)
这导致情节