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 carb values and write a brief summary.
# place the code to import graphics here
mtcars$carb=factor(mtcars$carb)
summary(mtcars$carb)
##  1  2  3  4  6  8 
##  7 10  3 10  1  1
mtcars.carb=c("carb1","carb2","carb3","carb4","carb6","carb8")
carb.count=c(7,10,3,10,1,1)
mtcars.pie=data.frame(mtcars.carb, carb.count)
mtcars.pie=within(mtcars.pie, {percarb=carb.count/sum(carb.count)})
carb.count/sum(carb.count)
## [1] 0.21875 0.31250 0.09375 0.31250 0.03125 0.03125
with(mtcars.pie, pie(percarb, labels=mtcars.carb, clockwise=TRUE, col=c("blue", "red","darkgreen","black","grey", "lightgreen", radius=1)))

Summary: We summarize that carb values 2 and 4 are most common, each presenting 30% in the dataset; while carb values 6 and 8 are the rarest with 3% each.

  1. Create a bar graph, that shows the number of each gear type in mtcarsand write a brief summary.
# place the code to import graphics here
counts1 <- table(mtcars$gear)
counts1
## 
##  3  4  5 
## 15 12  5
 barplot(counts1, main="Car Distribution", xlab="Number of Gears") 

Summary: The car distribution by gears declines as gear increases, with 3-gear being the most (15), and 5-gear being the least (5).

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyland write a brief summary.
# place the code to import graphics here
counts2 <- table(mtcars$cyl, mtcars$gear)
counts2
##    
##      3  4  5
##   4  1  8  2
##   6  2  4  1
##   8 12  0  2
barplot(counts2, main="Car Distribution by Gear and Cyl", xlab="Number of Gears", col=c("darkgreen","yellow","lightblue"), legend = rownames(counts2)) 

Summary: The distribution of cyl within each category of gear count differs a lot. 8-cyl dominates 3-gear, while 4-cyl represents majority of 4-gear, both 8-cyl and 4-cyl exceed 6-cyl for 5-gear.

  1. Draw a scatter plot showing the relationship between wt and mpgand write a brief summary.
# place the code to import graphics here
plot(mtcars$wt, mtcars$mpg)

Summary: We can see a negative linear regression relationship among wt and mpg, means that mpg declines as wt increases.

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
# place the code to import graphics here
library(ggcorrplot)
## Loading required package: ggplot2
mtcars$carb <- as.numeric(as.character(mtcars$carb))
mtcars$gear <- as.numeric(as.character(mtcars$gear))
corr <- round(cor(mtcars[,2:11]), 1)

ggcorrplot(corr, hc.order = TRUE, 
            type = "lower", 
            lab = TRUE, 
            lab_size = 3, 
            method="circle", 
            colors = c("tomato2", "white", "springgreen3"), 
            title="Correlogram of Car Distribution", 
            ggtheme=theme_bw)

Summary: I generate a Correlogram of all parameters within the dataset. It is a straightforward way of showing the correlation within all paragemeter by accents of color/bubble size. We can see some highly positively correlated pairs, such as gear & carb, cyl & disp, wt & disp; as well as some highly negatively correlated pairs, such as disp & vs, wt & drat.