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轴标签。而不是月份日期和时间(sep 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中的任何错误还是别的什么?
答案 0 :(得分:4)
我认为您的问题可能与时区有关。您的日期没有附加适当的时区:
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)
导致情节