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
data(mtcars) #Laod the mtcars dataset first
table(mtcars$carb)
## 
##  1  2  3  4  6  8 
##  7 10  3 10  1  1
labels = c("1 carburetors","2 carburetors","3 carburetors","4 carburetors","6 carburetors","8 carburetors")
paste(prop.table(table(mtcars$carb))*100, "%", sep = "")
## [1] "21.875%" "31.25%"  "9.375%"  "31.25%"  "3.125%"  "3.125%"
pie(table(mtcars$carb),labels = paste(labels," ",prop.table(table(mtcars$carb))*100, "%", sep = ""), main = "Proportion of Cars That Have Different Carburetors")

#The cars with 2 and 4 carburetors have the largest proportion,31.25%, among all cars, while the 6 and 8  carburetors have the smallest proportion,3.125%. 
  1. Create a bar graph, that shows the number of each gear type in mtcars.
table(mtcars$gear)
## 
##  3  4  5 
## 15 12  5
barplot(table(mtcars$gear), xlab = "Gear Type", ylab = "Frequency" , main = "Frenqucy of Each Gear Type",col = c("#E69F00", "#56B4E9", "#009E73"))

#Cars with 3 forward gears has the highest frequency(above 12), while cars with 5 forward gears has the lowest frequency(5). 
  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
table(mtcars$cyl, mtcars$gear)
##    
##      3  4  5
##   4  1  8  2
##   6  2  4  1
##   8 12  0  2
barplot(table(mtcars$cyl, mtcars$gear), xlab = "Gear Type", ylab = "Cylinder", main = "Distribution by Gear and Cylinder", col = c("pink", "light blue", "green"),legend = rownames(table(mtcars$cyl, mtcars$gear)))

#3 types of gears, 3,4 and 5. Cars with 3 and 5 gears could be devided by 3 types of cylinders(4,6,and 8), while those with 4 gears could be devided into 2 type of cylinders(4 and 6). 
  1. Draw a scatter plot showing the relationship between wt and mpg.
plot(x = mtcars$wt, y = mtcars$mpg, main = "Relation between Weight and MPG",, xlab = "Weight", ylab = "MPG")
lines(lowess(mtcars$wt,mtcars$mpg), col = "red")

#Based on the graph, we could see the as the weight increases, the mpg tend to decrease, which means these two variables have a negative correlation. 
  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
plot(x = mtcars$hp, y = mtcars$qsec, main = "Relation between Horsepower and 1/4 Mile Time", xlab = "Horsepower", ylab = "1/4 Mile Time")
lines(lowess(x = mtcars$hp, y = mtcars$qse),col = "blue")

#I wanted to see the relationship bewteen horsepower and 1/4 mile time,so I choose the scatter plot and a line to show it. Turned out that the higher the horsepower the lower the 1/4 mile time, and they have a negative correlation.