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]))))
}
To set title for the plot in R, call plot() function and along with the data to be plot, pass required title string value for the “main” parameter.
We can put multiple graphs in a single plot by setting some graphical parameters with the help of par() function. R programming has a lot of graphical parameters which control the way our graphs are displayed. The par() function helps us in setting or inquiring about these parameters.
Add Titles to a Graph in R Programming – title() Function title() function in R Language is used to add main title and axis title to a graph. This function can also be used to modify the existing titles. Syntax: title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, …)
To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.
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
:
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With