Find the mtcars data in R. This is the dataset that you will use to create your graphics.
library(plotrix)
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
cyl_unique=unique(mtcars$cyl)
cyl_table=table(mtcars$cyl)
cyl_4 <- length(which(mtcars$cyl == 4))
cyl_6 <- length(which(mtcars$cyl == 6))
cyl_8 <- length(which(mtcars$cyl == 8))
cyl_label=c(cyl_4,cyl_6,cyl_8)
cyl_prop=sprintf("%1.1f%%",((cyl_table/sum(cyl_table))*100))
cyl_label=paste(cyl_label,cyl_prop,sep ="\n")
pie3D(cyl_table,labels=cyl_label,explode = 0.1,main="Proportions of Cylinder Values for different cars")
The above Pie chart explains how the 3 different types of cylinder types of the “mtcars” dataset are present alongside it’s Proportions.
carb_table=table(mtcars$carb)
carb_names=names(carb_table)
barplot(carb_table,main="Number of each Carb types",xlab = "Number of Carbs",ylab = "Frequency of cars",names.arg = carb_names,col=rainbow(length(carb_names)) )
In the above Bar graph we can visualize the number of carbs types and the count of number of cars.
cylgear_table=table(mtcars$cyl,mtcars$gear)
cyl.gear = table(mtcars$cyl, mtcars$gear)
barplot(cylgear_table, main="Number of each gear type alongside Cylinders", xlab="Number of Gears",ylab="Number of Cars",legend = rownames(cylgear_table))
In the above stacked Bar graph we can clearly visualize how many number of cars are distributed for each number of Gears alongside each number of cylinders.
library("ggplot2")
ggplot(data = mtcars, mapping = aes(x = wt,y=mpg)) +
geom_point()+
ggtitle("Relationship between wt and mpg") +
xlab("wt")+
ylab("mpg")+
geom_smooth(method = 'lm', color = 'black')+
theme(plot.title = element_text(hjust = 0.5))
In the above Scatterplot we can clearly visualize and interpret that the datapoints of wt and mpg are having negative linear trend type of relationship.
library(corrplot)
plot(mtcars)
corrplot.mixed(cor(mtcars),lower.col = "black", number.cex = .7)
I choose the above two visualization techniques so as to explore the datapoints and it’s characteristics of the “mtcars” datasets alongside the correlation plot which explains the interaction between variable to variable.