7

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!

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

2 回答 2

11
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])))
    }
于 2013-04-30T07:59:50.293 回答
0

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

        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")
      }
于 2015-07-16T14:17:29.873 回答