Basic plotting in R
R has very good mechanism to do data plotting.
Let’s explore few of the commands present in R
To plot simple graph-
library(datasets)
head(iris, 300)
plot(iris$Species)
The best part of plot() is that it automatically understands the kind of datatype we are handling and automatically plots the graphs-
In the below example, we are plotting distribution of petal.width-
plot(iris$Petal.Width)
Let’s take one more example of plotting-
plot(iris$Species , iris$Petal.Width)
R will automatically determine that first variable is categorical and second is a numeric and will make the most common plot which is this case is a box plot
The box plot reveals us the quantile-wise distribution of the data.
The box represents Inter Quantile Range IQR(P25 to P75) with dark line being the median.
Lower line is P25–1.5*IQR
Upper line is P75 + 1.5IQR
The dots denote outliers whose values are completely drifting apart from the normal values.
With R , you get different options aswell to decorate the plot in a proper fashion.
Bar Chart
A basic plotting of bar chart can be done via barplot() function
library(datasets)
head(mtcars, 100)
barplot(mtcars$carb)
The above chart displays the values of carburetors in all the models in a graphical representation.
Now, let’s get an aggregated view of carburetors on how many models are having the same value carburetors
library(datasets)
head(mtcars, 100)
?mtcars
barplot(mtcars$carb)
carbFreq <- table(mtcars$carb)
barplot(carbFreq)
The above chart represents number of cars having a given value of carburetors.
table() function takes input as one variable as an input and creates a dataframe which has the frequency of that value occuring in that dataset.
Histogram
Histogram gives us a plot of the frequency of values lying in a range-
library(datasets)
head(iris, 200)
par(mfrow = c(4 , 1))
hist(iris$Sepal.Length)
hist(iris$Sepal.Width)
hist(iris$Petal.Length)
hist(iris$Petal.Width)
The above script gives you different histograms for different columns of iris dataset.