如何在不同面板中绘制多个时间序列?

时间:2015-08-13 10:14:02

标签: r ggplot2

我想生成一个包含多个时间序列的图表和一个在另一个下面的三个面板,对应于它们之间的Employee salary 1 & Employee salary 2 & Ratio。但是,我在ggplot中实现它有一些困难。提前谢谢!

一些数据:

#Employee salary 1

date<-c("2015-08-07","2015-08-08","2015-08-09","2015-08-10","2015-08-11")
GA_old<-c(7268.45,11212.46,12850.15,11313.96,12099.12)
GA_new<-c(7009.32,5665.81,16492.11,4997.29,10963.56)
Opti_old<-c(3582.07,1793.50,5556.42,780.00,10252.15)
Opti_new<-c(3653.33,2335.34,2007.50,4515.50,3598.27)
df<-data.frame(date,GA_old,GA_new,Opti_old,Opti_new)


#Employee salary 2

date<-c("2015-08-07","2015-08-08","2015-08-09","2015-08-10","2015-08-11")
GA_old<-c(7885,8202,11342,9279,9284)
GA_new<-c(7857,8034,11518,9388,9160)
Opti_old<-c(3147,3768,4487,3839,4000)
Opti_new<-c(3084,3669,4456,3922,3971)
df1<-data.frame(date,GA_old,GA_new,Opti_old,Opti_new)

#Ratio

date<-c("2015-08-07","2015-08-08","2015-08-09","2015-08-10","2015-08-11")
gatfrac_new<-c(0.8921115,0.7052290,1.4318554,0.5323061,1.1968952)
gafrac_old<-c(0.9218072,1.3670397,1.1329704,1.2193081,1.3032227)
optfrac_old<-c(1.1382491,0.4759820,1.2383374,0.2031779,2.5630375)
optfrac_new<-c(1.1846077,0.6365059,0.4505162,1.1513259,0.9061370)
df2<-data.frame(date,GA_old,GA_new,Opti_old,Opti_new)

#Plot:

library(ggplot2)
library(grid)
library(gridExtra)



p1<- ggplot(df, aes(date,GA_old,,GA_new,Opti_old,Opti_new)) + geom_line() + scale_y_reverse() + labs(x="Date") + scale_x_continuous(breaks = seq(1000,2000,200), limits = c(1000,2000))

p2<- ggplot(df1, aes(date, GA_old,,GA_new,Opti_old,Opti_new)) + geom_line() + labs(x=NULL) + scale_x_continuous(breaks = seq(1000,2000,200), limits = c(1000,2000)) + theme(axis.text.x=element_blank())

p3<- ggplot(df2, aes(date,optfrac_new,optfrac_old,gafrac_old, gatfrac_new )) + geom_line() + labs(x=NULL) + scale_x_continuous(breaks = seq(1000,2000,200), limits = c(1000,2000)) + theme(axis.text.x=element_blank())


#Shadowing some areas
rects<- data.frame(x1=c(2015-08-07,2015-08-09),x2=c(2015-08-08,2015-08-08),y1=c(0,0),y2=c(100,100))

xquiet <- scale_x_continuous("", breaks = NULL)
yquiet <- scale_y_continuous("", breaks = NULL)
bgquiet<- theme(panel.background = element_rect(fill = "transparent", colour = NA))
plotquiet<- theme(plot.background = element_rect(fill = "transparent", colour = NA))
quiet <- list(xquiet, yquiet, bgquiet, plotquiet)



prects<-ggplot(rects,aes(xmin=x1,xmax=x2,ymin=y1,ymax=y2))+geom_rect(alpha=0.1,fill="blue") + coord_cartesian(xlim = c(1000, 2000)) + quiet

#Arranging
pushViewport(viewport(layout = grid.layout(2, 1)))
vplayout <- function(x, y) 
viewport(layout.pos.row = x, layout.pos.col = y)
#arrange time series

print(p2, vp = vplayout(1, 1))
print(p1, vp = vplayout(2, 1))
#arrange shadows
print(prects, vp=vplayout(1:2,1))

1 个答案:

答案 0 :(得分:4)

不幸的是我无法运行您的编码。但是从问题和代码阅读中,我想你想要一个每个数据帧有一个面板,每个面板有多行的情节。

这是我的解决方案,也许有帮助:

library(dplyr)
library(tidyr)
library(ggplot2)

# building extra var to distinct dataframes
df$df = "Employee salary 1"
df1$df = "Employee salary 2"
df2$df = "Ratio"

# binding df together, rearrange and plot
bind_rows(df, df1, df2) %>%
    gather(variable, value, -c(date, df)) %>%
    ggplot(aes(x=as.Date(date), y=value, group=variable))  +
    geom_line(aes(colour = variable)) +
    facet_wrap(~df, ncol = 1, scales = "free") +
    scale_x_date() + xlab("")

结果如下: enter image description here