How to Draw a Line Plot in R
Plot Line in R (8 Examples) | Create Line Graph & Chart in RStudio
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 2: Add Main Title & Change Axis Labels
- 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)
- Video, Further Resources & Summary
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 data y1 <- c( 3, 1, 5, 2, 3, 8, 4, 7, 6, 9 )
x <- 1:10 # Create example data y1 <- 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(x, y1, type = "l") # Basic line plot in R
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(x, y1, type = "l", # Change main title & axis labels main = "This is my Line Plot", xlab = "My X-Values", ylab = "My Y-Values")
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(x, y1, type = "l", # Change color of line col = "pink")
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(x, y1, type = "l", # Change thickness of line lwd = 10)
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(x, y1, type = "b") # Add symbols to points
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 data y3 <- c( 3, 3, 3, 3, 4, 4, 5, 5, 7, 7 )
y2 <- c(5, 1, 4, 6, 2, 3, 7, 8, 2, 8) # Create more example data y3 <- 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 line lines(x, y2, type = "l", col = "red" ) # Add second line lines(x, y3, type = "l", col = "green" ) # Add third line
plot(x, y1, type = "l") # Draw first line lines(x, y2, type = "l", col = "red") # Add second line lines(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 )
legend("topleft", # Add legend to plot legend = c("Line y1", "Line y2", "Line y3"), col = c("black", "red", "green"), lty = 1)
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 symbol lines(x, y2, type = "b", col = "red", pch = 15 ) lines(x, y3, type = "b", col = "green", pch = 8 )
plot(x, y1, type = "b", pch = 16) # Change type of symbol lines(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 ) )
legend("topleft", # Add legend to plot legend = c("Line y1", "Line y2", "Line y3"), col = c("black", "red", "green"), pch = c(16, 15, 8))
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 ggplot2 library( "ggplot2" )
install.packages("ggplot2") # Install and load ggplot2 library("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
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( )
ggplot(data, aes(x = x, y = y, col = line)) + # Draw line plot with ggplot2 geom_line()
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
- Draw Multiple Graphs & Lines in Same Plot
- 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.
Source: https://statisticsglobe.com/plot-line-in-r-graph-chart
0 Response to "How to Draw a Line Plot in R"
Post a Comment