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 and write a brief summary.
carb.freq <- table(mtcars$carb)
nm <- names(carb.freq)
pct <- round(carb.freq/sum(carb.freq)*100)
nm <- paste(nm, pct)
## add percents to labels 
nm <- paste(nm,"%",sep="") 
## ad % to labels 
pie(carb.freq,labels = nm,col=rainbow(length(nm)), main="Proportion of Cars with Different Carburetor Values")

## We see that the cars with 2 or 4 carburetors has 31% of total, but the propotion of cars with 6 or 8 carburetors only has 3%.
  1. Create a bar graph, that shows the number of each gear type in mtcarsand write a brief summary.
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution (by Gears)", xlab="Number of Gears",ylab="Number of Cars",names.arg=c("3 Gears", "4 Gears", "5   Gears"),cex.names=0.5,col=c("Yellow","Orange","Red"))

## We can see from the bar chart that there are more than 14 three gears cars and the number of four gears cars is 12 and the number of five gears cars is less than 6.
  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyland write a brief summary.
counts <- table(mtcars$cyl, mtcars$gear)
barplot(counts, main="Car Distribution (by Gears and Cylinders)",
  xlab="Number of Gears", 
  names.arg=c("3 Gears", "4 Gears", "5   Gears"),
  cex.names=0.5,
  ylab="Number of Cars",
  col=c("Yellow","Orange","Red"),
    legend = rownames(counts))

##The stacked bar chart above shows: 1) the majority of 3 gears cars have 8 cylinders. 2) the number of 4 gears cars with 4 cylinders is as twice as the number of 4 gears cars with 6 cylinders. 3) Amoung 5 gear cars, the propotion of 6 cylinder cars is the smallest.
  1. Draw a scatter plot showing the relationship between wt and mpgand write a brief summary.
plot(mtcars$wt, mtcars$mpg, main="Relashionship between Weight and MPG", 
    xlab="Car Weight ", ylab="MPG", pch=18)
abline(lm(mtcars$mpg~mtcars$wt), col="red") # regression line (y~x) 
lines(lowess(mtcars$wt,mtcars$mpg), col="blue") # lowess line (x,y)

# The graph with scatter plot of car weight and MPG, and the regression line and the lowess (locally weighted scatterplot smoothing) line shows the relationship between those two variables. The linear regression line in red tells us the car weight and mpg have negtive correlation. The lowess line in blue indicates that they have negative relationship. And it shows a better fit.
  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
boxplot(mpg~cyl,data=mtcars, main="MPG VS Number of Cylinders", 
    xlab="Number of Cylinders", ylab="Miles Per Gallon",names=c("4 cyl", "6 cyl", "8 cyl"))

# The middle portion of each box is the interquartile range, the bold line inside of it indicates the median, the lower rim of the rectangle represents the first quartile and the upper rim of the rectangle stands for the third quartile. The lower line attached to the rectangle indicates the minimum and the upper line inidcates the maximum. In the 4 cylinder group, the average mpg value is about 26, the minimum mpg is about 22, and the maximum mpg is about 34. Compare with 4 cylinder group, the cars in 6 and 8 cylinder group have much lower mpg value. The average mpg of cars in 6 cylinder group is about 20 and this value in 8 cylinder group is 15.