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.
# place the code to import graphics here

# import mtcars dataset first
data=mtcars;
# count the portion of each crab
table=table(data$carb)
# simple pie chart
pie(table, main="Pie Chart of Crab")

  1. Create a bar graph, that shows the number of each gear type in mtcars.
# place the code to import graphics here

# count the portion of each gear
tablegear=table(data$gear)
# simple pie chart
barplot(tablegear, main="Car Gear", 
   xlab="Number of Gears")

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
# place the code to import graphics here

# countcyl and grouped by gear
counts=table(data$cyl, data$gear)
counts
##    
##      3  4  5
##   4  1  8  2
##   6  2  4  1
##   8 12  0  2
# create bar plot
barplot(counts, main="Vehicel Distribution by Gears and Cylinders",
  xlab="Gears", 
  names.arg=c("3 Gears", "4 Gears", "5 Gears"),
  ylab="Cars",
  col=c("green","blue","yellow"),
  legend = rownames(counts))

# conclusion
# 1) most of the 3 gears cars have 8 cylinders.
# 2) Within 4 gear cars, 4 cylinders is as twice as 6 cylinders. 
# 3) Within 5 gear cars, the propotion of 6 cylinder cars is the smallest. The rest two are almost equal.
  1. Draw a scatter plot showing the relationship between wt and mpg.
# place the code to import graphics here

plot(data$wt, data$mpg, main="Weight VS Mpg", 
    xlab="Weight", ylab="MPG")

# add regression line (y~x)
abline(lm(data$mpg~data$wt), col="red")

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
# place the code to import graphics here

boxplot(mpg~cyl, data=data, main="MPG Vs. Cylinder Number", 
        xlab="Cylinder Number", ylab="MPG", names=c("4 cyl", "6 cyl", "8 cyl"))

# Summary
# The target is to explorer whether larger number cylinder will desrease the fuel economy.
# Based on the box plot, in average, the 4-cyl cars have the best fuel economy, while the 8-cyl ones are not very eco friendly.