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.
f1<-factor(mtcars$carb)
f1
##  [1] 4 4 1 1 2 1 4 2 2 4 4 3 3 3 4 4 4 1 2 1 1 2 2 4 2 1 2 2 4 6 8 2
## Levels: 1 2 3 4 6 8
summary(f1)
##  1  2  3  4  6  8 
##  7 10  3 10  1  1
slice.labels<-c(1,2,3,4,6,8)
slice.labels
## [1] 1 2 3 4 6 8
pie(table(f1),labels = slice.labels)

## from the pie chart, carb values as "2" and "4" takes the highest proportion compared to others
  1. Create a bar graph, that shows the number of each gear type in mtcars.
x<-table(mtcars$gear)
x
## 
##  3  4  5 
## 15 12  5
barplot(x,col = "pink", ylim = c(0,20),xlab = "Gear type", ylab = "# of different gear types")

## from the bar graph, the heights of bars are displayed from highest to lowest with the reading order from left to right -- which indicates that "3" gear type takes the highest usage volumes as 15
  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
y <- table(mtcars$cyl, mtcars$gear)
barplot(y,xlab="Gear Type", col=c("cyan","red","orange"), legend = rownames(y))

## from the stacked bar graph, different numbers of gears have different distributions of # of cylinders. For "3"-gear group, "8"cylinders takes the largest share;for "4"-gear group, "4"cyliners takes the largest share; for "5"-gear group, # of 8 and 4 cyliners are the same. 
  1. Draw a scatter plot showing the relationship between wt and mpg.
attach(mtcars)
plot(wt, mpg, xlab="Weight ", ylab="MPG ")

## the simple scatterplot discribes the relationship between "Car Weight" and "Mile per Gallon" as a negative relationship -- in other words, with the increasing of "car weight", "mile per gallon" decrease
  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
plot(density(mtcars$wt))

## the line chart represents the weight of cars -- there is a spike around 3.5, which indicates the highest frequency of car weights as 3.5