Type to search

Scatter plot & Histogram in R Programming

To Know more about the Different Corporate Training & Consulting Visit our website www.Instrovate.com Or Email : info@instrovate.com or WhatsApp / Call at +91 74289 52788

R Programming

Scatter plot & Histogram in R Programming




A scatter plot is a useful way to visualize two quantitative variables in a dataset.

We are exploring mtcars dataset for some amazing data visualization.

We check mtcars dataset description by using following code:


It will open mtcars dataset description in Help window

We attached mtcars dataset in R. By attaching dataset ,we can use variables directly of mtcars .  



For plot graph between any variables , we use plot() .


It will plot between mpg and wt in Plots window.

We can add labels associated with axis and color by using this code :

plot(wt,mpg,col=”red”,ylab=”Miles/(US) gallon”,xlab=”Weight (1000 lbs)”)

We add label in x-axis by using xlab parameter and label in y-axis by using ylab parameter.

We can add naming of graph by using main parameter .

plot(wt,mpg,col=”red”,ylab=”Miles/(US) gallon”,xlab=”Weight (1000 lbs)”,main =”Miles per Gallon and Weight”)


It is used to plot histogram . It is an estimate of the probability distribution of a continuous variable.


It shows red frequency bars of mpg variable.

We add breaks parameter , it is used to break the x-axis in given number of breaks.

hist(mpg,col=”red”,breaks = 10)

We assign freq parameter to FALSE to draw lines in density plot.

We can also density plot by using this code:

hist(mpg,col=”red”,freq = FALSE)


We are using ggplot2 package for more advanced visualization.

We install ggplot2 package :


We load ggplot2 package as:


We are using qplot to plot graphs.


We determine dimensions of mtcars. It contains 32 rows and 11 columns.


We can see top 6 observations of mtcars dataset


We are using qplot() function to plot scatter plot between mpg and wt .

qplot(mpg, wt, data = mtcars)

We have cyl variable which shows number of cylinders. We add colors by cyl values as:

qplot(mpg, wt, data = mtcars, col= cyl)

We change cyl to factor . So , color select values on the basis of cyl value.

qplot(mpg, wt, data = mtcars, colour =  factor(cyl))

We add size and shape according to cyl values .

qplot(mpg, wt, data = mtcars,shape=factor(cyl), size = factor(cyl),  colour =  factor(cyl))

We added two variables by using facets parameter. It is used to add formula to add variables.

qplot(mpg, wt, data = mtcars, facets = vs ~ am)

Similarly , we add formula in facets parameter to represent am and cyl variables.

qplot(mpg, wt, data = mtcars, facets = am ~ cyl)


Published by Google DriveReport Abuse–Updated automatically every 5 minutes

Leave a Comment

Your email address will not be published. Required fields are marked *