Scatter plot & Histogram in R Programming

scatterplot

Scatterplot

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:

?mtcars

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 .  

attach(mtcars)

?plot

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

plot(wt,mpg)

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”)

hist()

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

hist(mpg,col=”red”)

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)

lines(density(mpg))

We are using ggplot2 package for more advanced visualization.

We install ggplot2 package :

install.packages(“ggplot2”)

We load ggplot2 package as:

library(ggplot2)

We are using qplot to plot graphs.

?qplot

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

dim(mtcars)

We can see top 6 observations of mtcars dataset

head(mtcars)

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)

qplot(mpg,wt,data=mtcars,facets=cyl~.,colour=factor(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 *

Scroll to Top