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.
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
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
pie_data <- mtcars %>% select(carb) %>% group_by(carb) %>% count() %>% ungroup() %>% 
            mutate(per=`n`/sum(`n`)) %>% arrange(desc(carb))
pie_data$labels <- scales::percent(pie_data$per)

ggplot(pie_data, aes(x="", y = n, fill = as.character(carb))) + 
  geom_bar(width = 1, stat = "identity") + 
  coord_polar(theta = "y", start = 0) +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid  = element_blank()) + 
  labs(x = "", y = "", title = "Proportion of different carbs", fill = "# of carburetors") +
  geom_text(aes(label = labels), position = position_stack(vjust = 0.5), size = 2.7) + 
  theme(text = element_text(size = 12))

Comment: In the mtcars dataset, 31.2% of cars have 2 and 4 carbs, respectively. Only about 6% of cars have carbs of 6 or 8.

  1. Create a bar graph, that shows the number of each gear type in mtcarsand write a brief summary.
bar_data <- mtcars %>% select(gear) %>% group_by(gear) %>% count()
ggplot(bar_data, aes(x=as.character(gear), y = n, fill = as.character(gear))) + 
  geom_bar(width = 1, stat = "identity") + 
  labs(x = "Number of forward gears", y = "Count", title = "Number of each gear type", fill = "") 

Comment: 15 cars have 3 forward gears, 12 cars have 4 forward gears, and 5 cars have 5 forward gears.

  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.
ggplot(mtcars, aes(as.character(cyl))) + geom_bar(aes(fill = as.character(gear))) + 
  labs(x = "Number of cylinders", y = "Count", title = "Number of each gear type by cylinders", fill = "Number of forward gears") 

Comment: For cars that have 4 or 6 cylinders, more than half of them have 4 forward gears, but for cars that have 8 cylinders, most of them have 3 forward gears and none of them have 4 forward gears.

  1. Draw a scatter plot showing the relationship between wt and mpgand write a brief summary.
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + geom_smooth(method = "lm", se=FALSE) + 
  theme(panel.background=element_rect(fill = "white", colour = "black")) + 
  labs(x = "Weight (1000 lbs)", y = "Miles/(US) gallon", title = "Relationship between Weight (1000 lbs) and Miles/(US) gallon")

Comment: Weight and MPG have a negative relationship, that is, MPG decreases as weight increases.

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
ggplot(mtcars, aes(factor(cyl), mpg)) + geom_violin() + geom_jitter(height = 0, width = 0.1) + 
  labs(x = "Number of cylinders", y = "Miles/(US) gallon", title = "Violin plot: Miles/(US) gallon by Number of cylinders")

Comment: violin plot shows the kernel probability density of the Miles/(US) gallon per Number of cylinders. Cars that have 6 cylinders have the smallest MPG range; while cars with 4 cylinders have the largest MPG range. For cars with 8 cyliners, most data points are clustered around the mean.