简体   繁体   English

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

[英]How to plot multiple time series within different panels?

I would like to produce a graph with multiple time series and three panels one below the other corresponding to Employee salary 1 & Employee salary 2 & Ratio between them. 我想生成一个具有多个时间序列和三个面板的图,一个在另一个下方,分别对应于Employee salary 1 & Employee salary 2 & Ratio及其之间的Employee salary 1 & Employee salary 2 & Ratio However, I have some difficulties to implement that in ggplot. 但是,我很难在ggplot中实现它。 Thank you in advance! 先感谢您!

Some data: 一些数据:

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

Unfortunately I couldn't get your coding running. 不幸的是,我无法运行您的编码。 But from the question and code reading, I guess you want a plot with one panel per dataframe and multiple lines per panel. 但是从问题和代码阅读中,我想您想要一个图表,每个数据帧有一个面板,每个面板有多条线。

This is my solution for this, maybe it helps: 这是我的解决方案,也许有帮助:

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

The result looks like this: 结果看起来像这样: 在此处输入图片说明

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

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