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.

# Loading
data(mtcars)
# Print the first 6 rows
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
  1. Create a pie chart showing the proportion of cars from the mtcars data set that have different cylinder (cyl) values.
mytable <- table(mtcars$cyl) # convert the column to a table
lbls <- names(mytable) # extract the labels
pct <- round(as.vector(mytable)/sum(as.vector(mytable))*100) # calculate percentage of the proportion
lbls <- paste(lbls, "\n", pct, sep="") # add percents to labels 
lbls <- paste(lbls,"%",sep="") # add % to labels 
pie(mytable, labels = lbls, 
   main="Car proportion by Cylinders")

  1. Create a bar graph, that shows the number of each carb type in mtcars.
counts <- table(mtcars$carb) # convert the column to a table
bp <- barplot(counts, main="Car Distribution by Carburetors", 
              xlab="Number of Carburetors",
              ylab="Number of Cars") # create bar chart
text(bp, 0, round(counts, 1),cex=1,pos=3) # add the data labels

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
counts <- table(mtcars$cyl, mtcars$gear) # convert two columns to a table
bp <- barplot(counts, main="Car Distribution by Gears and Cylinders",
  xlab="Number of Gears", 
  ylab="Number of Cars",
  col=c("darkblue","red","yellow"),
  legend = rownames(counts),
  args.legend = list(title = "Cylinder", x = "topright", cex = .7))

  1. Draw a scatter plot showing the relationship between wt and mpg.
attach(mtcars)
plot(wt, mpg, main="Relationship between Weight and Miles Per Gallon", 
   xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
abline(lm(mpg~wt), col="red") # add a regression line (y~x) 

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
attach(mtcars)
## The following objects are masked from mtcars (pos = 3):
## 
##     am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
boxplot(mpg~gear,data=mtcars, main="Miles Per Gallon by Gears", 
   xlab="Number of Gears", ylab="Miles Per Gallon")