使用ggplot 试试这个:
ts1 <- rnorm(100) # randomly generated values for times series
ts2 <- rnorm(100)
ts3 <- rnorm(100)
ts4 <- rnorm(100)
library(TTR)
df <- data.frame(time=rep(1:100, 8),
id=as.factor(rep(1:8, each=100)), id1=as.factor(rep(1:4, each=200)),
type=as.factor(rep(rep(1:2, each=100),4)),
value=c(ts1, SMA(ts1), ts2, SMA(ts2), ts3, SMA(ts3), ts4, SMA(ts4)))
library(ggplot2)
ggplot(df, aes(time, value, col=type, group=id)) +
geom_line() + facet_wrap(~id1, ncol=1) +
scale_color_manual(values=c('green', 'red'))+
guides(color=FALSE) + theme_bw() + theme(strip.text = element_blank())
如果您想为构面设置不同的 y 标签,请尝试以下操作:
library(grid)
library(gridExtra)
grid.arrange(ggplot(data.frame(time=rep(1:100, 2), id=as.factor(rep(1:2, each=100)),
type=as.factor(rep(1:2, each=100)),
ts1=c(ts1, SMA(ts1))), aes(time, ts1, col=type, group=id)) +
geom_line() + scale_color_manual(values=c('green', 'red')) + guides(color=FALSE) +
theme_bw() + theme(axis.text.x = element_blank(), axis.ticks = element_blank()) + xlab(''),
ggplot(data.frame(time=rep(1:100, 2), id=as.factor(rep(1:2, each=100)),
type=as.factor(rep(1:2, each=100)),
ts2=c(ts2, SMA(ts2))), aes(time, ts2, col=type, group=id)) +
geom_line() + scale_color_manual(values=c('green', 'red')) + guides(color=FALSE) +
theme_bw() + theme(axis.text.x = element_blank(), axis.ticks = element_blank()) + xlab(''),
ggplot(data.frame(time=rep(1:100, 2), id=as.factor(rep(1:2, each=100)),
type=as.factor(rep(1:2, each=100)),
ts3=c(ts3, SMA(ts3))), aes(time, ts3, col=type, group=id)) +
geom_line() + scale_color_manual(values=c('green', 'red')) + guides(color=FALSE) +
theme_bw() + theme(axis.text.x = element_blank(), axis.ticks = element_blank()) + xlab(''),
ggplot(data.frame(time=rep(1:100, 2), id=as.factor(rep(1:2, each=100)),
type=as.factor(rep(1:2, each=100)),
ts4=c(ts4, SMA(ts4))), aes(time, ts4, col=type, group=id)) +
geom_line() + scale_color_manual(values=c('green', 'red')) + guides(color=FALSE) + theme_bw(), ncol=1)