在R中用几行代码创建多个图表。

huangapple go评论72阅读模式
英文:

Creating multiple plots with few lines of code in R

问题

我想在同一张图中绘制多个时间序列变量,以便我可以看到时间序列滞后线如何对齐。我将有一些不同的数据组的不同图,每个图将代表不同的数据组,尽管每个图将具有相同名称的变量。

我可以逐个变量地进行操作,每次一个变量。但我有大约一百个变量。

以下是我的数据集示例,其中lag是滞后点(来自ccf()),每个var是在该滞后点上不同变量的自相关(ACF)。

sampledf <- data.frame(
  lag = -10:10,
  var1 = rnorm(21),
  var2 = rnorm(21),
  var3 = rnorm(21)
)

现在,我可以相对容易地像这样绘制它们:

plot(sampledf$lag,
     sampledf$var1, 
     type = "l",
     col = 1,
     xlab = "Lag",
     ylab = "ACF")
lines(sampledf$lag,
     sampledf$var2,
      type = "l",
      col = 2)
lines(sampledf$lag,
     sampledf$var3,
      type = "l",
      col = 3)
legend("topright",                          
       c("Var1", "Var2", "Var3"),
       lty = 1,
       col = 1:3)

但然后我要手动处理每个变量。如果我想以不同的方式查看相关性,例如,一个图中包含sampledf1sampledf20中的每个var1,我将不得不重新开始。

是否有办法以更少的代码行自动化这一过程?
这已经超出了我的R编程水平,但我意识到这可能与函数和其他内容有关(R在我的工作中主要是一个"统计"工具)。

如果有一种完全不同(但更容易)的方法来实现这一目标,我也愿意尝试不同的函数来以不同方式查看交叉相关性。

英文:

I want to plot multiple time-series variables in the same plot so that I can see how the time series lags line up. I will have a few different plots for different groups of data, and each of those plots will represent a different group of data, although each plot will have variables with the same name.

I could do it individually, one variable at a time. But I have like a hundred variables.

Here is an example of my dataset where lag is the lag point (from the ccf()) and each var is the ACF of a different variable at that lag point.

sampledf &lt;- data.frame(
  lag = -10:10,
  var1 = rnorm(21),
  var2 = rnorm(21),
  var3 = rnorm(21)
)

Now I could plot them fairly easily like this:

plot(sampledf$lag,
     sampledf$var1, 
     type = &quot;l&quot;,
     col = 1,
     xlab = &quot;Lag&quot;,
     ylab = &quot;ACF&quot;)
lines(sampledf$lag,
     sampledf$var2,
      type = &quot;l&quot;,
      col = 2)
lines(sampledf$lag,
     sampledf$var3,
      type = &quot;l&quot;,
      col = 3)
legend(&quot;topright&quot;,                          
       c(&quot;Var1&quot;, &quot;Var2&quot;, &quot;Var3&quot;),
       lty = 1,
       col = 1:3)

But then I am doing each variable manually. And if I wanted to view the correlations in a different way-for example, one plot with var1 from each of sampledf1, sampledf2, through sampledf20, I would have to start all over.

Is there a way to automate this in fewer lines of code?
This is just beyond my level of R programming but I realise this probably has something to do with functions and things (R is mainly a "stats" tool in my work).

I'm also open to different functions to view cross-correlations in different ways if there's a completely different (yet easier) way to achieve this.

答案1

得分: 1

base R

plot(sampledf$lag,
     sampledf$var1, 
     type = "n",
     col = 1,
     xlab = "Lag",
     ylab = "ACF")
Map(function(y, col) lines(sampledf$lag, y, col=col, type="l"),
    sampledf[,-1], seq_len(ncol(sampledf)-1))
legend("topright",                          
       c("Var1", "Var2", "Var3"),
       lty = 1,
       col = 1:3)

在R中用几行代码创建多个图表。

ggplot2

This works much better in a "long" format.

library(ggplot2)
tidyr::pivot_longer(sampledf, -lag) %>%
  ggplot(aes(lag, value, color = name, group = name)) +
  geom_line()

在R中用几行代码创建多个图表。

英文:

You will likely very quickly run out of colors, but you can do this:

base R

plot(sampledf$lag,
     sampledf$var1, 
     type = &quot;n&quot;,
     col = 1,
     xlab = &quot;Lag&quot;,
     ylab = &quot;ACF&quot;)
Map(function(y, col) lines(sampledf$lag, y, col=col, type=&quot;l&quot;),
    sampledf[,-1], seq_len(ncol(sampledf)-1))
legend(&quot;topright&quot;,                          
       c(&quot;Var1&quot;, &quot;Var2&quot;, &quot;Var3&quot;),
       lty = 1,
       col = 1:3)

在R中用几行代码创建多个图表。

ggplot2

This works much better in a "long" format.

library(ggplot2)
tidyr::pivot_longer(sampledf, -lag) |&gt;
  ggplot(aes(lag, value, color = name, group = name)) +
  geom_line()

在R中用几行代码创建多个图表。

huangapple
  • 本文由 发表于 2023年2月24日 01:16:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定