Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put column names of a data frame as the title of plots of each column

Tags:

dataframe

plot

r

I have a data frame with 36 columns and more than 3000 rows. I am using plot function inside a for loop to plot graphs of each column. I want the title of the graph to appear as column name. How can I do that?

for(i in c(1:36)){
  plot(DowData[,i],type="l",main="colnames(DowData)[i]")
}
like image 474
Ved Gupta Avatar asked Jan 02 '14 10:01

Ved Gupta


People also ask

How do I get column names from a DataFrame?

You can get the column names from pandas DataFrame using df. columns. values , and pass this to python list() function to get it as list, once you have the data you can print it using print() statement.

How do you name columns in data frames in R?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

How do I add column names to a DataFrame in Python?

Adding column name to the DataFrame : We can add columns to an existing DataFrame using its columns attribute. Output : Now the DataFrame has column names. Renaming column name of a DataFrame : We can rename the columns of a DataFrame by using the rename() function.


2 Answers

Using lapply you can loop over columns names:

 invisible(lapply(colnames(DowData),function(x){
      plot(DowData[,x],main=x,type="l")
    }))
like image 180
agstudy Avatar answered Oct 02 '22 05:10

agstudy


data <- read.csv("sample.csv",header=T,sep=",")
for ( i in seq(1,length( data ),1) ) plot(data[,i],ylab=names(data[i]),type="l")

This should Work

like image 40
Prasanna Nandakumar Avatar answered Oct 02 '22 04:10

Prasanna Nandakumar