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:

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:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

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

 

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.


2 Comments. Leave new

  • Simon G. Gichuki
    April 7, 2021 2:18 pm

    Hallow. Hope you are doing well. Thanks for the illustration. I have tried to follow the steps to plot a similar graph but am not successful. Below is the kind of data I want to plot, Chainage Vs the other columns.
    Kindly take me through the steps. This is my first time in using R.
    Regards, and Thanks

    Chainage G.L EL HGL
    0 1940 1945 1944.971367
    134 1940.61 1944.368988 1944.340355
    152 1941.38 1944.284225 1944.255592
    165 1940.47 1944.223008 1944.194374
    229 1940.12 1943.921629 1943.892995
    308 1921.55 1943.680259 1943.663921
    370 1923.74 1943.490829 1943.474491
    376 1925.72 1943.472497 1943.456159
    389 1925.57 1943.434553 1943.419002
    406 1925.67 1943.387207 1943.372424
    447 1925.47 1943.278384 1943.264349
    569 1917.79 1942.970171 1942.956865
    614 1919.37 1942.862111 1942.849515
    631 1921.87 1942.821288 1942.808692
    648 1924.02 1942.782541 1942.770635
    661 1925.3 1942.754459 1942.743224
    1019 1927.97 1942.178456 1942.170282
    1191 1923.93 1941.460382 1941.442796
    1376 1927.07 1940.738343 1940.72199
    1376 1927.07 1940.738343 1940.723179
    1422 1927.61 1940.582658 1940.568638
    1510 1927.31 1940.284826 1940.270806
    1771 1923.24 1939.465804 1939.452883
    1913 1925.74 1939.086525 1939.075668
    2010 1925.38 1938.82744 1938.816583
    2090 1923.64 1938.613761 1938.602904
    2228 1923.05 1938.275593 1938.265701
    2241 1925.52 1938.243737 1938.233844
    2510 1923.53 1936.711852 1936.692537
    2717 1928.88 1935.533041 1935.513726
    2746 1929.88 1935.367893 1935.348579

    Reply
    • Hi Simon,

      Thank you, I’m fine and you?

      Your data seems to be formatted well for the code of Example 1. You would simply have to replace the name of the data and the names of the columns.

      At which point do you get problems?

      Regards

      Joachim

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top