How To Draw Line Between Points In R
Plot Line in R (8 Examples) | Create Line Graph & Chart in RStudio
In this R tutorial you'll larn how to describe line graphs.
The commodity contains eight examples for the plotting of lines. To be more than specific, the article looks equally follows:
- Creating Instance Data
- Case ane: Basic Creation of Line Graph in R
- Example two: Add Main Title & Change Centrality Labels
- Example 3: Change Color of Line
- Instance four: Modify Thickness of Line
- Instance 5: Add Points to Line Graph
- Instance half-dozen: Plot Multiple Lines to Ane Graph
- Instance vii: Different Betoken Symbol for Each Line
- Instance eight: 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 employ the following case data:
ten <- ane : 10 # Create example information y1 <- c( 3, 1, five, two, 3, 8, iv, 7, 6, ix )
x <- i:x # Create example data y1 <- c(three, i, five, 2, three, 8, 4, 7, 6, 9)
Our data consists of ii numeric vectors 10 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 describe a bones line plot in R, we tin use the plot part with the specification type = "l". Have a look at the following R code:
plot(x, y1, type = "l" ) # Bones line plot in R
plot(x, y1, type = "l") # Basic line plot in R
Figure 1: Bones Line Plot in R.
Effigy i visualizes the output of the previous R syntax: A line chart with a single black line.
Based on Figure 1 you tin can also see that our line graph is relatively patently and simple. In the following examples, I'll explain how to modify the different parameters of this plot. Then go along on reading!
Case 2: Add Master Championship & Modify Axis Labels
In Example two, you'll larn how to change the master title and the centrality labels of our plot with the main, xlab, and ylab arguments of the plot office:
plot(x, y1, type = "fifty", # Alter main title & centrality labels main = "This is my Line Plot", xlab = "My X-Values", ylab = "My Y-Values" )
plot(x, y1, type = "l", # Alter main title & axis labels main = "This is my Line Plot", xlab = "My X-Values", ylab = "My Y-Values")
Figure ii: Manual Main Championship & Axis Labels.
Have a look at Effigy 2: Our new plot has the main title "This is my Line Plot", the ten-axis label "My X-Values", and the y-axis label "My Y-Values".
Instance 3: Change Color of Line
Nosotros can also adjust the color of our line by using the col argument of the plot command:
plot(x, y1, type = "l", # Modify color of line col = "pinkish" )
plot(x, y1, type = "l", # Change color of line col = "pink")
Figure 3: Changed Color of Line Graph.
Note that you may utilize any Hex color code or the predefined colors in R to alter the color of your graphics.
Example four: Alter Thickness of Line
We can increase or decrease the thickness of the lines of a line graphic with the lwd option as follows:
plot(10, y1, type = "l", # Change thickness of line lwd = 10 )
plot(x, y1, type = "50", # Change thickness of line lwd = ten)
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 past decreasing this number the line is condign thinner. Note that the line thickness may also be inverse, 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 improve. Nosotros but need to replace the blazon of our graph from "50" to "b":
plot(x, y1, type = "b" ) # Add symbols to points
plot(x, y1, type = "b") # Add symbols to points
Effigy 5: Unlike 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, nosotros demand to create further variables for our plot:
y2 <- c( five, ane, 4, 6, two, 3, 7, 8, 2, 8 ) # Create more example data y3 <- c( 3, 3, 3, three, 4, 4, 5, 5, vii, 7 )
y2 <- c(5, 1, 4, 6, 2, 3, 7, 8, 2, 8) # Create more than example data y3 <- c(iii, three, 3, 3, 4, 4, 5, v, vii, vii)
Now, we can use the lines function to add these new information to our previously created line chart:
plot(x, y1, type = "l" ) # Depict first line lines(x, y2, type = "fifty", col = "reddish" ) # Add 2d line lines(ten, y3, type = "l", col = "green" ) # Add third line
plot(x, y1, type = "l") # Draw commencement line lines(x, y2, type = "fifty", col = "ruddy") # Add second line lines(10, y3, type = "fifty", col = "green") # Add third line
Furthermore, we may add a legend to our motion-picture show to visualize which colour refers to which of the dissimilar variables.
fable( "topleft", # Add legend to plot legend = c( "Line y1", "Line y2", "Line y3" ), col = c( "black", "red", "green" ), lty = ane )
fable("topleft", # Add legend to plot fable = c("Line y1", "Line y2", "Line y3"), col = c("black", "red", "dark-green"), lty = one)
Figure 6: Draw Several Lines in Same Graphic.
Figure six shows the output of the R code of Example 6. We created a graph with multiple lines, dissimilar colors for each line, and a legend representing the different lines.
Example 7: Different Betoken Symbol for Each Line
Similar to Example 6, nosotros can assign different point symbols to each of our lines by specifying type = "b". With the pch statement we can specify a different bespeak symbol for each line.
plot(x, y1, blazon = "b", pch = 16 ) # Change type of symbol lines(ten, y2, type = "b", col = "red", pch = 15 ) lines(10, y3, type = "b", col = "green", pch = 8 )
plot(x, y1, blazon = "b", pch = 16) # Alter type of symbol lines(x, y2, type = "b", col = "crimson", pch = 15) lines(x, y3, type = "b", col = "green", pch = eight)
We also need to consider these different signal symbols in the legend of our plot:
fable( "topleft", # Add fable 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 fable = c("Line y1", "Line y2", "Line y3"), col = c("black", "red", "green"), pch = c(16, 15, 8))
Figure vii: Alter pch Symbols of Line Graph.
Example 8: Line Graph in ggplot2 (geom_line Function)
And so far, we have only used functions of the base installation of the R programming language. However, at that place are many packages available that provide functions for the drawing of line charts.
1 of the nigh powerful packages for the creation of graphics is the ggplot2 package. We can install and load the ggplot2 package with the post-obit two lines of R code:
install. packages ( "ggplot2" ) # Install and load ggplot2 library( "ggplot2" )
install.packages("ggplot2") # Install and load ggplot2 library("ggplot2")
Furthermore, nosotros demand to store our data in a information frame, since the ggplot2 parcel is commonly based on data frames:
information <- data. frame (10 = rep( ane : 10, 3 ), # Create information frame y = c(y1, y2, y3), line = c(rep( "y1", 10 ), rep( "y2", 10 ), rep( "y3", ten ) ) ) head(data) # Print beginning half-dozen rows # x y line # 1 3 y1 # 2 ane y1 # 3 five y1 # 4 2 y1 # 5 3 y1 # half-dozen 8 y1
data <- data.frame(x = rep(one:ten, iii), # Create data frame y = c(y1, y2, y3), line = c(rep("y1", 10), rep("y2", ten), rep("y3", 10))) head(data) # Impress outset 6 rows # x y line # i 3 y1 # 2 1 y1 # 3 5 y1 # 4 2 y1 # 5 iii y1 # 6 8 y1
The RStudio console is showing how our new data is structured. Our information frame contains 3 columns and xxx rows. The first column contains of our ten values (i.eastward. 1 to ten), the second column consists of the values of our 3 variables, and the tertiary cavalcade is specifying to which variable the values of a row belong.
Now, nosotros can employ the ggplot function in combination with the geom_line function to describe a line graph with the ggplot2 packet:
ggplot(data, aes(ten = ten, y = y, col = line) ) + # Describe line plot with ggplot2 geom_line( )
ggplot(data, aes(x = x, y = y, col = line)) + # Draw line plot with ggplot2 geom_line()
Figure viii: Create Line Chart with ggplot2 Package.
Figure 8 is showing how a ggplot2 line graph looks like. Of cause, the ggplot2 package is likewise providing many options for the modification of line graphics in R.
Video, Further Resources & Summary
Practise you need more information on the R programming syntax of this article? So you might scout 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 Aforementioned 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 lot accept whatsoever further questions, don't hesitate to let me know in the comments department.
Source: https://statisticsglobe.com/plot-line-in-r-graph-chart
Posted by: mcculloughglelavold.blogspot.com

0 Response to "How To Draw Line Between Points In R"
Post a Comment