Different headers for graphs using a loop in R

I am trying to make graphs in a loop. But how can I put different names on each plot? In this example, I need different names for 8 density plots such as beta [Healing], 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
source share
2 answers
 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, really, if the name of your indexes is the name of the beta columns:

 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]))) } 
+8
source

If a header is selected from a column in a data frame,

  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") } 
0
source

All Articles