LOADING

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

Data Analytics R Programming

Scatter plot & Histogram in R Programming

Share

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

Scatter plot & Histogram in R Programming 29

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.

Scatter plot & Histogram in R Programming 30

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.

Scatter plot & Histogram in R Programming 31

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

Scatter plot & Histogram in R Programming 32

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.

Scatter plot & Histogram in R Programming 33

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

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

Scatter plot & Histogram in R Programming 34

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)

Scatter plot & Histogram in R Programming 35

We can see top 6 observations of mtcars dataset

head(mtcars)

Scatter plot & Histogram in R Programming 36

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

qplot(mpg, wt, data = mtcars)

Scatter plot & Histogram in R Programming 37

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

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

Scatter plot & Histogram in R Programming 38

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

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

Scatter plot & Histogram in R Programming 39

We add size and shape according to cyl values .

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

Scatter plot & Histogram in R Programming 40

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)

Scatter plot & Histogram in R Programming 41

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

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

Scatter plot & Histogram in R Programming 42

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

Scatter plot & Histogram in R Programming 43

Published by Google DriveReport Abuse–Updated automatically every 5 minutes