How to build all columns of a data frame in R

I have a data frame in R. A data frame has n columns, and I would like to get n graphs, one graph for each column.

I'm a newbie, and I'm not sure about R, anyway, I found two solutions.

The first one works, but it does not print the column name (and I need them!):

data <- read.csv("sample.csv",header=T,sep=",") for ( c in data ) plot( c, type="l" ) 

The second one works better because it prints the column name:

 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") 

Are there any better (in terms of R-point of view) solutions?

Thank. Alessandro

+50
r plot dataframe
Feb 02 '11 at 16:58
source share
9 answers

The ggplot2 package requires a little training, but the results look very beautiful, you get nice legends, as well as many other nice features, without having to write a lot of code.

 require(ggplot2) require(reshape2) df <- data.frame(time = 1:10, a = cumsum(rnorm(10)), b = cumsum(rnorm(10)), c = cumsum(rnorm(10))) df <- melt(df , id.vars = 'time', variable.name = 'series') # plot on same grid, each series colored differently -- # good if the series have same scale ggplot(df, aes(time,value)) + geom_line(aes(colour = series)) # or plot on different plots ggplot(df, aes(time,value)) + geom_line() + facet_grid(series ~ .) 

enter image description hereenter image description here

+52
Feb 02 '11 at 17:51
source share

There is a very simple way to build all columns from a data frame using separate panels or one panel:

 plot.ts(data) 

What gives (where X1 - X4 are the column names):

enter image description here

Have a look? plot.ts for all parameters.

If you no longer control the charting function and use a loop, you can also do something like:

 par(mfcol = c(ncol(data), 1)) Map(function(x,y) plot(x, main =y), data, names(data)) 
+26
Feb 03 2018-11-11T00:
source share

You can go through the hoops and convert your solution into a call to lapply , sapply or apply . (I see that @jonw shows one way to do this.) Also, you already have some pretty good code.

If these are all time series or similar, then the following may be a suitable alternative: each of them has its own panel in one print area. We use the zoo package, as it really processes ordered data.

 require(zoo) set.seed(1) ## example data dat <- data.frame(X = cumsum(rnorm(100)), Y = cumsum(rnorm(100)), Z = cumsum(rnorm(100))) ## convert to multivariate zoo object datz <- zoo(dat) ## plot it plot(datz) 

What gives: Example of zoo plotting capabilities

+12
Feb 02 2018-11-11T00:
source share

Using some of the tips above (especially thanks to @daroczig for the form names(df)[i] ), this function prints a histogram for numeric variables and a histogram for variable factors. A good start to learning about a data frame is:

 par(mfrow=c(3,3),mar=c(2,1,1,1)) #my example has 9 columns dfplot <- function(data.frame) { df <- data.frame ln <- length(names(data.frame)) for(i in 1:ln){ mname <- substitute(df[,i]) if(is.factor(df[,i])){ plot(df[,i],main=names(df)[i])} else{hist(df[,i],main=names(df)[i])} } } 

Best regards, Mat.

+4
Jan 24 '12 at 22:30
source share

I am surprised that no one mentioned matplot . This is quite convenient if you do not need to draw each line in separate axes. Only one command:

 matplot(y = data, type = 'l', lty = 1) 

Use ?matplot to view all options.

To add a legend, you can set a color palette and then add it:

 mypalette = rainbow(ncol(data)) matplot(y = data, type = 'l', lty = 1, col = mypalette) legend(legend = colnames(data), x = "topright", y = "topright", lty = 1, lwd = 2, col = mypalette) 
+4
Dec 05 '16 at 19:20
source share

You can specify the title (as well as the title of the axes using xlab and ylab ) with the main option. For example:.

 plot(data[,i], main=names(data)[i]) 

And if you want to display (and save) each variable in a dataframe, you must use png , pdf or any other graphics driver that you need, and then run the dev.off() command. For example:.

 data <- read.csv("sample.csv",header=T,sep=",") for (i in 1:length(data)) { pdf(paste('fileprefix_', names(data)[i], '.pdf', sep='') plot(data[,i], ylab=names(data[i]), type="l") dev.off() } 

Or draw all the graphs on the same image using the mfrow par() parameter. For example: use par(mfrow=c(2,2) to include the following 4 graphs in the same “image”.

+2
Feb 02 2018-11-18T00:
source share

I do not have R on this computer, but there is a crack here. You can use par to display multiple graphs in a window, or like this, to trigger a click before displaying the next page.

 plotfun <- function(col) plot(data[ , col], ylab = names(data[col]), type = "l") par(ask = TRUE) sapply(seq(1, length(data), 1), plotfun) 
+2
Feb 02 '11 at 17:11
source share

With lattice :

 library(lattice) df <- data.frame(time = 1:10, a = cumsum(rnorm(10)), b = cumsum(rnorm(10)), c = cumsum(rnorm(10))) form <- as.formula(paste(paste(names(df)[- 1], collapse = ' + '), 'time', sep = '~')) xyplot(form, data = df, type = 'b', outer = TRUE) 
+1
Nov 21 '11 at 10:22
source share

If the column names in the .csv file are not valid, R name:

 data <- read.csv("sample.csv",sep=";",head=TRUE) data2 <- read.csv("sample.csv",sep=";",head=FALSE,nrows=1) for ( i in seq(1,length( data ),1) ) plot(data[,i],ylab=data2[1,i],type="l") 
0
Mar 01 '16 at 10:51 on
source share



All Articles