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.

library(ggplot2)

cleanup = theme(panel.grid.major = element_blank(),
                panel.grid.minor = element_blank(),
                panel.background = element_blank(),
                axis.line.x = element_line(color = 'black'),
                axis.line.y = element_line(color = 'black'),
                legend.key = element_rect(fill = 'white'),
                text = element_text(size = 15))
  1. Create a pie chart using ggplot showing the proportion of cars from the mtcars data set that have different cylinder (cyl) values.
# From the Pie chat we understand that the proportion of cars with 8 cylinders is the highest.
ggplot(mtcars, aes(x = '', y = 'Cylinder', fill = mtcars$cyl)) +
  geom_bar(width = 1, stat = 'Identity', position = 'stack') +
  coord_polar('y', start = 0) +
  labs(title = 'Cylinder values Distributions') +
  xlab('') +
  ylab('Cylinder') +
  theme_bw() + 
  cleanup

  1. Create a bar graph using ggplot, that shows the number of each carb type in mtcars.
# From the bar plot we understand that cars with 2 and 4 carbs are greater.
ggplot(mtcars, aes(x = carb)) + 
  geom_bar(color = 'blue', fill = 'black') +
  xlab('Carb') + 
  ylab('Count') + 
  theme_bw() +
  cleanup

  1. Next show a stacked bar graphusing ggplot of the number of each gear type and how they are further divided out by cyl.
# From the bar chart we see that 4 gear cars with 4 cylinders are the highest in number as compared to the other combinations.
ggplot(mtcars, aes(x = gear, y = '', fill = mtcars$cyl)) +
  geom_bar(stat = 'Identity') +
  xlab('Gear') +
  ylab('Count') +
  cleanup

  1. Draw a scatter plot using ggplot showing the relationship between wt and mpg.
# From the scatterplot we learn that miles per gallon is inversely proportional to weight of the car. As the weight increases, the miles per gallon decreases.
ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point() +
  geom_smooth(method = 'lm', color = 'blue') +
  labs(title = 'Relationship between Weight of Car and Miles Per Gallon') +
  xlab('Weight of the car') + 
  ylab('Miles per gallon') +
  cleanup

  1. Design a visualization of your choice using ggplot using the data and write a brief summary about why you chose that visualization.
# Below visualization depicts the relationship of horsepower with its mileage. It shows how the horsepower of a car impacts the miles per gallon.
ggplot(mtcars, aes(x = hp, y = mpg)) + 
  geom_point(size = 2) + 
  geom_smooth(method = lm, color = 'red4') + 
  labs(title = 'Relationship between Horsepower and Miles per gallon') + 
  xlab('Horsepower') + 
  ylab('Miles per Gallon') +
  theme_bw() +
  cleanup