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 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 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 cylinder (cyl) values.
cyl.freq <- table(mtcars$cyl)
label <- names(cyl.freq)
label <- paste("Cylinder",label,"-")
percentage <- round(cyl.freq/sum(cyl.freq)*100)
lableWithPercent <- paste(label, percentage)
lableWithPercent <- paste(lableWithPercent, "%")
pie(cyl.freq, lableWithPercent, col = rainbow(length(label)) ,main = "Propotion of Cars by Cylinder Values")

  1. Create a bar graph, that shows the number of each carb type in mtcars.
cnt <- table(mtcars$carb)
labelBG <- names(cnt)
barplot(
  cnt,
  main = "Distribution of Cars by Carb",
  xlab = "Number of Carb",
  ylab = "Number of Cars",
  names.arg = c("Carb - 1","Carb - 2","Carb - 3","Carb - 4","Carb - 6","Carb - 8"),
  cex.names = 0.6,
  col = rainbow(length(labelBG))
)

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
cntStacked <- table(mtcars$cyl, mtcars$gear)
barplot(
  cntStacked,
  main = "Distribution of Cars by Gears and Cylinders",
  xlab = "Number of Gears",
  ylab = "Number of Cylinders",
  names.arg = c("3 Gears","4 Gears","6 Gears"),
  cex.names = 0.8,
  col = c("red","blue","green"), 
                  legend = rownames(cntStacked))

  1. Draw a scatter plot showing the relationship between wt and mpg.
plot(
  mtcars$wt,
  mtcars$mpg,
  main = "Comparision of Weight & Mpg",
  xlab = "Weight of Car",
  ylab = "Miles per gallon",
  pch=19
)

lines(lowess(mtcars$wt,mtcars$mpg), col="blue")

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
boxplot(mpg ~ gear,
        xlab = "Gear of Car",
        ylab = "Mileage of Car",
        names=c("3 Gear","4 Gear","5 Gear"),
        main = "Comparision of Gears & Mileage for Cars",
        data=mtcars)

Box plot is my favorite one, the major reason is it actually inclueds more information. One of the good thing about this chart is easy to understand and also shows outliers if any plus it also shows the summary of your data by displaying the 25 pecentile, median and 75 percentile. In this example, the average value shuffle of being 16 for 3 gear cars, 22 for 4 gear cars and 20 for 5 Gear cars.