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 Moodle. 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 pie chart showing the proportion of cars from the mtcars data set that have different cylinder (cyl) values.
data("mtcars")
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
cyl<-table(mtcars$cyl)
cyl_proportion <- names(cyl)
percentlabel <- round(cyl/sum(cyl)*100,2)
cyl_proportion <-paste(cyl_proportion, percentlabel)
cyl_proportion <- paste(cyl_proportion, "%", sep = "")
pie(cyl,labels = cyl_proportion,main="Pie Chart of Cylinder Type")

The pie chart above shows use area size to represent the proportion of cars with different cylinder 4, 6, and 8.

  1. Create a bar graph, that shows the number of each carb type in mtcars.
barplot(table(mtcars$carb),ylab="Number of Cars",xlab="Carburetors Type",main="Bar Graph of Carburetors Type")

There are six carb types. The bar chart above shows the number of cars for each carb type. The longer the bar, the more number of cars for that type.

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
cyl_2 <- table(mtcars$cyl, mtcars$gear)
barplot(cyl_2, 
        ylab="Number of Cars",
        xlab="Number of Forward Gears",
        main="Stacked Bar Graph of #Cars by Gear and Cylinder Type",
        cex.names=1,
        legend=rownames(cyl_2), args.legend=list(title="# of Cylinders"))

There are three type of Gears - 3, 4, and 5. Each Gear type may include different cylinder. The stack bar chart above visualizes the number of cars that broken down by gears and cylinder type. Each bar shows the number of cars for each gear, but also uses different color to show the breakdown of cylinder type.

  1. Draw a scatter plot showing the relationship between wt and mpg.
plot(mtcars$wt, mtcars$mpg, xlab="Weight", ylab="Miles per Gallon",main="Scatter Plot between Weight and Miles per Gallon")

The scatter plot simply shows the correlation trend between weight and miles per gallon of the car. As the weight of car increases, the miles per gallon decreases.

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
boxplot(wt~gear,data=mtcars, main="Weight of Car By Different Gear Type",xlab="Number of Forward Gear", ylab="Weight(1000 lbs)")

The box and whisker chart shows the value distribution broken down by certain criteria. The chart above shows how weight of car distributied by different type of gear. It shows that the 3 forward gears has the more larger weight than 4 and 5 gears. And the distribution of weight for 4 and 5 gears appeared similar.