简体   繁体   English

如何固定x轴和y轴比例

[英]how to fix x-axis and y-axis scale

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")

在此处输入图片说明

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..? 无论输入数据是什么,我都希望具有相同的x轴和y轴比例。 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 ) 如果您在上图中查看x3和x4图-您将看到x轴和y轴比例的差异(我知道这是因为与x1x2相比缺少一些数据)

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. 无论数据是什么,我都希望在上图中的所有四个图表上看到相同的x轴和y轴比例。

thank you. 谢谢。

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(...)替换为

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

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM