Introduction

 

MTCars (Motor Trend Car Road Tests)

The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).  
 

Load the dataset
data(mtcars)
View the dataset
mtcars
Structure of the datset
str(mtcars)
Check for missing data
colSums(is.na(mtcars))
##  mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
##    0    0    0    0    0    0    0    0    0    0    0
Descriptive Statistics of the data
summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000

     

Question 1

Draw a pie chart showing the proportion of cars from the mtcars data set that have different carb values.

Answer

Analog

knitr::include_graphics("C:/Harrisburg/512/Assignment 1/Ques 1.jpg", dpi = 108)

 

Digital

##### Frequency table for Carb
carb_freq = table(mtcars$carb)

##### Create carb label values
carb_lbs_val<- round(100*carb_freq/sum(carb_freq), 2)

##### Create labels for each carb pie in the chart
carb_lbs<- paste(carb_lbs_val, "%", sep="")

##### Piechart for proportion of Carburetor in MTCars dataset
pie(carb_freq,col = rainbow(length(carb_freq)), labels = carb_lbs , main = 'Pie Chart for MTCars distribution of Carburetors', cex = 0.9, radius = 1.0)

#####Legend for the pie chart
legend("topright", c("Carb 1","Carb 2","Carb 3","Carb 4","Carb 6","Carb 8"), cex=0.7, fill =  rainbow(length(carb_freq)))

From the above piechart, it can be incurred that significant number of cars in MTCars dataset had number of Carburetors 2 or 4 with a proportion of 31.25% and cars with number of carburetors as 6 and 8 were made rarely with a proportion of 3.12%

     

Question 2

Draw a bar graph, that shows the number of each gear type in mtcars.

Answer

Analog

knitr::include_graphics("C:/Harrisburg/512/Assignment 1/Ques 2.jpg", dpi = 108)

 

Digital

#Frequency table for gear
gear_freq<- table(mtcars$gear)

#Barplot for number of gears
barplot(
  gear_freq, main="Bar plot for Gear Type", col= "light blue",
  xlab ="Number of gears", ylab = "Frequency",
  names.arg=c("fwd gear=3","fwd gear=4", "fwd gear=5"),
  cex.lab = 0.9, cex.main = 0.9, cex.axis =0.74, cex.names = 0.74, las =0.6
)

From the above bar graph, it can be seen that the number of gears for significant number of the cars in MTCars dataset were 3. Cars with number of gears as 4 and 5 is almost at 75% proportion and 33% proportion of the cars with number of gears as 3 respectively.

     

Question 3

Next show a stacked bar graph of the number of each gear type and how they are further divded out by cyl.

Answer

Analog

knitr::include_graphics("C:/Harrisburg/512/Assignment 1/Ques 3.jpg", dpi = 108)

 

Digital

#Frequency table for gear and cylinder type
gear_cyl_freq <- table(mtcars$cyl, mtcars$gear)

#Bar graph for gear and cylinder type
barplot(gear_cyl_freq, main = "Stacked BarPlot for MTCars distribution by Gears Vs Cyl", 
        xlab = "Number of Gears",ylab= "Frequency", col = rainbow(length(gear_cyl_freq)),
        names.arg=c("fwd gear=3","fwd gear=4", "fwd gear=5"),
        cex.lab = 0.9, cex.main = 0.9, cex.axis =0.74, cex.names = 0.74, las =0.6)
legend("topright", c("8 cylinders","6 cylinders","4 cylinders"), cex=0.7, fill =  rainbow(length(gear_cyl_freq)))

From the above stacked bar graph following infrences can be made:

  1. Significant number of cars having number of gears as 3 had 8 cylinders.

  2. The distribution of the number of 4 gears cars having 4 cylinders is twice as the number of 4 gears cars with 6 cylinders.

  3. For cars having number of gears as 5, the distribution of number of cylinders is equal for cars having 8 and 4 cylinders and minimal for the cars having 6 cylinders.

     

Question 4

Draw a scatter plot showing the relationship between wt and mpg.

Answer

Analog

knitr::include_graphics("C:/Harrisburg/512/Assignment 1/Ques 4.jpg", dpi = 108)

 

Digital

#Scatterplot to display the relationship between weight and mpg
x<-mtcars$wt
y<-mtcars$mpg
plot(x, y, main="Weight VS Miles per gallon", 
     xlab="Weight ", ylab="Miles Per Gallon ", pch=18, col = "dark green")

#Regression line (y~x) to see the correlation between mpg and weight
abline(lm(y~x), col="red", lwd=2)

#Lowess line (x,y) to see the correlation between mpg and weight
lines(lowess(x,y), col="blue", lwd=2)

Based on the above scattered plot, weight is negatively correlated to miles per gallon of the cars in MTCars dataset. Thus, as the weight of car increases, mileage per gallon decreases.

     

Question 5

Design a visualization of your choice using the data.

Answer

According to me the most important cost saving feature of the car is being fuel potent while being affordable. Therefore, I would like to analyze the impact of the transmission type on the miles per gallon of the cars in MTCar dataset i.e., Is an automatic or manual transmission better for MPG and thereby quantifying the MPG difference between automatic and manual transmissions.

Analog

knitr::include_graphics("C:/Harrisburg/512/Assignment 1/Ques 5.jpg", dpi = 108)

 

Digital

#install.packages("ggplot2")
library(ggplot2)

#Setting the plot title to center
theme_update(plot.title = element_text(hjust = 0.5))

#ggplot to display the relationship between miles per gallon and transmission type
ggplot(mtcars, aes(y=mpg, x=factor(am), 
                   fill=factor(am))) +scale_fill_brewer(palette="Set2")+
  xlab("Transmission Type") + ylab("Miles per Gallon (MPG)") + 
  geom_boxplot(color="black", size=0.5) + labs(title = "Miles per gallon VS Transmission type") +
  scale_fill_discrete(name="AM (Trans type)",
                       breaks=c(0,1),
                       labels=c("Automatic", "Manual")) 

# Descriptive statistics to display the impact of transmission type on miles per gallon
data(mtcars)
trans_type <- transform(
  mtcars, am = factor(am,
                      labels = c("Automatic", "Manual")),
  cars = rownames(mtcars)
)

auto_type = subset(trans_type, am == "Automatic")
manual_type = subset(trans_type, am == "Manual")
mean_am <- c(mean(auto_type$mpg),mean(manual_type$mpg))
am_freq <- table(trans_type$am)
am <- cbind(am_freq,round(prop.table(am_freq)*100,2),round(mean_am,2))
colnames(am) <- c('Number of Cars','Proportion', 'Mean MPG')
am
##           Number of Cars Proportion Mean MPG
## Automatic             19      59.38    17.15
## Manual                13      40.62    24.39
#Regression to show the correlation between miles per gallon and transmission type
ggplot(mtcars, aes(x = am, y = mpg)) +
  geom_point() + 
  geom_smooth(method = "lm") + labs(title = "ggplot for Miles per gallon VS Transmission type") +
  xlab("Transmission Type") + ylab("Miles per Gallon (MPG)")

From the above ggplot, it can be seen that 59.38 % of the total number of cars in MTCars dataset have automatic where as 40.62% have manual transmission.  
Besides, the average mileage for vehicles with automatic transmissions is 17.15 mpg, and 24.39 mpg for vehicles with manual transmissions and hence manual transmissions provide 7.25 higher mpg as compared to manual transmissions keeping other variables constant.  
Thus, cars with manual transmission have a higher MPG than cars with a automatic transmission and thua a higher fuel potency.