This document sets out graphical representations of some aspects of the MT Cars dataset, and includes both manually drawn charts as well as corresponding graphs created using the R codes.
(Created manually)
knitr::include_graphics("C:/Users/Mansi/Documents/Harrisburg classes/Sem 3/512/3-1.jpg")
(Created in R)
ggplot(data=mtcars, aes(x = factor(32), fill = factor(carb))) +
labs(title="Proportions of Cars by Carb Values") + xlab(" ") + ylab(" ") +
geom_bar(width = 1) +
coord_polar(theta = "y") + theme_bw()
Explanation:
More than 60% of the samples in mtcars database have 2 and 4 carburators (split equally at 31%). 21% have 1 carburator, and 9% have 3 carburators. The remaining 6% is split equally between cars with 6 and 8 carburators.
Process: Graph created by identifying proportion of cars within each category. Code used: prop.table(table(mtcars$carb))
(Created manually)
knitr::include_graphics("C:/Users/Mansi/Documents/Harrisburg classes/Sem 3/512/2-1.jpg")
(Created in R)
ggplot(mtcars, aes(x=gear)) + geom_bar(col = "pink", fill = 'light blue') +
labs(title = "Number of Cars by Gear Type") + xlab ("Gear Type") + ylab("Number of Cars") +
theme_bw()
Explanation:
Almost half the cars (47%) in the mtcars database have 3 gears (15 of total 32 cars). About 38% of the cars have 4 gears and 15% have 5 gears.
Process: Graph created by identifying number of cars in each category. Code used: table(mtcars$gear)
(Created manually)
knitr::include_graphics("C:/Users/Mansi/Documents/Harrisburg classes/Sem 3/512/2-2.jpg")
(Created in R)
ggplot(mtcars, aes(x=factor(gear), fill = factor(cyl))) +
geom_bar(col = "black") +
labs(title = "Number of Cars by Gear Type") + xlab ("Gear Type") + ylab("Number of Cars") +
theme_bw()
Explanation:
Almost half the cars (47%) in the mtcars database have 3 gears (15 of total 32 cars). About 38% of the cars have 4 gears and 15% have 5 gears.
Within cars that have 3 gears, 80% have 8 cylinders, 13% have 6 gears and 7% have 4 gears. 2/3rd of the cars with 4 gears have 4 cylinders, and 1/3rd have 6 gears.
Of the cars with 5 gears, 2 cars have 8 cylinders (40%), 2 have 4 cylinders (40%) and 1 has 6 cylinders (20%).
Process: Graph created by identifying number of cars in each category. Code used: table(mtcars\(gear, mtcars\)cyl)
(Created manually)
knitr::include_graphics("C:/Users/Mansi/Documents/Harrisburg classes/Sem 3/512/1-1.jpg")
(Created in R)
equation = function(x) {
lm_coef <- list(a = round(coef(x)[1], digits = 2),
b = round(coef(x)[2], digits = 2),
r2 = round(summary(x)$r.squared, digits = 2));
lm_eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(R)^2~"="~r2,lm_coef)
as.character(as.expression(lm_eq));
}
fit <- lm(mtcars$wt~ mtcars$mpg)
ggplot(mtcars, aes(x=wt, y=mpg)) +
xlab ("Weight of the car") + ylab ("Miles per Gallon") +
geom_point() +
geom_line() +
ggtitle("Relationship between Weight of Car and Mileage") +
geom_smooth(method = 'lm', formula = y ~ x) +
annotate("text", x = 4, y = 30, label = equation(fit), parse = TRUE) +
theme_bw()
Explanation:
As the graphs above show, mileage is inversely reated to the weight of the car: heavier the car, lower is the car’s mileage, and vice versa. The system generated graph shows that line of best fit along with the scatter plot, which is downward sloping, and thus supports our analysis. 75% of the varation in miles per gallon can be explained by the weight of the car. Thus, there is a strong relationship between weight and car mileage.
Process: Graph created by plotting number of cars with mileage in the y axis and weight in the x axis. Code used: table(mtcars\(wt, mtcars\)mpg)
(Created manually)
knitr::include_graphics("C:/Users/Mansi/Documents/Harrisburg classes/Sem 3/512/4-1.jpg")
(Created in R)
cyltt <- table(mtcars$cyl, mtcars$am)
barplot(cyltt, main = "Car Distribution by Number of Cylinders and Transmission type",
xlab = "Transmission type", ylab = "Number of Cars", legend = rownames(cyltt), beside = TRUE)
Explanation:
Of the total 32 cars, 19 have manual transmission (~60%) and 13 have automatic transmission (remaining 30%).
Within the manual transmission category, majority of the cars (63%) have 8 cylinders. The remaining ~20% have 6 cylinders and 16% have 4 cylinders. Conversely, 62% of the cars with automatic transmission have 4 cylinders, 23% have 6 cylinders and 15% have 8 cylinders.
Thus, we can conclude that cars with automatic transmission are likely to have more cylinders, and the ones with manual transmission are likely to have a lower number of cylinders.
Process: Graph created by identifying number of cars in each category. Code used: table(mtcars\(cyl, mtcars\)am)