Line Plot using ggplot2 in R
Last Updated :
19 May, 2023
In a line graph, we have the horizontal axis value through which the line will be ordered and connected using the vertical axis values. We are going to use the R package ggplot2 which has several layers in it.
First, you need to install the ggplot2 package if it is not previously installed in R Studio.
Function Used:
- geom_line connects them in the order of the variable on the horizontal (x) axis.
Syntax:
geom_line(mapping=NULL, data=NULL, stat=”identity”, position=”identity”,…)
- geom_path connects the observation in the same order as in data
Syntax:
geom_path(mapping=NULL, data=NULL, stat=”identity”, position=”identity”,…)
Single Line Plot
In this section, we will be dealing with a single-line chart and will also discuss various attributes that help its appearance.
Data Set in Use:
R
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
head (val)
|
Output:
course num
1 DSA 77
2 C++ 55
3 R 80
4 Python 60
Basic Line Plot
For a simple line chart data is roughly passed to the function with some required attributes.
Example:
R
library (ggplot2)
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
ggplot (data=val, aes (x=course, y=num, group=1)) +
geom_line ()+
geom_point ()
|
Output:
Line plot
Formatting Line
For this, the command line type is used. ggplot2 provides various line types. For example dotted, two dash, dashed, etc. This attribute is passed with a required value.
Example:
R
library (ggplot2)
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
ggplot (data=val, aes (x=course, y=num, group=1)) +
geom_line (linetype = "dotted" )+
geom_point ()
|
Output:
Line plot
The command color is used and the desired color is written in double quotes [” “] inside geom_line( ).
Example:
R
library (ggplot2)
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
ggplot (data=val, aes (x=course, y=num, group=1)) +
geom_line (color= "green" )+
geom_point ()
|
Output:
Line plot
The line size can be changed using the command size and providing the value of the size inside geom_line( ).
Example:
R
library (ggplot2)
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
ggplot (data=val, aes (x=course, y=num, group=1)) +
geom_line (color= "green" ,size=1.5)+
geom_point ()
|
Output:
Line plot
Adding Chart Title, Axis Title
ggtitle() with the appropriate title can be used to add chart title and labs again with appropriate input can be used to add axes title.
Example:
R
library (ggplot2)
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
line<- ggplot (data=val, aes (x=course, y=num, group=1)) +
geom_line (color= "green" ,size=1.5)+
geom_point ()
line+ ggtitle ( "Courses vs Students Enrolled in GeeksforGeeks" )+
labs (x= "Courses" ,y= "Number of Students" )
|
Output:
Line plot
Changing the Theme
Use theme_theme_name() to add the theme. There are a lot of themes available in R library. For example: dark, classic, etc. Values can be provided as desired.
Example:
R
library (ggplot2)
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
line<- ggplot (data=val, aes (x=course, y=num, group=1)) +
geom_line (color= "green" ,size=1.5)+
geom_point ()
line+ ggtitle ( "Courses vs Students Enrolled in GeeksforGeeks" )+
labs (x= "Courses" ,y= "Number of Students" )+
theme_dark ()
|
Output:
Line plot
Adding arrow
To add an arrow in line use the grid library is used. Then to add arrows use the arrow( ) to add an arrow. It is also possible to change the parameters in an arrow like angle, type, ends.
Example:
R
library (ggplot2)
library (grid)
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
ggplot (data=val, aes (x=course, y=num, group=1)) +
geom_line (arrow= arrow ())+
geom_point ()
arr= arrow (angle = 20, ends = "both" , type = "closed" )
ggplot (data=val, aes (x=course, y=num, group=1)) +
geom_line (arrow=arr)+
geom_point ()
|
Output:
Line plot
Adding Data labels
Use label to get the values in y-axis and nudge_y to place the data label.
Example:
R
library (ggplot2)
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
ggplot (data=val, aes (x=course, y=num, group=1, label=num)) +
geom_line ()+
geom_point ()+
geom_text (nudge_y = 2)
|
Output:
Line plot
Scaling axis :
Use xlim( ) to change the x-axis scale and ylim( ) to change the y-axis scale and pass appropriate values to these.
Syntax:
xlim(min,max)
ylim(min,max)
Example:
R
library (ggplot2)
val <- data.frame (course= c ( 'DSA' , 'C++' , 'R' , 'Python' ),
num= c (77,55,80,60))
ln <- ggplot (data=val, aes (x=course, y=num, group=1)) +
geom_line (color= "green" ,size=2)+
geom_point ()
ln+ ylim (0,100)+
theme_dark ()
|
Output:
Line plot
Plotting Multiple lines
For plotting multiple plots into one, nothing changes except that group attribute has to set to the name of the column on the basis of which different lines will be drawn.
Example:
R
library (ggplot2)
vacc <- data.frame (type= rep ( c ( "Covishield" , "Covaxin" ), each=2),
dose= rep ( c ( "D1" , "D2" ),2),
slots= c (33, 45, 66, 50))
ggplot (data=vacc, aes (x=dose, y=slots, group=type)) +
geom_line (linetype= "longdash" , color= "green" , size=1.5)+
geom_point (color= "red" , size=5)+
theme_dark ()
|
Output:
Line plot
You can also add title, axes title, data labels in the above line plot as discussed in the previous section.
Formatting the plot :
- Using separate line types based on groups
To differentiate the lines by changing the type of line provide the line type in geom_line() and shape for the legend in geom_point().
Example:
R
library (ggplot)
vacc <- data.frame (type= rep ( c ( "Covishield" , "Covaxin" ), each=2),
dose= rep ( c ( "D1" , "D2" ),2),
slots= c (33, 45, 66, 50))
ggplot (vacc, aes (x=dose, y=slots, group=type)) +
geom_line ( aes (linetype=type))+
geom_point ()+
theme_classic ()
ggplot (vacc, aes (x=dose, y=slots, group=type)) +
geom_line ( aes (linetype=type))+
geom_point ( aes (shape=type))+
theme_classic ()
|
Output:
Line plot
- Assigning different line colors on the basis of groups
The following code automatically controls color using the level of the variable “type”. It will assign separate colors to each line.
Example:
R
library (ggplot2)
vacc <- data.frame (type= rep ( c ( "Covishield" , "Covaxin" ), each=2),
dose= rep ( c ( "D1" , "D2" ),2),
slots= c (33, 45, 66, 50))
ln <- ggplot (vacc, aes (x=dose, y=slots, group=type)) +
geom_line ( aes (color=type))+
geom_point ( aes (color=type))+
theme_classic ()
ln
|
Output:
Line Plot
To enter color manually you can use :
- scale_color_brewer( ) : It uses different color palettes from the RColorBrewer package. It has various color palettes.
- scale_color_manual( ) : It is used to manually add discrete colors.
Example:
R
library (ggplot2)
vacc <- data.frame (type= rep ( c ( "Covishield" , "Covaxin" ), each=2),
dose= rep ( c ( "D1" , "D2" ),2),
slots= c (33, 45, 66, 50))
ln <- ggplot (vacc, aes (x=dose, y=slots, group=type)) +
geom_line ( aes (color=type))+
geom_point ( aes (color=type))+
theme_classic ()
ln+ scale_color_brewer (palette= "Set2" )
ln+ scale_color_manual (values= c ( "green" , "blue" ))
|
Output:
Multiple line plot
- Changing the position of legends
For changing the legend position legen.position attribute of the theme function is passed with the required value.
Syntax:
theme(legend.position=”pos”)
pos It can be top, right, bottom, left or none
Example:
R
library (ggplot2)
vacc <- data.frame (type= rep ( c ( "Covishield" , "Covaxin" ), each=2),
dose= rep ( c ( "D1" , "D2" ),2),
slots= c (33, 45, 66, 50))
ln <- ggplot (vacc, aes (x=dose, y=slots, group=type)) +
geom_line ( aes (color=type))+
geom_point ( aes (color=type))+
theme_classic ()
ln <- ln + scale_color_brewer (palette= "Dark2" )+
theme_classic ()
ln + theme (legend.position= "top" )
ln + theme (legend.position= "left" )
ln + theme (legend.position= "none" )
|
Output:
Multiple line plot
Share your thoughts in the comments
Please Login to comment...