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

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

Creating multiple plots with few lines of code in R

问题

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

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

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

  1. sampledf <- data.frame(
  2. lag = -10:10,
  3. var1 = rnorm(21),
  4. var2 = rnorm(21),
  5. var3 = rnorm(21)
  6. )

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

  1. plot(sampledf$lag,
  2. sampledf$var1,
  3. type = "l",
  4. col = 1,
  5. xlab = "Lag",
  6. ylab = "ACF")
  7. lines(sampledf$lag,
  8. sampledf$var2,
  9. type = "l",
  10. col = 2)
  11. lines(sampledf$lag,
  12. sampledf$var3,
  13. type = "l",
  14. col = 3)
  15. legend("topright",
  16. c("Var1", "Var2", "Var3"),
  17. lty = 1,
  18. 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.

  1. sampledf &lt;- data.frame(
  2. lag = -10:10,
  3. var1 = rnorm(21),
  4. var2 = rnorm(21),
  5. var3 = rnorm(21)
  6. )

Now I could plot them fairly easily like this:

  1. plot(sampledf$lag,
  2. sampledf$var1,
  3. type = &quot;l&quot;,
  4. col = 1,
  5. xlab = &quot;Lag&quot;,
  6. ylab = &quot;ACF&quot;)
  7. lines(sampledf$lag,
  8. sampledf$var2,
  9. type = &quot;l&quot;,
  10. col = 2)
  11. lines(sampledf$lag,
  12. sampledf$var3,
  13. type = &quot;l&quot;,
  14. col = 3)
  15. legend(&quot;topright&quot;,
  16. c(&quot;Var1&quot;, &quot;Var2&quot;, &quot;Var3&quot;),
  17. lty = 1,
  18. 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

  1. plot(sampledf$lag,
  2. sampledf$var1,
  3. type = "n",
  4. col = 1,
  5. xlab = "Lag",
  6. ylab = "ACF")
  7. Map(function(y, col) lines(sampledf$lag, y, col=col, type="l"),
  8. sampledf[,-1], seq_len(ncol(sampledf)-1))
  9. legend("topright",
  10. c("Var1", "Var2", "Var3"),
  11. lty = 1,
  12. col = 1:3)

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

ggplot2

This works much better in a "long" format.

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

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

英文:

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

base R

  1. plot(sampledf$lag,
  2. sampledf$var1,
  3. type = &quot;n&quot;,
  4. col = 1,
  5. xlab = &quot;Lag&quot;,
  6. ylab = &quot;ACF&quot;)
  7. Map(function(y, col) lines(sampledf$lag, y, col=col, type=&quot;l&quot;),
  8. sampledf[,-1], seq_len(ncol(sampledf)-1))
  9. legend(&quot;topright&quot;,
  10. c(&quot;Var1&quot;, &quot;Var2&quot;, &quot;Var3&quot;),
  11. lty = 1,
  12. col = 1:3)

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

ggplot2

This works much better in a "long" format.

  1. library(ggplot2)
  2. tidyr::pivot_longer(sampledf, -lag) |&gt;
  3. ggplot(aes(lag, value, color = name, group = name)) +
  4. 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:

确定