Dataset: MTCars

  1. Create a pie chart showing the proportion of cars from the mtcars dataset that have different carb values.
prop <- table(mtcars$carb)
lab <- paste(round(prop/nrow(mtcars)*100), "%")
pie(prop, labels=lab, main = "Proportion of Carburetors", col=rainbow(6))
legend("topleft", legend=names(prop), fill=rainbow(6), title="Carburetors")

The piechart above displays the proportion of the cars in mtcars dataset grouped by number of carburetors carb. From the piechart, we can see that the proportion of cars with 2 and 4 carburetors are 31% each, whereas 22% of cars have only 1 carburetor. 9% have 3 carburators, whereas the lowest number of cars have 6 or 8 carburetors with 3% each.

  1. Create a bar graph, that shows the number of each gear type in mtcars.
ger <- table(mtcars$gear)
barplot(ger
        , main="Number of cars for different gears"
        , xlab="Number of Gears"
        , ylab="Number of Cars"
        , ylim=c(0,18))

The barplot above displays the number of cars in the mtcars dataset based on the number of gears. We can see that the cars with 3 gears have most numbers (15), followed by 4 gears (12) and 5 gears (5) respectively.

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
gearcyl <- table(mtcars$cyl, mtcars$gear)
barplot(as.matrix(gearcyl)
        , main="Number of cars for different gears grouped by number of cylinders"
        , xlab="Number of Gears"
        , ylab="Number of Cars"
        , ylim=c(0,18)
        , legend.text=TRUE
        , args.legend=list(x="topright", title="Cylinders"))

The stacked barplot above displays the number of cars based on number of gears, and further grouped by number of cylinders per gear group. Different stacks on a bar of each gear group represent the number of cars for different number of cylinders. For e.g., in the group with 3 gears, there is 1 car with 4 cylinders, 2 cars with 6 cylinders, and 12 cars with 8 cylinders.

  1. Draw a scatter plot showing the relationship between wt and mpg.
plot(mtcars$wt ~ mtcars$mpg
     , xlab="Miles per Gallon"
     , ylab="Weight (1000 lbs)"
     , main="Scatter plot of mpg vs wt for mtcars"
     , pch=20)

The scatterplot above displays the relationship between the weight wt and miles per gallon mpg for the cars. From the plot above, we can see that the mileage (miles per gallon) of cars increases for decreasing weight. This is understandable as heavier cars need more power to operate, hence have less fuel efficiency.

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
mpg1 <- subset(mtcars, cyl==4)
mpg2 <- subset(mtcars, cyl==6)
mpg3 <- subset(mtcars, cyl==8)
par(mfrow=c(3,1))
xlim = c(0,40)
hist(mpg1$mpg, main = "Histogram of mpg for cyl = 4", xlab="Miles per Gallon", xlim=xlim)
hist(mpg2$mpg, main = "Histogram of mpg for cyl = 6", xlab="Miles per Gallon", xlim=xlim)
hist(mpg3$mpg, main = "Histogram of mpg for cyl = 8", xlab="Miles per Gallon", xlim=xlim)

We try to analyze the distribution of miles per gallon mpg grouped by number of cylinders. This will help us identify the cars that has more mileage based on how many cylinder they have, and study the effects of number of cylinders to the fuel efficiency of cars. For that reason, we need to plot histograms of the mpg variable for different cyl. The histograms are placed on top of each other to compare the results. From the plot, we can see that for cyl = 4, there are more number of cars in the high mileage range of mpg > 20. For cyl = 6, there are cars in the mid-mileage range in mpg between 15 and 25. For cyl = 8, there are cars in the low mileage range of mpg < 20. From the visualization above, we can conclude that cars with more number of cylinders have less fuel efficiency.