【问题标题】:Plot multiple plots from dataframes stored in list从存储在列表中的数据帧中绘制多个图
【发布时间】:2020-03-17 01:23:40
【问题描述】:

我正在尝试从列表中存储的每个数据中绘制多个图。当我尝试为列表中的单个数据框进行绘图时,我的代码有效:

ggplot(data.frame(s[1]), aes(x = No, y = Val))  +
  geom_line(linetype = "dashed",color="red")+geom_point()

但在我运行循环时无法工作。

for (i in 1:3)
{
  ggplot(data.frame(s[i]), aes(x = No, y = Val))  +
    geom_line(linetype = "dashed",color="red")+geom_point()
  i = i+1
}

如何解决问题?

【问题讨论】:

  • 您能否详细说明它是如何不起作用的?情节不显示吗?数据看起来不对吗?您想将绘图保存在列表中吗?你有错误吗?

标签: r list dataframe ggplot2 lapply


【解决方案1】:

您可以将 for 循环中的绘图保存在变量上并按如下方式打印:

for (i in 1:3)
{
  myplot <- ggplot(data.frame(s[i]), aes(x = No, y = Val))  +
    geom_line(linetype = "dashed",color="red")+geom_point()
  print(myplot)

}

【讨论】:

    【解决方案2】:

    您还可以将所有图存储在一个列表中

    myplot <- list()
    for (i in 1:3) {
       myplot[i] <- ggplot(data.frame(s[i]), aes(x = No, y = Val)) +
       geom_line(linetype = "dashed", color = "red") + 
       geom_point()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2022-11-26
      • 2015-04-12
      • 2013-11-16
      相关资源
      最近更新 更多