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?
library(ggplot2)

# place the code to import graphics here
ggplot(mpg, aes(x=trans, y=displ)) + 
    geom_boxplot() + xlab("Transmission Types") + ylab("Engine Displacement") +ggtitle("Boxplot Graph for Engine Displacement by Transmisstion Type") + theme(axis.text.x=element_text(angle=30,hjust=1))

The graph shows the distribution of engine displacement if eacg transmission type. There are 10 transmission types in totally including 8 auto transmission types and 2 manual transmission types. From the output, we can learn that Manuel (m5) has outliers, and auto(I6) has the max mean of displacement comparing with any other transmission types. Also auto(I3) has the min mean of displacement comparing with any other transmission types.

  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
ggplot(mpg, aes(x= class)) + geom_bar() + geom_text(stat='count', aes(label=..count..), vjust=-0.25)+
    labs(title = "The frequency of each class type",
         x = "Class",
         y = "Frequency")

The graph shows the frequency of each class type. Within the 7 class types, 2 seater class has the lowest frequency, meanwhile, suv has the highest frequency.

  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(mpg, aes(x = class)) +
    geom_bar(aes(fill = as.factor(cyl)), position = "stack") +
    labs(title = "The frequency of each cyl type within class",
         x = "Class",
         y = "Frequency")

By looking at this stacked bar graph, we can learn that SUV has the most number of cylinders =8, and compact has the most number of cylinders=4.

2seater class is the only one only has cylinder=8.

  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(size=2, shape=4,color="blue") + xlab("City Miles per Gallon") +ylab("Highway Miles per Gallon or Efficiency")+ggtitle("The Positive Relationship Between City Miles per Gallon and Highway Miles per Gallon") + theme(plot.title = element_text(size=12))

The graph shows the two variables are positively related.Therefore, a high flue-efficient car perform well both on highway and in city. Thus, the flue-efficiency performance of vehicle is consistent under different road conditions.

  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
mpg_mean<-aggregate(cty  ~ model, data = mpg,  mean)
mpg_mean<- mpg_mean[with(mpg_mean,order(-cty)),]
mpg_rank <- mpg_mean[1:5,]

ggplot(mpg_rank, aes(x=reorder(model,-cty), y=cty)) +
  geom_bar(stat="identity", width=.4, fill="purple") + 
  labs(title="Fuel Efficiency by Brand (in city)", subtitle="Top 5 Brands")+ 
  theme(axis.text.x = element_text(angle=35, vjust=0.6))+xlab("Brand")+ylab("City Miles per Gallon")

I choose this graph because I am always interested to know which vehicle brand is the most flue efficient. By having this chart, I learned that corolla is the model which consumes the least fuel in city.