简体   繁体   English

R中使用循环绘制的不同标题

[英]Different titles for plots using loop in R

I am trying to make plots in a loop. 我想在循环中制作情节。 But how do I put different titles on each plot? 但是我如何在每个情节上添加不同的标题? In this example, I want different names for my 8 density plots, such as beta[Treatment], beta[Time Dummy], etc. Thanks! 在这个例子中,我想要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]))))
}
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])))
    }

Or actually if the name of your subscripts is the name of the columns of beta : 或者实际上,如果您的下标名称是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])))
    }

If the title is being picked from a column in a dataframe, 如果从数据框中的列中选取标题,

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

The following code can be used to get different titles in the plot: 以下代码可用于在图中获取不同的标题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM