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 pie chart using ggplot showing the proportion of cars from the mtcars data set that have different cylinder (cyl) values.
# place the code to import graphics here
library(ggplot2)
ggplot(data = mtcars, aes(x = cyl)) +
  geom_bar(aes(y = (..count..)/sum(..count..)), binwidth = 1,
           color = 'indianred4', fill = 'indianred3') +
  xlab("cylinder (cyl)") +
  ylab("Proportion of Cars")
## Warning: `geom_bar()` no longer has a `binwidth` parameter. Please use
## `geom_histogram()` instead.

According to the plot, from the mtcars data set, there are about 35% cars having cylinder (cyl) = 4, 21% cars with cylinder (cyl) = 6 and 44% cars with cylinder (cyl) = 8.

  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(data = mtcars, aes(x = carb)) +
  geom_histogram(color = 'indianred4', binwidth = 0.5, fill = 'indianred3') +
  xlab("carb") +
  ylab("Frequency")

According to the plot, from the mtcars data set, there are 7 cars having carb = 1, 10 cars having carb = 2, 3 cars having carb = 3, 10 cars having carb = 4, 1 car having carb = 6 and 1 car having carb = 8

  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(data = mtcars, aes(x = gear, fill = factor(cyl))) +
  geom_histogram(binwidth = 0.5) +
  xlab("gear") +
  ylab("Frequency")

According to the plot, from the mtcars data set, there are 15 cars having gear = 3, 12 cars having gear = 4 and 5 cars having gear = 5. Among the cars having gear = 3, there are 12 cars having cyl = 8, 2 cars having cyl = 6 and 1 car having cyl = 4. Among cars having gear = 4, there are 4 cars having cyl = 6 and 8 cars having having cyl = 4. Among cars having gear = 5, there are 2 cars having cyl = 8, 1 car having cyl = 6 and 2 cars having cyl = 4.

  1. Draw a scatter plot using ggplot showing the relationship between wt and mpg.
# place the code to import graphics here
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point()+
  xlab("wt") +
  ylab("mpg")

From the scatter plot, it seems like there’s a negative relationship between wt and mpg, the higher wt a car has, the lower 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
ggplot(mtcars, aes(x = wt)) + 
  geom_histogram(color = 'darkseagreen4', fill = 'darkseagreen')
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

This is a histogram of the wt of the cars in mtcars dataset. Histogram was chosen to visualize the distribution of wt variable because wt is a numeric variable. According to the plot, most cars having wt between 2 and 4, there are 4 cars having wt less than 2 and 4 cars having wt greater than 4, and 3 cars having wt greater than 5.5.