Welcome to the spoken tutorial on Plotting Histograms and Pie Chart.
In this tutorial, we will learn how to:
Plot histograms
Plot pie chart
Save plots
To understand this tutorial, you should know,
Data frames in R
Basics of Statistics
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.456
Install R version 3.2.0 or higher.
For this tutorial, we will use
A data frame moviesData.csv
A script file myPlots.R.
Please download these files from the Code files link of this tutorial.
I have downloaded and moved these files to Plots folder.
This folder is located in myProject folder on my Desktop.
I have also set this folder as my Working Directory.
Let us switch to Rstudio.
Open the script myPlots.R in RStudio.
Run this script by clicking on Source button.
movies data frame opens in the Source window.
This data frame has 600 rows and 31 columns.
It means this data frame has 600 observations of 31 variables.
To know about more this dataframe, please refer to the Additional Material section on this website.
In the Source window, scroll from left to right to see the remaining objects of movies data frame.
Now we will learn how to plot a histogram of the object named runtime in movies.
A histogram is
A visual representation of the distribution of a dataset.
Used to plot the frequency of score occurrences in a continuous dataset
Let us switch to RStudio.
Click on the script myPlots.R
In the Source window, type hist, within parentheses movies dollar sign runtime.
Save the script and run the current line by pressing Run button.
The histogram appears in the Plots window.
Click on Zoom to maximize this plot.
In the histogram there are 9 bins.
Height of a bin represents the number of observations lying in that interval.
Now, we will learn how to add labels to this histogram.
Also, we will change the color of bins in this histogram.
For this, we will add more arguments to the hist function.
Close the histogram.
In the Source window, type the following command.
Here, we have used the following arguments:
main for adding title to the histogram
xlab for adding label to the x axis
xlim to set the range of values on x axis
col to set the color of bins
Run the current line.
In the Files and Plots window, click on Zoom to maximize the plot.
The labels and the title of histogram have been changed.
We can observe that most of the movies have the runtime of around 75 to 125 minutes.
Now we will modify the number of breaks in the histogram.
We can make the groups finer or coarser by modifying the number of breaks.
Close this plot.
Let us add breaks argument in hist function and set it to 4.
In the Source window, type the following command.
Save the script and run the current line.
In the Files and Plots window, click on Zoom to maximize the plot.
Now, there are five bins in the histogram.
Remember we had set breaks to be 4.
Close the plot.
In the Source window, click on movies.
I am scrolling from right to left.
Now, we will learn how to create a pie chart from the object genre in the movies data frame.
A pie chart is
A circular chart.
Divided into wedge-like sectors, illustrating proportion.
The total value of the pie is always 100 percent.
Let us switch to RStudio.
First, we will make a table of the number of different genres.
For this, we use table function.
Click on the script myPlots.R
In the Source window, type the following command.
Run the last two lines.
We can see that there are 65 movies in Action & Adventure and 87 movies in Comedy.
Now, we draw a pie chart with genreCount.
Click on the script myPlots.R
In the Source window, type pie within parentheses genreCount.
Run the current line.
The pie chart appears in the Plots window.
Click on Zoom to maximize this pie chart.
We can see that Drama has largest share of movies.
Now, we will change the color of border in this pie chart.
For this, we will add more arguments to the pie function.
Cose this plot.
In the Source window, type the following command.
Save the script and run the current line.
The modified pie chart appears in the Plots window.
Click on Zoom to maximize the plot.
Now, we will learn how to save this pie chart as an image on our computer.
Close this plot.
I am resizing the Files and Plots window.
In the Plots window, click on Export.
From the drop-down menu, select Save as Image.
A window named Save Plot as Image appears.
You can select the format in which you want to save your image.
I am saving it in JPEG format.
Below the Image format option, you can select the directory where you want to save your image.
By default, RStudio will save the image in the directory where the script has been placed.
I will save the image in this folder.
Below the directory, in the field File name, you can write the name of the image to be saved.
I am saving the image with the name pieChart with capital C.
In the top right corner of this window, you can modify the dimensions of this image.
I am setting the width to 650.
Please make sure that Maintain aspect ratio is checked.
Below Maintain aspect ratio, click on Update Preview button.
The image with larger dimensions is shown.
Finally, click on Save.
I am resizing the Files and Plots window.
Now, click on the Files tab.
The image pieChart.jpeg has been saved in the Plots folder.
Click on this image to see the saved pie chart.
Let us summarize what we have learnt.
In this tutorial, we have learnt how to:
Plot histograms
Plot pie chart
Save plots
We now suggest an assignment.
Read the file moviesData.csv. Create a histogram of the object named imdb_num_votes in this file.
Create a pie chart of the object mpaa_rating.
Save both the plots.
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 NMEICT, MHRD, Govt. of India
The script for this tutorial was contributed by Tushar Bajaj (TISS Mumbai).
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.