Welcome to this tutorial on Plotting bar charts and scatter plot.
In this tutorial, we will learn how to:
Plot bar charts
Plot scatter plot
Find the correlation coefficient between two objects.
To understand this tutorial, you should know,
Data frames in R
Basics of Statistics
If not, please locate the relevant tutorials 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
A script file barPlots.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 Plots folder as my Working Directory.
Let us switch to Rstudio.
Open the script barPlots.R in RStudio.
Run this script by clicking on Source button.
movies data frame opens in the Source window.
It has 600 observations of 31 variables.
In the Source window, scroll from left to right.
This will enable us to see the remaining objects of movies data frame.
Now, we will learn how to draw a bar chart of the object named imdb underscore rating in movies.
A bar chart represents data in rectangular bars with length of the bar proportional to the value of the variable.
R uses the function barplot to create bar charts.
Let us switch to RStudio.
For the sake of simplicity, we are considering only the first 20 observations of movies to draw a bar chart.
Click on the script barPlots.R
In the Source window, type the following command.
Save the script and run the current line by pressing Ctrl + Enter keys simultaneously.
Let me resize the Source window.
moviesSub with 20 observations is loaded in the Environment.
Now, we draw a bar chart of imdb_rating for these movies.
In the Source window, type the following command.
Here, we have used the following arguments:
moviesSub dollar sign imdb underscore rating is the data for plotting
ylab and xlab for adding labels to the respective axes.
col to set the color of bins
ylim to set the range of values on Y-axis
main for adding a title to the bar chart.
Run the current line.
The bar chart is displayed with Movies on X-axis and their imdb_rating on Y-axis.
In the Plots window, click on Zoom to maximize the plot.
This particular movie has an IMDB rating of approximately 6.
Similarly, this particular movie has an IMDB rating of approximately 8.
However, we do not know the name of the movies.
So, we will add more arguments in barplot function to show the names of movies on X-axis.
Close this plot.
In the Source window, type the following command.
Here, we have used the argument names.arg and set it to title.
Remember, title column in moviesSub contains the names of movies.
Run the current line.
In the Plots window, click on Zoom to maximize the plot.
Now, the names of movies are displayed on the X-axis.
But not for all movies.
This is due to the point that the names are too long to be accommodated.
That’s why, we will make these names perpendicular to X-axis.
Close this plot.
In the Source window, type the following command.
Here, we have used las argument.
las equal to 2 produces labels which are at right angles to the axis.
Run the current line.
In the Plots window, click on Zoom to maximize the plot.
Now the names for all the movies are displayed on X-axis.
For example, Filly Brown has an IMDB rating of approximately 6.
However, longer names are being truncated.
We can add more arguments to barplot function for adjusting labels.
For more information, please refer to the Additional Material section on this website.
Close this plot.
In the Source window, click on movies.
Let us analyze the relation between imdb underscore rating
and audience underscore score.
For this, we will draw a scatter plot with these two objects by using plot function.
Remember, we have already learnt how to plot a single object.
Scatter plot is a graph in which the values of two variables are plotted along two axes.
The pattern of the resulting points reveals the correlation.
Let us switch to RStudio.
In the Source window, click on the script barPlots.R
In the Source window, type the following command.
Here, we have kept imdb underscore rating on the X-axis and audience underscore score on the Y-axis.
As imdb underscore rating of any movie varies between 0 and 10,
we have set the range of values on X-axis from 0 to 10.
Similarly, we have set the range of values on Y-axis from 0 to 100.
Save the script and run the current line.
In the Plots window, click on Zoom to maximize the plot.
We can observe that the movies having higher imdb underscore rating has a high audience underscore score.
Close this plot.
Now we will learn how to calculate the correlation coefficient between imdb underscore rating and audience underscore score.
For this, we use cor function.
In the Source window, type the following command.
Save the script and run the current line.
The correlation coefficient between imdb underscore rating and audience underscore score is evaluated as 0.865.
The value of correlation coefficient is always between -1 and +1.
A positive value indicates that the variables are positively related.
Let us summarize what we have learnt.
In this tutorial, we have learnt how to:
Plot bar charts
Plot scatter plot
Find the correlation coefficient between two objects
We now suggest an assignment.
Read the file moviesData.csv.
Create a bar chart of critics underscore score for the first 10 movies.
Create a scatter plot of imdb underscore rating and imdb underscore num underscore votes to see their relation.
Save both the plots.
The video at the following link summarises the Spoken Tutorial project.
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.