Q1 Create a pie chart showing the porpotion of cars from the mtcars dataset that have different carb values.

carb.freq <- table(mtcars$carb)
pct=round(carb.freq/sum(carb.freq)*100)
name <- c("1 carburetors","2 carburetors","3 carburetors","4 carburetors","6 carburetors","8 carburetors")
name <- paste(name,pct)
name <- paste(name,"%",sep=" ")
pie(carb.freq,labels=name,main="Pie Chart of Number of Carburetors")

Analysis: based on the pie chart above, we can find that the porpotion of 2 carburetors and 4 carburetors are the highest. 6 carburetors and 8 carburetors are rare in the dataset.

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

gear.count <- table(mtcars$gear)
barplot(gear.count,main="Gear Distribution",xlab="Number of Gears",ylab="Count",names.arg=c("3 Gears","4 Gears","5 Gears"),col=c("purple","purple","purple"))

Analysis: in the mtcars dataset, most cars have 3 gears and only 5 cars have 5 gears.

Q3 Stacked bar graph of the number of each gear type and how they are further divided out by cyl.

gear.cyl <-  table(mtcars$cyl,mtcars$gear)
barplot(gear.cyl,main="Car Distribution by Gears and Cylinders",xlab="Number of Gears", ylab="count", col=c("pink","purple","orange"),legend=rownames(gear.cyl),args.legend = list(title="Cylinders Number"))

Analysis: Accoring to the stacked bar graph above, we can easily find that most 3 gears’ cars have 8 cylinders and most 4 gears’ cars have 4 cylinders.

Q4 Draw a Scatter plot showing the realtionship between wt and mpg.

plot(mtcars$wt,mtcars$mpg,xlab="Weight of Car",ylab="Miles / gallon",main="Relationship between car weight and mpg")
abline(lm(mtcars$mpg~mtcars$wt),col="purple")

Analysis: based on the scatter plot above, we can find that there does exist a realtionship between the car weight and its mpg. Mpg goes down when weight of car increases.

Q5 Design a visualization of your choice using the data and write a brief summary about why you choose that visualization.

boxplot(mtcars$mpg ~ mtcars$gear,main = "Boxplot of MPG by gears",xlab="Number of gears",ylab="MPG")

Analysis: I choose boxplot as my last visualization for my HW1. Because I think boxplot is a very good method to do the descriptive statistics and it helps us to know more about our data. I generated the boxplot of MPG by different gears and the graph tells us that mean MPG of the 4 gears’ car group is much higher than that of the 3 genars’ car group.