Difference between revisions of "R/C2/Aesthetic-Mapping-in-ggplot2/English"
Sudhakarst (Talk | contribs) |
Nancyvarkey (Talk | contribs) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 3: | Line 3: | ||
'''Author''': Varshit Dubey (CoE Pune) and Sudhakar Kumar (IIT Bombay) | '''Author''': Varshit Dubey (CoE Pune) and Sudhakar Kumar (IIT Bombay) | ||
− | '''Keywords''': R, RStudio, ggplot, aesthetic, mapping, video tutorial | + | '''Keywords''': R, RStudio, ggplot, aesthetic, mapping, video tutorial. |
− | + | ||
{| border =1 | {| border =1 | ||
Line 24: | Line 23: | ||
|- | |- | ||
− | || | + | || Show slide |
− | + | ||
− | Show slide | + | |
Pre-requisites | Pre-requisites | ||
Line 50: | Line 47: | ||
Download Files | Download Files | ||
|| For this tutorial, we will use | || For this tutorial, we will use | ||
− | * A '''data frame | + | * A '''data frame moviesData.csv''', and |
* A '''script''' file '''aesPlots.R'''. | * A '''script''' file '''aesPlots.R'''. | ||
Line 67: | Line 64: | ||
|- | |- | ||
|| | || | ||
− | || Now let us see what is '''Aesthetic | + | || Now let us see what is '''Aesthetic'''? |
|- | |- | ||
|| Show slide | || Show slide | ||
Line 75: | Line 72: | ||
* '''Aesthetic '''is a visual property of the objects in a plot. | * '''Aesthetic '''is a visual property of the objects in a plot. | ||
* It includes lines, points, symbols, colors, and position. | * It includes lines, points, symbols, colors, and position. | ||
− | * It is used to add '''customization '''to our plots. | + | * It is used to add '''customization''' to our plots. |
|- | |- | ||
− | || | + | || Cursor on the interface. |
|| Let us switch to '''RStudio'''. | || Let us switch to '''RStudio'''. | ||
|- | |- | ||
|| Highlight '''aesPlots.R''' in the '''Files '''window''' '''of '''RStudio ''' | || Highlight '''aesPlots.R''' in the '''Files '''window''' '''of '''RStudio ''' | ||
− | || Open the '''script aesPlots.R '''in''' RStudio'''. | + | || Open the '''script aesPlots.R''' in '''RStudio'''. |
|- | |- | ||
|| Highlight '''ggplot''' function in the '''Source''' window | || Highlight '''ggplot''' function in the '''Source''' window | ||
Line 94: | Line 91: | ||
|- | |- | ||
|| Highlight '''movies''' in the '''Source''' window | || Highlight '''movies''' in the '''Source''' window | ||
− | || '''movies ''' | + | || '''movies data frame''' opens in the '''Source''' window. |
|- | |- | ||
|| Highlight the plot in the '''Plots''' window | || Highlight the plot in the '''Plots''' window | ||
Line 105: | Line 102: | ||
|- | |- | ||
|| Highlight '''genre''' in the '''Source''' window | || Highlight '''genre''' in the '''Source''' window | ||
− | || As we can see that there are different genres like | + | || As we can see that there are different '''genres''' like |
* '''Drama''' | * '''Drama''' | ||
* '''Comedy''' | * '''Comedy''' | ||
Line 111: | Line 108: | ||
* '''Documentary''', etc. | * '''Documentary''', etc. | ||
− | So, we will assign a unique color to each genre. | + | So, we will assign a unique color to each '''genre'''. |
|- | |- | ||
|| Highlight the '''script aesPlots.R''' in the '''Source''' window | || Highlight the '''script aesPlots.R''' in the '''Source''' window | ||
|| Click on the '''script aesPlots.R''' | || Click on the '''script aesPlots.R''' | ||
|- | |- | ||
− | | | + | || '''[RStudio]''' |
'''ggplot(data = movies, ''' | '''ggplot(data = movies, ''' | ||
Line 127: | Line 124: | ||
'''geom_point()''' | '''geom_point()''' | ||
− | | | + | ||In the '''Source''' window, type the following '''commands'''. |
|- | |- | ||
− | | | + | ||Highlight '''aes''' in the '''Source''' window |
− | | | + | ||Inside '''aes''', we have added '''color argument''' and set it to '''genre'''. |
|- | |- | ||
|| Highlight '''run''' button in the '''Source''' window | || Highlight '''run''' button in the '''Source''' window | ||
− | || Save the '''script''' and run the current line by pressing '''Ctrl+Enter''' keys | + | || Save the '''script''' and run the current line by pressing '''Ctrl+ Enter''' keys simultaneously. |
|- | |- | ||
|| Highlight '''Plots''' window | || Highlight '''Plots''' window | ||
Line 145: | Line 142: | ||
|| Highlight the plot | || Highlight the plot | ||
|| We can see that each point is assigned a unique '''color''' according to its '''genre'''. | || 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. | In the right side of the plot, we can view the mapping of '''genres''' with their colors. | ||
Line 152: | Line 150: | ||
|- | |- | ||
|| Highlight '''ggplot''' in the '''Source''' window | || Highlight '''ggplot''' in the '''Source''' window | ||
− | || Now, we will learn how to draw a '''bar chart''' using '''ggplot ''' | + | || Now, we will learn how to draw a '''bar chart''' using '''ggplot function'''. |
|- | |- | ||
|| Highlight '''movies''' in the '''Source''' window | || Highlight '''movies''' in the '''Source''' window | ||
Line 161: | Line 159: | ||
|- | |- | ||
|| Highlight '''mpaa_rating''' in the '''Source''' window | || Highlight '''mpaa_rating''' in the '''Source''' window | ||
− | || Let us inspect the | + | || Let us inspect the '''object''' named '''mpaa underscore rating''' in '''movies'''. |
|- | |- | ||
|| Highlight the '''script aesPlots.R''' in the '''Source''' window | || Highlight the '''script aesPlots.R''' in the '''Source''' window | ||
Line 171: | Line 169: | ||
'''levels(movies$mpaa_rating)''' | '''levels(movies$mpaa_rating)''' | ||
− | || In the '''Source''' window, type the following commands. | + | || In the '''Source''' window, type the following '''commands'''. |
|- | |- | ||
|| Highlight '''run''' button in the '''Source''' window | || Highlight '''run''' button in the '''Source''' window | ||
Line 179: | Line 177: | ||
|| '''mpaa_rating '''is a '''factor'''. | || '''mpaa_rating '''is a '''factor'''. | ||
− | It has 6''' ''' | + | It has 6 '''levels''' like |
* '''G''' | * '''G''' | ||
* '''NC-17''' | * '''NC-17''' | ||
Line 191: | Line 189: | ||
|| So, our '''bar chart''' will have 6 different bars. | || So, our '''bar chart''' will have 6 different bars. | ||
− | Each bar will represent the number of movies in each level. | + | |
+ | Each bar will represent the number of movies in each '''level'''. | ||
|- | |- | ||
|| '''[RStudio]''' | || '''[RStudio]''' | ||
− | '''ggplot(data = movies | + | '''ggplot(data = movies''', |
'''mapping = aes(x = mpaa_rating)) + ''' | '''mapping = aes(x = mpaa_rating)) + ''' | ||
'''geom_bar()''' | '''geom_bar()''' | ||
− | || In the '''Source''' window, type the following command. | + | || In the '''Source''' window, type the following '''command'''. |
|- | |- | ||
|| Highlight '''aes''' in the '''Source''' window | || Highlight '''aes''' in the '''Source''' window | ||
− | || Here, we have mapped '''mpaa | + | || Here, we have mapped '''mpaa underscore rating''' on X-axis. |
|- | |- | ||
|| Highlight '''geom_bar''' in the Source window | || Highlight '''geom_bar''' in the Source window | ||
− | || Next, we have used '''geom | + | || Next, we have used '''geom underscore bar''' as we are plotting a '''bar chart'''. |
Similarly, we can use | Similarly, we can use | ||
Line 225: | Line 224: | ||
Type''' Space '''plus sign >> press Enter. | Type''' Space '''plus sign >> press Enter. | ||
− | || In the '''Source''' window, after '''geom_bar(), '''type space plus sign and press '''Enter'''. | + | || In the '''Source''' window, after '''geom_bar(), '''type '''space plus sign''' and press '''Enter'''. |
|- | |- | ||
|| '''[RStudio]''' | || '''[RStudio]''' | ||
Line 232: | Line 231: | ||
'''title = "Count of mpaa_rating")''' | '''title = "Count of mpaa_rating")''' | ||
− | || | + | || Now type the following '''commands'''. |
− | + | ||
− | Now type the following | + | |
|- | |- | ||
|| Highlight labs in the Source window | || Highlight labs in the Source window | ||
− | || Here, we have used '''labs''' | + | || Here, we have used '''labs argument''' to add label and title to the bar chart. |
|- | |- | ||
|| Highlight '''run''' button in the '''Source''' window | || Highlight '''run''' button in the '''Source''' window | ||
Line 261: | Line 258: | ||
'''title = "Count of mpaa_rating by genre")''' | '''title = "Count of mpaa_rating by genre")''' | ||
− | || In the '''Source''' window, type the following command. | + | || In the '''Source''' window, type the following '''command'''. |
|- | |- | ||
|| Highlight '''fill''' in the '''Source''' window | || Highlight '''fill''' in the '''Source''' window | ||
− | || Inside '''aes''', we have added '''fill''' | + | || Inside '''aes''', we have added '''fill argument''' and set it to '''genre'''. |
|- | |- | ||
|| Highlight '''run''' button in the '''Source''' window | || Highlight '''run''' button in the '''Source''' window | ||
Line 276: | Line 273: | ||
|- | |- | ||
|| Highlight fifth bar in the '''Plots''' window | || Highlight fifth bar in the '''Plots''' window | ||
− | || There are seven different colors in each bar. Besides the plot, the meaning of each color has been given | + | || There are seven different colors in each bar. |
+ | |||
+ | |||
+ | Besides the plot, the meaning of each color has been given. | ||
|- | |- | ||
|| Click on X button to close the plot. | || Click on X button to close the plot. | ||
Line 285: | Line 285: | ||
|- | |- | ||
|| Highlight '''runtime '''in the '''Source''' window | || Highlight '''runtime '''in the '''Source''' window | ||
− | || Now we will plot a '''histogram '''for | + | || Now we will plot a '''histogram '''for the '''object''' named as '''runtime''' in '''movies'''. |
− | |||
− | Now we will use '''ggplot2 ''' | + | Recall that, we have already learned how to plot a histogram using '''hist function'''. |
+ | |||
+ | |||
+ | Now we will use '''ggplot2 package''' to plot a '''histogram'''. | ||
|- | |- | ||
|| Highlight the '''script aesPlots.R''' in the '''Source''' window. | || Highlight the '''script aesPlots.R''' in the '''Source''' window. | ||
Line 305: | Line 307: | ||
'''title = "Distribution of movies' runtime")''' | '''title = "Distribution of movies' runtime")''' | ||
− | || In the '''Source''' window, type the following command. | + | || In the '''Source''' window, type the following '''command'''. |
|- | |- | ||
|| Highlight '''run''' button in the '''Source''' window | || Highlight '''run''' button in the '''Source''' window | ||
Line 323: | Line 325: | ||
Summary | Summary | ||
|| In this tutorial, we have learnt, | || In this tutorial, we have learnt, | ||
− | * What is''' aesthetic''' | + | * What is '''aesthetic''' |
* How to create plots using '''aesthetic''' | * How to create plots using '''aesthetic''' | ||
* Tuning parameters in '''aesthetic''' | * Tuning parameters in '''aesthetic''' | ||
Line 333: | Line 335: | ||
|| We now suggest an assignment. | || We now suggest an assignment. | ||
− | * Using built-in data set | + | * Using '''built-in data set mtcars''', draw a bar chart from the '''object cyl'''. |
* Add suitable labels to this bar chart. | * Add suitable labels to this bar chart. | ||
Latest revision as of 14:31, 23 September 2019
Title of the script: Aesthetic Mapping in ggplot2
Author: Varshit Dubey (CoE Pune) and Sudhakar Kumar (IIT Bombay)
Keywords: R, RStudio, ggplot, aesthetic, mapping, video tutorial.
Visual Cue | Narration |
Show slide
Opening Slide |
Welcome to this tutorial on Aesthetic Mapping in ggplot2. |
Show slide
Learning Objective |
In this tutorial, we will learn,
|
Show slide
Pre-requisites |
To understand this tutorial, you should know,
If not, please locate the relevant tutorials on R on this website. |
Show slide
System Specifications |
This tutorial is recorded on
Install R version 3.2.0 or higher. |
Show slide
Download Files |
For this tutorial, we will use
Please download these files from the Code files link of this tutorial. |
[Computer screen]
Highlight moviesData.csv and aesPlots.R in the folder aesPlots Point to aesPlots folder. |
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? | |
Show slide
What is Aesthetics |
|
Cursor on the interface. | Let us switch to RStudio. |
Highlight aesPlots.R in the Files window of RStudio | Open the script aesPlots.R in RStudio. |
Highlight ggplot function in the Source window | Here, we are plotting a scatter plot between critics_score and audience_score of movies. |
Highlight the Source button | Run this script by clicking on the Source button. |
Highlight Plots window | Scatter plot appears in the Plots window. |
Highlight movies in the Source window | movies data frame opens in the Source window. |
Highlight the plot in the Plots 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. |
Highlight the scroll bar in the Source window | In the Source window, scroll from left to right. |
Highlight genre in the Source window | As we can see that there are different genres like
So, we will assign a unique color to each genre. |
Highlight the script aesPlots.R in the Source window | Click on the script aesPlots.R |
[RStudio]
ggplot(data = movies, mapping = aes(x = critics_score, y = audience_score, color = genre)) + geom_point() |
In the Source window, type the following commands. |
Highlight aes in the Source window | Inside aes, we have added color argument and set it to genre. |
Highlight run button in the Source window | Save the script and run the current line by pressing Ctrl+ Enter keys simultaneously. |
Highlight Plots window | Modified scatter plot appears in the Plots window. |
Click on Zoom button to maximize the plot.
Highlight Plots window |
In the Plots window, click on the Zoom button to maximize the plot. |
Highlight the plot | We can see that each point is assigned a unique color according to its genre.
|
Click on X button to close the plot. | Close this plot. |
Highlight ggplot in the Source window | Now, we will learn how to draw a bar chart using ggplot function. |
Highlight movies in the Source window | In the Source window, click on movies. |
Highlight the scroll bar in the Source window | In the Source window, scroll from left to right. |
Highlight mpaa_rating in the Source window | Let us inspect the object named mpaa underscore rating in movies. |
Highlight the script aesPlots.R in the Source window | Click on the script aesPlots.R |
[RStudio]
str(movies$mpaa_rating) levels(movies$mpaa_rating) |
In the Source window, type the following commands. |
Highlight run button in the Source window | Run the last two lines of code. |
Highlight output in the Console window | mpaa_rating is a factor.
It has 6 levels like
|
Highlight output in the Console window | So, our bar chart will have 6 different bars.
|
[RStudio]
ggplot(data = movies, mapping = aes(x = mpaa_rating)) + geom_bar() |
In the Source window, type the following command. |
Highlight aes in the Source window | Here, we have mapped mpaa underscore rating on X-axis. |
Highlight geom_bar in the Source window | Next, we have used geom underscore bar as we are plotting a bar chart.
Similarly, we can use
|
Highlight run button in the Source window | Run the current line. |
Highlight Plots window | The bar chart appears in the Plots window. |
Highlight plot in the Plots window | Now, we will learn how to add labels to this bar chart. |
Point to geom_bar().
Type Space plus sign >> press Enter. |
In the Source window, after geom_bar(), type space plus sign and press Enter. |
[RStudio]
labs(y = "Rating count", title = "Count of mpaa_rating") |
Now type the following commands. |
Highlight labs in the Source window | Here, we have used labs argument to add label and title to the bar chart. |
Highlight run button in the Source window | Run the current line. |
Highlight Plots window | The modified bar chart appears in the Plots window. |
Highlight fifth bar 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. |
[RStudio]
ggplot(data = movies, mapping = aes(x = mpaa_rating, fill = genre)) + geom_bar() + labs(y = "Rating count", title = "Count of mpaa_rating by genre") |
In the Source window, type the following command. |
Highlight fill in the Source window | Inside aes, we have added fill argument and set it to genre. |
Highlight run button in the Source window | Run the current line. |
Highlight Plots window | The modified bar chart appears in the Plots window. |
Highlight Plots window | In the Plots window, click on the Zoom button to maximize the plot. |
Highlight fifth bar in the Plots window | There are seven different colors in each bar.
|
Click on X button to close the plot. | Close the plot. |
Highlight movies in the Source window | In the Source window, click on movies data frame. |
Highlight runtime in the Source window | Now we will plot a histogram for the object named as runtime in movies.
|
Highlight the script aesPlots.R in the Source window. | Click on the script aesPlots.R |
[RStudio]
ggplot(data = movies, mapping = aes(x = runtime)) + geom_histogram() + labs(x = "Runtime of movies", title = "Distribution of movies' runtime") |
In the Source window, type the following command. |
Highlight run button in the Source window | Save the script and run the current line. |
Highlight output in the Console window | There are some warning messages, which we will ignore for now. |
Highlight Plots window | The histogram appears in the Plots window. |
Let us summarize what we have learnt. | |
Show slide
Summary |
In this tutorial, we have learnt,
|
Show slide
Assignment |
We now suggest an assignment.
|
Show slide
About the Spoken Tutorial Project |
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it. |
Show slide
Spoken Tutorial Workshops |
We conduct workshops using Spoken Tutorials and give certificates.
|
Show Slide
Forum to answer questions |
Please post your timed queries in this forum. |
Show Slide
Forum to answer questions |
Please post your general queries in this forum. |
Show Slide
Textbook Companion |
The FOSSEE team coordinates the TBC project.
For more details, please visit these sites. |
Show Slide
Acknowledgment |
The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India |
Show Slide
Thank You |
The script for this tutorial was contributed by Varshit Dubey (CoE Pune).
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching. |