Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different titles for plots using loop in R

Tags:

for-loop

plot

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!

par(mfrow=c(4,2)
for (i in 2:8) {
  plot(density(beta[,i]))
  title(main=substitute(paste('Density of ', beta[Treatment]))))
}
like image 665
Heisenberg Avatar asked Apr 30 '13 07:04

Heisenberg


People also ask

How do you set the title of a plot in R?

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.

How do you make multiple plots in R?

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.

How do you code a title in R?

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, …)

How do you set the axis labels and title of the R plots?

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”.


1 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 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])))
    }
like image 159
plannapus Avatar answered Oct 20 '22 05:10

plannapus