Welcome to this tutorial on Introduction to ggplot2.
In this tutorial, we will learn about
Need for data visualization
Basic plot function in R
ggplot2 package
To understand this tutorial, you should know,
Basics of Statistics and
Data frames
If not, please locate the relevant tutorials on R on this website.
This tutorial is recorded on
Ubuntu Linux OS version 16.04
R version 3.4.4
RStudio version 1.1.463
Install R version 3.2.0 or higher.
For this tutorial, we will use,
A data frame moviesData.csv
And a script file ggPlots.R.
Please download these files from the Code files link of this tutorial.
I have downloaded and moved these files to ggPlots folder.
This folder is located in myProject folder on my Desktop.
I have also set ggPlots folder as my Working Directory.
Now let us learn about visualization.
Visualization is an important tool for insight generation.
It is used to understand the data structure, identify outliers and find patterns.
There are 2 methods of data visualization in R:
Basics graphics and
Grammar of graphics (popularly known as ggplot2)
Let us switch to RStudio.
Open the script ggPlots.R in RStudio.
Let us run this script by clicking on the Source button.
movies data frame is loaded in the workspace.
This data frame will be used later in this tutorial.
First, we will plot a sine curve by taking equally spaced samples.
In the Source window, type the following commands.
Here, we have used the seq function to generate a sequence.
This sequence is from minus pi to plus pi with an interval of zero point one.
In plot command, the first argument is x and the second argument y is Sine of x.
Save the script and run the last three lines of code by pressing Ctrl + Enter keys simultaneously.
A plot of sine curve appears in the Plots window.
In the Plots window, click on the Zoom button to maximize the plot.
Now we will add some more layers in this plot.
Click on Close button to close this plot.
In the Source window, type the following commands.
Here, we have added main and ylab arguments to the plot function.
Run the current line.
In the Plots window, click on Zoom button to maximize the plot.
The title of the plot and label of Y-axis have been added to the plot.
Close this plot.
Now we will learn how to change the type of plot.
In the Source window, type the following commands.
Here, we have used the type argument and set it to l.
It means that the type of plot we need is lines.
col equal to blue, changes the colour of the plot to blue.
Run the current line.
The type and color of the plot have been changed.
Now, we will plot one more graph on the same plot.
Let us plot cosine of x along with sine of x on the same plot.
In the Source window, type the following commands.
This command plots sine of x using the plot function.
Next, we use lines function to plot cosine of x.
After the first line is plotted, the lines function is used.
It takes an additional vector cos of x as an input to draw the second line in the plot.
Run the last two lines of code by pressing Ctrl+Enter keys simultaneously.
The two graphs appear in the same plot window.
Here we can add a legend to the plot to differentiate between the multiple graphs.
For this, we will use legend function.
In the Source window, type the following command.
I will resize the Source window.
The first argument refers to the coordinates for placing the legend in our plot.
We have set the coordinates to topleft.
The second argument is the names to be given.
Since we have plotted sine and cosine functions, we will pass these two names as a vector.
Next, we have used the fill argument to specify the graphs by their colors.
Recall that, sine function is plotted in blue and cosine function in red.
I will resize the Plots window.
Run the last three lines of code by pressing Ctrl+Enter keys simultaneously.
In the Plots window, click on Zoom button to maximize the plot.
The two plots with their names appear in the same graph.
Close the plot.
So far, we have discussed the basic graphics in R language.
Now, we will learn about the grammar of graphics by using ggplot2 package.
ggplot2 package was created by Hadley Wickham in 2005.
It offers a powerful graphics language for creating elegant and complex plots.
Let us switch to RStudio.
I will resize the Plots window.
To use any package in R, we need to install and then load it.
As I have already installed ggplot2 package, I will load this directly.
If you have not installed the package, please use install dot packages function.
Please make sure that you are connected to the Internet while installing the packages.
To load this package, we will add the library at the top of the script.
In the Source window, scroll up to the top of the script.
Now, at the top of the script, type library and ggplot2 in parentheses.
Save the script and run this line.
Now, in the Source window, click on the next line after the legend function.
We will use movies data frame for exploring ggplot2 package.
Let us view the objects available in movies data frame.
In the Source window, type View and movies in parentheses.
Run the current line.
movies data frame opens in the Source window.
Now, we will create a simple scatter plot with two different objects of movies.
Remember, a scatter plot is a graph in which the values of two variables are plotted along the axes.
In the Source window, scroll from left to right to see the remaining objects of movies data frame.
Suppose, we want to visualize the correlation between critics_score and audience_score.
In the Source window, click on the script ggPlots.R
In the Source window, type the following command.
ggplot function takes three basic arguments:
Data Aesthetics Geometry
In ggplot function, we have used the following arguments:
data, which refers to the data set to be used for plotting.
We have set data equal to movies.
mapping, which is used to apply aesthetics mapping to the plot.
aes, which is used to specify the mapping of objects on X and Y axes.
We will learn more about aesthetics mapping later in this series.
geom underscore point is used to draw points defined by X and Y coordinates.
Run the current line.
Scatter plot appears in the Plots window.
In the Plots window, click on the Zoom button to maximize the plot.
We can see that there is a positive correlation between critics_score and audience_score.
Now we will learn how to save a plot generated by ggplot function.
Close this plot.
For saving the plots, there is a function named ggsave in ggplot2 package.
To know the syntax of ggsave function, we will access the Help section in RStudio.
In the Console window, type question mark ggsave and press Enter.
I will resize the Help window.
The first argument in this function is the filename.
Next, there is the argument named plot which means the plot to be saved.
By default, it will save the last plot.
Click on the Plots window.
Let us save our scatter plot with a name scatter underscore plot in png format.
In the Source window, type the following command.
Save the script and run the current line.
Click on the Files tab.
The plot has been saved in our current working directory.
Let us summarize what we have learnt.
In this tutorial, we have learnt about,
Need for data visualization
Basic plot function in R
ggplot2 package
We now suggest an assignment.
Consider the built-in data set mtcars. Find the numerical variables in this data set.
Make a scatter plot from the objects named mpg and wt in this data set.
Save the plot in .jpeg format.
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it.
We conduct workshops using Spoken Tutorials and give certificates.
Please contact us.
Please post your timed queries in this forum.
Please post your general queries in this forum.
The FOSSEE team coordinates the TBC project.
For more details, please visit these sites.
The Spoken Tutorial project is funded by , MHRD, Govt. of India
The script for this tutorial was contributed by Varshit Dubey (CoE Pune).
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.