Welcome to this tutorial on Aesthetic Mapping in ggplot2.
In this tutorial, we will learn,
What is aesthetic
How to create plots using aesthetic
Tuning parameters in aesthetic
To understand this tutorial, you should know,
Basics of statistics
Basics of ggplot2 package
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 aesPlots.R.
Please download these files from the Code files link of this tutorial.
I have downloaded and moved these files to aesPlots folder.
This folder is located in myProject folder on my Desktop.
I have set aesPlots folder as my Working Directory.
Now let us see what is Aesthetic?
Aesthetic is a visual property of the objects in a plot.
It includes lines, points, symbols, colors, and position.
It is used to add customization to our plots.
Let us switch to RStudio.
Open the script aesPlots.R in RStudio.
Here, we are plotting a scatter plot between critics_score and audience_score of movies.
Run this script by clicking on the Source button.
Scatter plot appears in the Plots window.
movies data frame opens in the Source window.
In this scatter plot, each point refers to a particular movie.
Suppose we want to color these points according to the genre of the movies.
In the Source window, scroll from left to right.
As we can see that there are different genres like
Drama
Comedy
Horror
Documentary, etc.
So, we will assign a unique color to each genre.
Click on the script aesPlots.R
In the Source window, type the following commands.
Inside aes, we have added color argument and set it to genre.
Save the script and run the current line by pressing Ctrl+ Enter keys simultaneously.
Modified scatter plot appears in the Plots window.
In the Plots window, click on the Zoom button to maximize the plot.
We can see that each point is assigned a unique color according to its genre.
In the right side of the plot, we can view the mapping of genres with their colors.
Close this plot.
Now, we will learn how to draw a bar chart using ggplot function.
In the Source window, click on movies.
In the Source window, scroll from left to right.
Let us inspect the object named mpaa underscore rating in movies.
Click on the script aesPlots.R
In the Source window, type the following commands.
Run the last two lines of code.
mpaa_rating is a factor.
It has 6 levels like
G NC-17 PG PG-13 R, and Unrated.
So, our bar chart will have 6 different bars.
Each bar will represent the number of movies in each level.
In the Source window, type the following command.
Here, we have mapped mpaa underscore rating on X-axis.
Next, we have used geom underscore bar as we are plotting a bar chart.
Similarly, we can use
geom_line to draw a line chart
geom_boxplot to draw a box plot
Run the current line.
The bar chart appears in the Plots window.
Now, we will learn how to add labels to this bar chart.
In the Source window, after geom_bar(), type space plus sign and press Enter.
Now type the following commands.
Here, we have used labs argument to add label and title to the bar chart.
Run the current line.
The modified bar chart appears in the Plots window.
We can see that most of the movies have been rated as R in mpaa_rating.
Suppose, in this bar chart, we want to view the distribution of movies by genre.
In the Source window, type the following command.
Inside aes, we have added fill argument and set it to genre.
Run the current line.
The modified bar chart appears in the Plots window.
In the Plots window, click on the Zoom button to maximize the plot.
There are seven different colors in each bar.
Besides the plot, the meaning of each color has been given.
Close the plot.
In the Source window, click on movies data frame.
Now we will plot a histogram for the object named as runtime in movies.
Recall that, we have already learned how to plot a histogram using hist function.
Now we will use ggplot2 package to plot a histogram.
Click on the script aesPlots.R
In the Source window, type the following command.
Save the script and run the current line.
There are some warning messages, which we will ignore for now.
The histogram appears in the Plots window.
Let us summarize what we have learnt.
In this tutorial, we have learnt,
What is aesthetic
How to create plots using aesthetic
Tuning parameters in aesthetic
We now suggest an assignment.
Using built-in data set mtcars, draw a bar chart from the object cyl.
Add suitable labels to this bar chart.
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.