Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (2024)

In this R tutorial you’ll learn how to draw line graphs.

The article contains eight examples for the plotting of lines. To be more specific, the article looks as follows:

  • Creating Example Data
  • Example 1: Basic Creation of Line Graph in R
  • Example 3: Change Color of Line
  • Example 4: Modify Thickness of Line
  • Example 5: Add Points to Line Graph
  • Example 6: Plot Multiple Lines to One Graph
  • Example 7: Different Point Symbol for Each Line
  • Example 8: Line Graph in ggplot2 (geom_line Function)

Let’s get started…

Creating Example Data

In the examples of this R tutorial, we’ll use the following example data:

x <- 1:10 # Create example datay1 <- c(3, 1, 5, 2, 3, 8, 4, 7, 6, 9)

Our data consists of two numeric vectors x and y1. The vector x contains a sequence from 1 to 10, y1 contains some random numeric values.

Example 1: Basic Creation of Line Graph in R

If we want to draw a basic line plot in R, we can use the plot function with the specification type = “l”. Have a look at the following R code:

plot(x, y1, type = "l") # Basic line plot in R

Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (1)

Figure 1: Basic Line Plot in R.

Figure 1 visualizes the output of the previous R syntax: A line chart with a single black line.

Based on Figure 1 you can also see that our line graph is relatively plain and simple. In the following examples, I’ll explain how to modify the different parameters of this plot. So keep on reading!

Example 2: Add Main Title & Change Axis Labels

In Example 2, you’ll learn how to change the main title and the axis labels of our plot with the main, xlab, and ylab arguments of the plot function:

plot(x, y1, type = "l", # Change main title & axis labels main = "This is my Line Plot", xlab = "My X-Values", ylab = "My Y-Values")

Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (2)

Figure 2: Manual Main Title & Axis Labels.

Have a look at Figure 2: Our new plot has the main title “This is my Line Plot”, the x-axis label “My X-Values”, and the y-axis label “My Y-Values”.

Example 3: Change Color of Line

We can also adjust the color of our line by using the col argument of the plot command:

plot(x, y1, type = "l", # Change color of line col = "pink")

Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (3)

Figure 3: Changed Color of Line Graph.

Note that you may use any Hex color code or the predefined colors in R to change the color of your graphics.

Example 4: Modify Thickness of Line

We can increase or decrease the thickness of the lines of a line graphic with the lwd option as follows:

plot(x, y1, type = "l", # Change thickness of line lwd = 10)

Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (4)

Figure 4: User-Defined Thickness of Lines.

In this example, we used an lwd of 10. By increasing this number, the thickness is getting larger, and by decreasing this number the line is becoming thinner. Note that the line thickness may also be changed, when exporting your image to your computer.

Example 5: Add Points to Line Graph

It is possible to add points to visualize the underlying data of our line plot even better. We simply need to replace the type of our graph from “l” to “b”:

plot(x, y1, type = "b") # Add symbols to points

Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (5)

Figure 5: Different Types of Line Plot.

Example 6: Plot Multiple Lines to One Graph

In this example I want to show you how to plot multiple lines to a graph in R. First, we need to create further variables for our plot:

y2 <- c(5, 1, 4, 6, 2, 3, 7, 8, 2, 8) # Create more example datay3 <- c(3, 3, 3, 3, 4, 4, 5, 5, 7, 7)

Now, we can use the lines function to add these new data to our previously created line chart:

plot(x, y1, type = "l") # Draw first linelines(x, y2, type = "l", col = "red") # Add second linelines(x, y3, type = "l", col = "green") # Add third line

Furthermore, we may add a legend to our picture to visualize which color refers to which of the different variables.

legend("topleft", # Add legend to plot legend = c("Line y1", "Line y2", "Line y3"), col = c("black", "red", "green"), lty = 1)

Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (6)

Figure 6: Draw Several Lines in Same Graphic.

Figure 6 shows the output of the R code of Example 6. We created a graph with multiple lines, different colors for each line, and a legend representing the different lines.

Example 7: Different Point Symbol for Each Line

Similar to Example 6, we can assign different point symbols to each of our lines by specifying type = “b”. With the pch argument we can specify a different point symbol for each line.

plot(x, y1, type = "b", pch = 16) # Change type of symbollines(x, y2, type = "b", col = "red", pch = 15)lines(x, y3, type = "b", col = "green", pch = 8)

We also need to consider these different point symbols in the legend of our plot:

legend("topleft", # Add legend to plot legend = c("Line y1", "Line y2", "Line y3"), col = c("black", "red", "green"), pch = c(16, 15, 8))

Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (7)

Figure 7: Change pch Symbols of Line Graph.

Example 8: Line Graph in ggplot2 (geom_line Function)

So far, we have only used functions of the base installation of the R programming language. However, there are many packages available that provide functions for the drawing of line charts.

One of the most powerful packages for the creation of graphics is the ggplot2 package. We can install and load the ggplot2 package with the following two lines of R code:

install.packages("ggplot2") # Install and load ggplot2library("ggplot2")

Furthermore, we need to store our data in a data frame, since the ggplot2 package is usually based on data frames:

data <- data.frame(x = rep(1:10, 3), # Create data frame  y = c(y1, y2, y3), line = c(rep("y1", 10), rep("y2", 10), rep("y3", 10)))head(data) # Print first 6 rows# x y line# 1 3 y1# 2 1 y1# 3 5 y1# 4 2 y1# 5 3 y1# 6 8 y1

The RStudio console is showing how our new data is structured. Our data frame contains three columns and 30 rows. The first column contains of our x values (i.e. 1 to 10), the second column consists of the values of our three variables, and the third column is specifying to which variable the values of a row belong.

Now, we can apply the ggplot function in combination with the geom_line function to draw a line graph with the ggplot2 package:

ggplot(data, aes(x = x, y = y, col = line)) + # Draw line plot with ggplot2 geom_line()

Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (8)

Figure 8: Create Line Chart with ggplot2 Package.

Figure 8 is showing how a ggplot2 line graph looks like. Of cause, the ggplot2 package is also providing many options for the modification of line graphics in R.

Video, Further Resources & Summary

Do you need more information on the R programming syntax of this article? Then you might watch the following video of my YouTube channel. I’m explaining the content of this article in the video.

The YouTube video will be added soon.

In addition, you might have a look at some of the related tutorials on this website.

  • plot() Function
  • R Graphics Gallery
  • R Functions List (+ Examples)
  • The R Programming Language

You learned in this tutorial how to plot lines between points in the R programming language. If you have any further questions, don’t hesitate to let me know in the comments section.

7 Comments. Leave new

  • Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (9)

    JDub

    December 27, 2020 4:04 am

    Thanks

    Reply
    • Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (10)

      Joachim

      December 28, 2020 7:31 am

      You are very welcome JDub!

      Reply
  • Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (11)

    John Waweru

    July 27, 2021 6:09 pm

    Thanks for this very helpful tutorial. I would like to know how I could plot a dotted line in R.

    Reply
    • Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (12)

      Joachim

      August 2, 2021 10:12 am

      Hi John,

      Thanks a lot for the kind words!

      Regarding your question, you can find a ggplot2 solution here: https://statisticsglobe.com/control-line-color-type-in-ggplot2-plot-legend-in-r

      Furthermore, I have planned to release a Base R tutorial on line types soon. I’ll keep you updated.

      Regards

      Joachim

      Reply
    • Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (13)

      Joachim

      August 3, 2021 12:43 pm

      As a follow-up to my previous response: I have just published a new tutorial that explains how to modify the line types of a Base R plot: https://statisticsglobe.com/change-line-type-base-r-plot

      I hope that helps!

      Joachim

      Reply
  • Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (14)

    Olivier

    December 23, 2021 6:51 pm

    Thanks Joachim,

    I was looking for a quick way to draw a chart and already had R around, this page was perfect for a quick solution.

    When I used my own data, this gave me bogus result, though :

    x <- 1:12
    y1 <- c(1411, 1164, 2129, 1712, 2040, 1581, 1661, 2700, 2067, 1492, 2073, 1354)
    y2 <- c(688, 688, 688, 750, 655, 655, 671, 741, 742, 742, 742, 673)
    plot(x, y1, type = 'b')
    lines(x, y2, type = 'b', col = "red")

    … the second line would just not appear in the chart. If anyone hit the same problem, the solution for me was to specify the bounds of the y axis manually :

    plot(x, y1, type = 'b', ylim = c(0, 3000))

    After that, everything worked as expected.

    Reply
    • Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (15)

      Joachim

      December 27, 2021 7:00 am

      Hey Olivier,

      Thanks a lot for the kind words, glad you found this page useful!

      Also, thank you for the hint! Indeed, the axis limits are defined by the data in the first plot (i.e. the plot function) if not specified manually. Glad you found this solution, and thanks for sharing!

      Regards,
      Joachim

      Reply

Leave a Reply

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

Statistics Globe Newsletter

Related Tutorials

Add p-Values to Correlation Matrix Plot in R (2 Examples)

Change Space Between Boxplots in R (2 Examples)

Plot Line in R (8 Examples) | Draw Line Graph & Chart in RStudio (2024)

References

Top Articles
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 5283

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.