Line Plot using ggplot2 in R
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
# Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60))head(val) |
Output:

Basic Line Plot
For a simple line chart data is roughly passed to the function with some required attributes.
Example:
R
library(ggplot2) # Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60)) # Basic Lineggplot(data=val, aes(x=course, y=num, group=1)) + geom_line()+ geom_point() |
Output:

Formating Line
- Line Type
For this, the command linetype 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) # Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60)) # Format the line typeggplot(data=val, aes(x=course, y=num, group=1)) + geom_line(linetype = "dotted")+ geom_point() |
Output:

- Line Color
The command color is used and the desired color is written in double quotes [” “] inside geom_line( ).
Example:
R
library(ggplot2) # Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60)) # Format the line colorggplot(data=val, aes(x=course, y=num, group=1)) + geom_line(color="green")+ geom_point() |
Output:

- Line Size
The line size can be changed using the command size and providing the value of the size inside geom_line( ).
Example:
R
library(ggplot2) # Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60)) # Format the line sizeggplot(data=val, aes(x=course, y=num, group=1)) + geom_line(color="green",size=1.5)+ geom_point() |
Output:

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) # Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60)) # Adding titlesline<-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:

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) # Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60)) # Adding titlesline<-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:

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) # Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60)) # Adding an arrowggplot(data=val, aes(x=course, y=num, group=1)) + geom_line(arrow=arrow())+ geom_point() # Adding closed arrow on both ends of the linearr=arrow(angle = 20, ends = "both", type = "closed")ggplot(data=val, aes(x=course, y=num, group=1)) + geom_line(arrow=arr)+ geom_point() |
Output:

Adding Data labels
Use label to get the values in y-axis and nudge_y to place the data label.
Example:
R
library(ggplot2) # Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60)) # Adding data labelggplot(data=val, aes(x=course, y=num, group=1, label=num)) + geom_line()+ geom_point()+ geom_text(nudge_y = 2) |
Output:

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) # Create data for chartval <-data.frame(course=c('DSA','C++','R','Python'), num=c(77,55,80,60)) # Storing the line plotln <-ggplot(data=val, aes(x=course, y=num, group=1)) + geom_line(color="green",size=2)+ geom_point() # y-axis limitsln+ylim(0,100)+theme_dark() |
Output:

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) # Inserting datavacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2), dose=rep(c("D1", "D2"),2), slots=c(33, 45, 66, 50)) # Plotting line with multiple groupsggplot(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:

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) # Inserting datavacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2), dose=rep(c("D1", "D2"),2), slots=c(33, 45, 66, 50)) # Changing the line type on the basis of groupsggplot(vacc, aes(x=dose, y=slots, group=type)) + geom_line(aes(linetype=type))+ geom_point()+ theme_classic() # Changing the line type on the basis of groups and also the shape of pointsggplot(vacc, aes(x=dose, y=slots, group=type)) + geom_line(aes(linetype=type))+ geom_point(aes(shape=type))+ theme_classic() |
Output:

- 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) # Inserting datavacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2), dose=rep(c("D1", "D2"),2), slots=c(33, 45, 66, 50)) # Change line color by group type of vaccineln <-ggplot(vacc, aes(x=dose, y=slots, group=type)) + geom_line(aes(color=type))+ geom_point(aes(color=type))+ theme_classic()ln |
Output:

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) # Inserting datavacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2), dose=rep(c("D1", "D2"),2), slots=c(33, 45, 66, 50)) # Change line color by group type of vaccineln <-ggplot(vacc, aes(x=dose, y=slots, group=type)) + geom_line(aes(color=type))+ geom_point(aes(color=type))+ theme_classic() # Adding line colors using brewer color paletteln+scale_color_brewer(palette="Set2") # Adding line colors using color manualln+scale_color_manual(values=c("green", "blue")) |
Output:

- 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) # Inserting datavacc <- data.frame(type=rep(c("Covishield", "Covaxin"), each=2), dose=rep(c("D1", "D2"),2), slots=c(33, 45, 66, 50)) # Change line color by group type of vaccineln <-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() # Legend at topln + theme(legend.position="top") # Legend at leftln + theme(legend.position="left") # Remove legendln + theme(legend.position="none") |
Output:



