how to fix x-axis and y-axis scale

1.8k views Asked by At
library(ggplot2)
library(scales)
library(gridExtra)

df3 <- structure(list(hours = structure(c(1433995200, 1433998800, 1434002400, 
1434006000, 1434009600, 1434013200, 1434016800, 1434020400, 1434024000, 
1434027600, 1434031200, 1434034800, 1434038400, 1434042000, 1434045600, 
1434049200, 1434052800, 1434056400, 1434060000, 1434063600, 1434067200, 
1434070800, 1434074400, 1434078000), class = c("POSIXct", "POSIXt"
), tzone = ""), x1 = c(25, 28, 33, 30, 32, 35, 31, 29, 28, 34, 
33, 32, 31, 24, 30, 23, 30, 28, 27, 29, 25, 21, 26, 27), x2 = c(25, 
30, 31, 31, 28, 22, 21, 26, 28, 27, 26, 29, 37, 33, 21, 31, 30, 
25, 13, 36, 29, 15, 20, 18), x3 = c(18, 21, 30, 23, 19, 17, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA), x4 = c(NA, NA, NA, NA, 0, 17, 20, 23, 25, 30, 13, 28, 19, 
21, 29, 27, 23, 34, 36, 30, 35, 25, 34, 23)), .Names = c("hours", 
"x1", "x2", "x3", "x4"), row.names = c(NA, -24L), class = "data.frame")

head(df3)
                hours x1 x2 x3 x4
1 2015-06-11 06:00:00 25 25 18 NA
2 2015-06-11 07:00:00 28 30 21 NA
3 2015-06-11 08:00:00 33 31 30 NA
4 2015-06-11 09:00:00 30 31 23 NA
5 2015-06-11 10:00:00 32 28 19  0
6 2015-06-11 11:00:00 35 22 17 17

q1 <- ggplot(df3, aes(x = df3$hours, y = df3$x1)) + geom_bar(stat="identity")+theme(legend.position="none")+
xlab("Time") + ylab("No.Of pieces") +
ggtitle("x1")+
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))

q2 <- ggplot(df3, aes(x = df3$hours, y = df3$x2)) + geom_bar(stat="identity")+theme(legend.position="none")+
xlab("Time") + ylab("No.Of pieces") +
ggtitle("x2")+
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))

q3 <- ggplot(df3, aes(x = df3$hours, y = df3$x3)) + geom_bar(stat="identity")+theme(legend.position="none")+
xlab("Time") + ylab("No.Of pieces") +
ggtitle("x3")+
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))

q4 <- ggplot(df3, aes(x = df3$hours, y = df3$x4)) + geom_bar(stat="identity")+theme(legend.position="none")+
xlab("Time") + ylab("No.Of pieces") +
ggtitle("x4")+
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))

grid.arrange(q1,q2,q3,q4, nrow=1, main = "sample")

enter image description here

sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

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

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

other attached packages:
[1] scales_0.2.4    gridExtra_0.9.1 ggplot2_1.0.1   RPostgreSQL_0.4
[5] DBI_0.3.1      

loaded via a namespace (and not attached):
 [1] Rcpp_0.11.6      digest_0.6.8     MASS_7.3-40      plyr_1.8.2      
 [5] gtable_0.1.2     magrittr_1.5     stringi_0.4-1    reshape2_1.4.1  
 [9] proto_0.3-10     tools_3.2.0      stringr_1.0.0    munsell_0.4.2   
[13] colorspace_1.2-6

I would like to have same x-axis and y-axis scale, whatever the input data is..? if you look at x3 and x4 graph in the above plot - you will see the difference in x-axis and y-axis scale (I know it is because of some data missing in comparison with x1 and x2)

whatever the data is, I would like to see the same x- axis and y-axis scale on all four graphs in the above plot.

thank you.

2

There are 2 answers

0
mts On

This should do what you want. Add

man.breaks = df3$hours[seq(from=1, to=24, by=2)]
man.lims = c(min(df3$hours), max(df3$hours))

and replace scale_x_datetime(...) by

scale_x_datetime(breaks=man.breaks, labels=date_format("%H"), limits = man.lims) 
2
Axeman On

I would use facets.

library(tidyr)
pd <- gather(df, 'xs', 'count', 2:5)

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

library(ggplot2)
library(scales)
library(gridExtra)
ggplot(pd, aes(x = hours, y = count)) + geom_bar(stat = "identity") + theme(legend.position = "none")+
  xlab("Time") + ylab("No.Of pieces") +
  ggtitle("sample")+
  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(~ xs)

enter image description here