Directions

During ANLY 512 we will be studying the theory and practice of data visualization. We will be using R and the packages within R to assemble data and construct many different types of visualizations. We begin by studying some of the theoretical aspects of visualization. To do that we must appreciate the basic steps in the process of making a visualization.

The objective of this assignment is to introduce you to R markdown and to complete and explain basic plots before moving on to more complicated ways to graph data.

The final product of your homework (this file) should include a short summary of each graphic.

To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Canvas. Please make sure that this link is hyperlinked and that I can see the visualization and the code required to create it.

Questions

Find the mtcars data in R. This is the dataset that you will use to create your graphics.

  1. Create a box plot using ggplot showing the range of values of 1/4 mile time (qsec) for each tansmission type (am, 0 = automatic, 1 = manual) from the mtcars data set.
# place the code to import graphics here
library(ggplot2)
data("mtcars")
mtcars$am=factor(mtcars$am,levels = c(0,1),labels = c("Automatic","Manual"))
ggplot(data = mtcars, aes(x= am, y= qsec)) + geom_boxplot(color="black", fill="yellow", alpha=0.3) + xlab("Transmission Type (am)") + ylab("1/4 Mile Time (qsec)") +
ggtitle("1/4 Mile Time vs Transmission Type")

Summary: From the above box plot, it can be interpreted that the 1/4 mile times for Automatic transmission types are greater than those for Manual transmission types.

  1. Create a bar graph using ggplot, that shows the number of each carb type in mtcars.
# place the code to import graphics here

ggplot(mtcars, aes(x=carb)) + geom_bar(stat="count", color = "black", fill = "pink", alpha = 0.3) + scale_x_discrete(name = 'Number of Carburetors', limits=1:8) + scale_y_discrete(name = 'Number of Carburetors', limits=c(2,4,6,8,10)) + ggtitle("Distribution of Cars by Number of Carburetors")

Summary: There are higher number of cars (10 cars each) with number of carburetors of either 2 or 4. There are no cars with either 5 or 7 number of carburetors.

  1. Next show a stacked bar graph using ggplot of the number of each gear type and how they are further divided out by cyl.
# place the code to import graphics here
ggplot(mtcars, aes(x = factor(gear),fill = factor(cyl))) + geom_bar() + xlab("Gear Type") + ylab("Number of Cars") + scale_fill_discrete("Cylinders") + ggtitle("Distribution of Cars by Gear Type and Cylinders") + scale_fill_manual(values = c("yellow","skyblue","pink"))
## Scale for 'fill' is already present. Adding another scale for 'fill', which
## will replace the existing scale.

Summary: 3 gear cars are highest in number while 5 gear cars are the least whereas the 8 cylinders and 3 gear types are the highest followed by 4 cylinders and 4 gear cars. 4 cylinders and 3 gear cars are least in number.

  1. Draw a scatter plot using ggplot showing the relationship between wt and mpg.
# place the code to import graphics here
ggplot(mtcars,aes(wt,mpg))+
  xlab("Weight (lb/1000)")+ylab("Fuel Efficiency (MPG)")+
  geom_point()+ggtitle("Weight vs Fuel Efficiency") +
  geom_smooth(method=lm) + geom_line()

Summary: From the above visualization, it can be interpreted that Weight is inversely proportional to MPG.

  1. Design a visualization of your choice using ggplot using the data and write a brief summary about why you chose that visualization.
# place the code to import graphics here
mpg_hp_am<-lm(mpg ~ hp*factor(am),mtcars)
ggplot(mtcars,aes(x=hp,y=mpg,color=factor(am)))+geom_point()+
geom_abline(intercept = coef(mpg_hp_am)[1], slope = coef(mpg_hp_am)[2],color="salmon")+
geom_abline(intercept = coef(mpg_hp_am)[1] + coef(mpg_hp_am)[3], 
slope = coef(mpg_hp_am)[2] + coef(mpg_hp_am)[4], color="cyan") + ggtitle("Horsepower vs Fuel Efficiency by Transmission Type")

Summary: I chose this visualization to see the relationship between MPG and Horsepower by transmission type. It can be intepreted from the above visualization that if horsepower increases, mpg decreases and vice versa. This is the same for both transmission types.