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.

Each question is worth 5 points.

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 mpg data in R. This is the dataset that you will use for the first three questions.

  1. Create a box plot using ggplot showing engine displacement displ for each transmission type trans from the mpg data set. Hint: Can you figure out how to rotate the x-axis categories so they are all readable?
# place the code to import graphics here
library(ggplot2)
data(mpg)
boxplot(displ~trans,data=mpg, ylab="Engine Displacement",xlab="", las=2)
mtext("Transmission Type", side=3)

  1. Create a histogram or bar graph using ggplot, that shows the frequency of each class type in mpg.
# place the code to import graphics here
library(ggplot2)
ggplot(data.frame(mpg), aes(x=class))+geom_bar()

  1. Next show a stacked bar graph using ggplot, that shows the frequency of each cyl type within class. Hint:You might have to use (group) or convert cyl to a factor (as.factor).
# place the code to import graphics here

ggplot(data.frame(mpg), aes(x=class,fill = cyl,las=2)) + geom_bar()+theme(axis.text.x=element_text(angle=90,hjust=1))

  1. Draw a scatter plot using ggplot showing the relationship between cty and hwy. Explain the utility or lack of utility of this graphic.
# place the code to import graphics here
ggplot(mpg, aes(x = cty, y = hwy))+geom_point()

#This plot basically shows that highway miles per gallon is mostly linearly proportional to city miles per gallon. It is not a useful graph since miles per gallon should be same irrespective of highway or city.
  1. Design a visualization of your choice using ggplot using mpg and write a brief summary about why you chose that visualization.
# place the code to import graphics here
ggplot(mpg, aes(x=displ, y=hwy+cty, color=drv, shape=drv)) +geom_point()+geom_smooth(method=lm)

#This plot tries to identify the relation between engine displacement and miles per gallon for different types of drive train.Miles per gallon decreases as engine displacement increases as a general trend for all drive types though there are outliers in the trend lines.