R/C2/Aesthetic-Mapping-in-ggplot2/English-timed
From Script | Spoken-Tutorial
Time | Narration |
00:01 | Welcome to this tutorial on Aesthetic Mapping in ggplot2. |
00:06 | In this tutorial, we will learn, |
00:10 | What is aesthetic |
00:12 | How to create plots using aesthetic |
00:16 | Tuning parameters in aesthetic |
00:20 | To understand this tutorial, you should know, |
00:24 | Basics of statistics |
00:27 | Basics of ggplot2 package |
00:30 | Data frames |
00:33 | If not, please locate the relevant tutorials on R on this website. |
00:40 | This tutorial is recorded on |
00:43 | Ubuntu Linux OS version 16.04 |
00:48 | R version 3.4.4 |
00:52 | RStudio version 1.1.463 |
00:57 | Install R version 3.2.0 or higher. |
01:03 | For this tutorial, we will use |
01:06 | A data frame moviesData.csv, and |
01:10 | A script file aesPlots.R. |
01:16 | Please download these files from the Code files link of this tutorial. |
01:23 | I have downloaded and moved these files to aesPlots folder. |
01:30 | This folder is located in myProject folder on my Desktop. |
01:36 | I have set aesPlots folder as my Working Directory. |
01:42 | Now let us see what is Aesthetic? |
01:46 | Aesthetic is a visual property of the objects in a plot. |
01:50 | It includes lines, points, symbols, colors, and position. |
01:57 | It is used to add customization to our plots. |
02:01 | Let us switch to RStudio. |
02:05 | Open the script aesPlots.R in RStudio. |
02:11 | Here, we are plotting a scatter plot between critics_score and audience_score of movies. |
02:19 | Run this script by clicking on the Source button. |
02:24 | Scatter plot appears in the Plots window. |
02:28 | movies data frame opens in the Source window. |
02:33 | In this scatter plot, each point refers to a particular movie. |
02:39 | Suppose we want to color these points according to the genre of the movies. |
02:45 | In the Source window, scroll from left to right. |
02:49 | As we can see that there are different genres like |
02:54 | Drama |
02:56 | Comedy |
02:58 | Horror |
03:00 | Documentary, etc. |
03:03 | So, we will assign a unique color to each genre. |
03:08 | Click on the script aesPlots.R |
03:12 | In the Source window, type the following commands. |
03:18 | Inside aes, we have added color argument and set it to genre. |
03:26 | Save the script and run the current line by pressing Ctrl+ Enter keys simultaneously. |
03:36 | Modified scatter plot appears in the Plots window. |
03:41 | In the Plots window, click on the Zoom button to maximize the plot. |
03:47 | We can see that each point is assigned a unique color according to its genre. |
03:54 | In the right side of the plot, we can view the mapping of genres with their colors. |
04:01 | Close this plot. |
04:03 | Now, we will learn how to draw a bar chart using ggplot function. |
04:09 | In the Source window, click on movies. |
04:13 | In the Source window, scroll from left to right. |
04:17 | Let us inspect the object named mpaa underscore rating in movies. |
04:26 | Click on the script aesPlots.R |
04:31 | In the Source window, type the following commands. |
04:36 | Run the last two lines of code. |
04:40 | mpaa_rating is a factor. |
04:45 | It has 6 levels like |
04:47 | G NC-17 PG PG-13 R, and Unrated. |
04:55 | So, our bar chart will have 6 different bars. |
05:00 | Each bar will represent the number of movies in each level. |
05:06 | In the Source window, type the following command. |
05:11 | Here, we have mapped mpaa underscore rating on X-axis. |
05:18 | Next, we have used geom underscore bar as we are plotting a bar chart. |
05:25 | Similarly, we can use |
05:27 | geom_line to draw a line chart |
05:31 | geom_boxplot to draw a box plot |
05:36 | Run the current line. |
05:39 | The bar chart appears in the Plots window. |
05:43 | Now, we will learn how to add labels to this bar chart. |
05:49 | In the Source window, after geom_bar(), type space plus sign and press Enter. |
05:58 | Now type the following commands. |
06:02 | Here, we have used labs argument to add label and title to the bar chart. |
06:09 | Run the current line. |
06:12 | The modified bar chart appears in the Plots window. |
06:17 | We can see that most of the movies have been rated as R in mpaa_rating. |
06:26 | Suppose, in this bar chart, we want to view the distribution of movies by genre. |
06:33 | In the Source window, type the following command. |
06:37 | Inside aes, we have added fill argument and set it to genre. |
06:44 | Run the current line. |
06:47 | The modified bar chart appears in the Plots window. |
06:52 | In the Plots window, click on the Zoom button to maximize the plot. |
06:58 | There are seven different colors in each bar. |
07:03 | Besides the plot, the meaning of each color has been given. |
07:09 | Close the plot. |
07:12 | In the Source window, click on movies data frame. |
07:17 | Now we will plot a histogram for the object named as runtime in movies. |
07:25 | Recall that, we have already learned how to plot a histogram using hist function. |
07:32 | Now we will use ggplot2 package to plot a histogram. |
07:38 | Click on the script aesPlots.R |
07:43 | In the Source window, type the following command. |
07:48 | Save the script and run the current line. |
07:53 | There are some warning messages, which we will ignore for now. |
07:58 | The histogram appears in the Plots window. |
08:02 | Let us summarize what we have learnt. |
08:06 | In this tutorial, we have learnt, |
08:09 | What is aesthetic |
08:11 | How to create plots using aesthetic |
08:15 | Tuning parameters in aesthetic |
08:19 | We now suggest an assignment. |
08:22 | Using built-in data set mtcars, draw a bar chart from the object cyl. |
08:30 | Add suitable labels to this bar chart. |
08:34 | The video at the following link summarises the Spoken Tutorial project. |
08:39 | Please download and watch it. |
08:42 | We conduct workshops using Spoken Tutorials and give certificates. |
08:47 | Please contact us. |
08:49 | Please post your timed queries in this forum. |
08:53 | Please post your general queries in this forum. |
08:57 | The FOSSEE team coordinates the TBC project. |
09:01 | For more details, please visit these sites. |
09:05 | The Spoken Tutorial project is funded by MHRD, Govt. of India |
09:12 | The script for this tutorial was contributed by Varshit Dubey (CoE Pune). |
09:19 | This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching. |