Plot All Columns Of Data Frame In R (3 Examples) - Statistics Globe

Plot All Columns of Data Frame in R (3 Examples) | How to Draw Each Variable

In this tutorial, I’ll explain how to draw all variables of a data set in a line plot in the R programming language.

Table of contents:

1) Creation of Example Data 2) Example 1: Drawing Multiple Variables Using Base R 3) Example 2: Drawing Multiple Variables Using ggplot2 Package 4) Example 3: Drawing Multiple Variables in Different Panels with ggplot2 Package 5) Video & Further Resources 6) Subscribe to the Statistics Globe Newsletter 7) Thank you!

Let’s dive right in!

Creation of Example Data

Let’s first create some example data:

set.seed(987425) # Create example data data <- data.frame(x = 1:10, y1 = rnorm(10), y2 = runif(10), y3 = rpois(10, 1)) data # Print example data # x y1 y2 y3 # 1 1 -1.19464442 0.6631678 2 # 2 2 -0.27292005 0.9540095 0 # 3 3 -0.05134384 0.6712889 1 # 4 4 0.45500651 0.1736061 1 # 5 5 -2.07007318 0.2290419 0 # 6 6 0.92083477 0.3240386 0 # 7 7 -0.26656251 0.2139329 0 # 8 8 0.10529478 0.7744575 1 # 9 9 -2.17999010 0.6029383 1 # 10 10 -1.51876252 0.8177035 0

As you can see based on the previous output of the RStudio console, our example data contains ten rows and four columns. The variable x is ranging from 1 to 10 and defines the x-axis for each of the other variables.

Example 1: Drawing Multiple Variables Using Base R

The following code shows how to draw a plot showing multiple columns of a data frame in a line chart using the plot R function of Base R. Have a look at the following R syntax:

plot(data$x, data$y1, type = "l", col = 1, ylim = c(- 3, 3)) # Plot with Base R lines(data$x, data$y2, type = "l", col = 2) lines(data$x, data$y3, type = "l", col = 3)

r graph figure 1 plot all columns data frame

As shown in Figure 1, we created a Base R line plot showing three lines with the previous code.

Example 2: Drawing Multiple Variables Using ggplot2 Package

Example 2 illustrates how to use the ggplot2 package to create a graphic containing the values of all data frame columns. First, we need to reshape our data frame to long format:

data_ggp <- data.frame(x = data$x, # Reshape data frame y = c(data$y1, data$y2, data$y3), group = c(rep("y1", nrow(data)), rep("y2", nrow(data)), rep("y3", nrow(data)))) head(data_ggp) # Head of reshaped data frame # x y group # 1 1 -1.19464442 y1 # 2 2 -0.27292005 y1 # 3 3 -0.05134384 y1 # 4 4 0.45500651 y1 # 5 5 -2.07007318 y1 # 6 6 0.92083477 y1

Furthermore, we have to install and load the ggplot2 package, if we want to use the corresponding functions:

install.packages("ggplot2") # Install & load ggplot2 library("ggplot2")

Now, we can draw a ggplot2 line graph with the following R code:

ggp <- ggplot(data_ggp, aes(x, y, col = group)) + # Create ggplot2 plot geom_line() ggp # Draw plot

r graph figure 2 plot all columns data frame

As shown in Figure 2, we plotted a graph showing a different line for each variable with the previous R programming code. On the right side of the plot, we have also created a legend illustrating the different groups of our data.

Example 3: Drawing Multiple Variables in Different Panels with ggplot2 Package

In Example 3, I’ll show how to draw each of our columns in a different panel of a facet plot. For this, we simply need to add the facte_grid function to our previously created graph of Example 2:

ggp + facet_grid(group ~ .) # Draw plot in different panels

r graph figure 3 plot all columns data frame

As shown in Figure 3, the previous syntax created a facet plot using the ggplot2 package.

Video & Further Resources

Would you like to know more about the plotting of columns? Then you could watch the following video of my YouTube channel. I’m explaining the examples of this article in the video:

In addition, you could have a look at the related articles of this website.

  • Plot Line in R
  • Reshape Data Frame from Wide to Long Format
  • R Graphics Gallery
  • The R Programming Language

To summarize: In this R programming tutorial you learned how to draw each column of a data matrix in a graphic. Don’t hesitate to let me know in the comments section below, if you have any additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.I hate spam & you may opt out anytime: Privacy Policy.

Subscribe  

Loading...

Thank you!

Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.

Tag » How To Plot A Dataframe In R