Chanti
Chanti

Reputation: 575

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

Reproducible data

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 Script

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)

Output Graph enter image description here

I want to change the x-axis labels. Instead of month date and hour (sep 30 06:00) I want to have only the time (06:00 07:00 and so on..)

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

Here I don't understand why the hours changed from 04:00 to 04:00, it should be 06:00 to 06:00 for every sample as shown below. enter image description here

With the same script, I used to get the desired output in R version 3.2.0 Recently I got a new laptop and installed a latest version of R, then for the same script it is giving an annoying output.

Does any one have any idea? Is it because any bug in R or something else?

Upvotes: 4

Views: 636

Answers (1)

Stibu
Stibu

Reputation: 15907

I think your problem might be connected to time zones. Your dates do not have a proper time zone attached to them:

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

If is set the time zone to "UTC" as follows:

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

and then plot using the same command as you do, I get a plot with x-axis labels 07, 10, 13 etc. To get the x-axis that you show in your desired plot, you also have to change the x-axis breaks:

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

The full code I use (starting with pd already defined`) is thus

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)

which results in the plot

enter image description here

Upvotes: 4

Related Questions