【问题标题】:Different titles for plots using loop in R在 R 中使用循环的绘图的不同标题
【发布时间】:2013-04-24 01:51:31
【问题描述】:

我正在尝试循环绘制图。但是我如何在每个情节上放置不同的标题?在这个例子中,我想要为我的 8 个密度图使用不同的名称,例如 beta[Treatment]、beta[Time Dummy] 等。谢谢!

par(mfrow=c(4,2)
for (i in 2:8) {
  plot(density(beta[,i]))
  title(main=substitute(paste('Density of ', beta[Treatment]))))
}

【问题讨论】:

    标签: r for-loop plot


    【解决方案1】:

    如果标题是从数据框中的列中选取的,

            V1  V2
        1   Title1  AA
        2   Title2  BB
        3   Title3  CC
        4   Title4  DD
        5   Title5  EE
    

    以下代码可用于获取剧情中的不同标题:

        num.plots <- nrow(df)
        for(i in 1:num.plots){
          plot(df$V2~df$V3, main=df$V1[i], type = "l", col="red")
          }
    

    【讨论】:

      【解决方案2】:
      tvec <- c("Treatment", "Time Dummy")
      
      par(mfrow=c(2,1))
      for(i in 1:2){
          plot(density(beta[,i]), 
               main=substitute(paste('Density of ', beta[a]), list(a=tvec[i])))
          }
      

      或者实际上,如果您的下标名称是beta 的列名称:

      par(mfrow=c(4,2))
      for(i in 2:8){
          plot(density(beta[,i]), 
               main=substitute(paste('Density of ', beta[a]), list(a=colnames(beta)[i])))
          }
      

      【讨论】:

        猜你喜欢
        • 2020-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-20
        • 2020-02-26
        • 2019-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多