【问题标题】:Multiple lines on multiple plots in RR中多个图上的多条线
【发布时间】:2017-01-11 11:32:25
【问题描述】:

我正在寻找一种解决方法来为同一图上的不同时间序列绘制平滑时间序列。我知道我可以使用par(mfrow(c(4,1)) 在同一张图上绘制多个图。但我希望图表具有相同的 X 轴。所以我有类似下面的东西。

我在使用 cbind 组合数据集后进行了绘图。 现在我想在同一张图中有对应于每个时间序列的平滑图。通过使用par,我得到以下结果。

但我不希望到处都有 x 轴的标签。我在每个情节中都使用了线条来获得上述结果。

par(mfrow=c(4,1))
plot.ts(ts1,col="green")
lines(SMA(ts1,n=10),col="red")
plot.ts(ts2,col="green")
lines(SMA(ts2,n=10),col="red")
plot.ts(ts3,col="green")
lines(SMA(ts3,n=10),col="red")
plot.ts(ts4,col="green")
lines(SMA(ts4,n=10),col="red")

有什么方法可以在 R 中使用 plot 或 ggplot 获得所需的结果?

【问题讨论】:

  • 为什么这个问题被否决了??
  • 在基础 R 中,尝试 plot(1:10, 1:10, xlab="") 删除 x 轴标签或 plot(1:10, 1:10, xlab="", xaxt="n") 删除标签和刻度线标签。
  • 我可以删除标签以及 X 轴的索引。但我无法将 4 个图一起作为一个图

标签: r plot


【解决方案1】:

使用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)

【讨论】:

  • 是的。我明白了..即使我还没有完全理解它,它的工作原理。我不习惯使用 ggplot()
  • 只是出于好奇,难道不能用plot.ts吗??
  • @Vini 应该是可能的,但 ggplot 在美学上看起来要好得多。
  • 哦,是的.. 更多错误.. 我不能将 y 轴的标签设置为 ts1、ts2、ts3 和 ts4.. 类似于 plot.ts?
猜你喜欢
  • 1970-01-01
  • 2021-01-27
  • 2023-03-30
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 2017-09-06
相关资源
最近更新 更多