【问题标题】:multiple plots using a loop使用循环的多个图
【发布时间】:2019-07-21 10:02:38
【问题描述】:

我有一个数据框,其中第一列是年份,连续列是国家名称,每个国家/地区都有时间序列数据 (28)。我想一口气画出所有这些。 这是我的代码:

 par(mfrow=c(4,7))
    for (i in DP[,2:29]) {
      plot(DP$Year,i,
           ylim=range(c(0, 400)),
           type="p",col="red", xaxt="n", yaxt="n",
           ylab="mortality rate",
           xlab="year",
           pch=16, main=c(colnames(DP[,2:29])))
      axis(side = 1)
      axis(side = 2, seq(from=0, to=400, by=25))
    }

但是,该图显示了每个图表的所有国家/地区名称。我该怎么办?

【问题讨论】:

标签: r loops plot


【解决方案1】:

您可以尝试先将国家/地区名称放在一个列表中,然后循环遍历它们。

country_list<-unique(dataset$country)
for (i in seq_along(country_list)){
  plot(DP$Year,i,
       ylim=range(c(0, 400)),
       type="p",col="red", xaxt="n", yaxt="n",
       ylab="mortality rate",
       xlab="year",
       pch=16, main=country_list[i])
  axis(side = 1)
  axis(side = 2, seq(from=0, to=400, by=25))
}

或者您可以使用 ggplot2 和 facet_wrap 按国家/地区分面。

【讨论】:

  • 谢谢,但仍然无法正常工作 xy.coords(x, y, xlabel, ylabel, log) 中的错误:'x' 和 'y' 长度不同
  • 感谢 Joran,这是我的原始代码 par(mfrow=c(4,7)) for (i in DP[,2:29]) { plot(DP$Year,i, ylim= range(c(0, 400)), type="p",col="red", xaxt="n", yaxt="n", ylab="死亡率", xlab="year", pch=16 ) axis(side = 1) axis(side = 2, seq(from=0, to=400, by=25)) } 这给了我想要的情节,但没有标题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 2021-10-20
相关资源
最近更新 更多