How to plot line chart in R using ggplot?

How to plot line chart in R using ggplot?

How to plot line chart in R using ggplot?

This recipe helps you plot line chart in R using ggplot


Recipe Objective

A line chart is a type of chart which showcases information as a sequency of data points also known as 'markers' which are connected by staright line. It is mainly used to plot the relationship or trend of a categorical variable with respect to a numerical variable. ​

In this recipe we are going to use ggplot2 package to plot the required Line Chart. ggplot2 package is based on the book "Grammar of Graphics" by Wilkinson. This package provides flexibility while incorporating different themes and plot specification with a high level of abstraction. The package mainly uses aesthetic mapping and geometric objects as arguments. Different types of geometric objects include: ​

  1. geom_point() - for plotting points
  2. geom_bar() - for plotting bar graph
  3. geom_line() - for plotting line chart
  4. geom_histogram() - for plotting histogram

The basic syntax of gggplot2 plots is: ​

ggplot(data, mapping = aes(x =, y=)) + geometric object ​

where: ​

  1. data : Dataframe that is used to plot the chart
  2. mapping = aes() : aesthetic mapping which deals with controlling axis (x and y indicates the different variables)
  3. geometric object : Indicates the code for typeof plot you need to visualise.

This recipe demonstrates how to make a Line Chart using ggplot2.

STEP 1: Loading required library and creating dataset

We will use an example of no of schools established in 2 states between 1970 and 2014

# Data manipulation package library(tidyverse) # ggplot for data visualisation library(ggplot2) # years year = c('1970','1980', '1990', '2000', '2013', '2014') #no of schools in state1 corresponding to a particular year no_of_schools_state1 = c(15, 30, 60, 120, 240, 300) #creating a dataframe df = data.frame(year_x,state_y1) df
year_x	state_y1
1970	15
1980	30
1990	60
2000	120
2013	240
2014	300

STEP 2: Plotting a Line Chart using ggplot

We use geometric object as geom_line() to plot a line chart between two variables (Age and Customer ID)


  1. The + sign in the syntax earlier makes the code more readable and enables R to read further code without breaking it.
  2. We also use labs() function to give a title to the graph
  3. group arguement is kept at 1 as just one obsrvation is present for each year.
ggplot(df, mapping = aes(x = year, y = no_of_schools_state1, group = 1)) + geom_line() + labs(title = "No of schools vs Year")

Relevant Projects

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.