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 using ggplot showing the proportion of cars from the mtcars data set that have different cylinder (cyl) values.

This pie chart shows the proportion of cars that have different cylinder values.

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
library(ggplot2)
bar.chart <- ggplot(mtcars, aes(x='',y='cylinder',fill = mtcars$cyl)) + geom_bar( stat='Identity')
pie.chart <- bar.chart + coord_polar('y',start=0)
pie.chart

  1. Create a bar graph using ggplot, that shows the number of each carb type in mtcars.

This bar graph shows the number of each carb type among all the cars.

bar.graph<-ggplot(mtcars, aes(x=carb,y=''),fill = type)+geom_bar(stat = "identity")
bar.graph

  1. Next show a stacked bar graph using ggplot of the number of each gear type and how they are further divided out by cyl.

This graph shows the numbers of each gear type among all the cars and how they are further break down by cylinder numbers.

stacked.chart<-ggplot(mtcars,aes(x=mtcars$gear,y='',fill=mtcars$cyl)) + geom_bar(stat='identity')
stacked.chart

  1. Draw a scatter plot using ggplot showing the relationship between wt and mpg.

This scatter plot shows that wt and mpg has an approximate linear relationship

scatter.plot<-ggplot(mtcars, aes(x=wt, y=mpg))+geom_point()+geom_smooth(method=lm)
scatter.plot

  1. Design a visualization of your choice using ggplot using the data and write a brief summary about why you chose that visualization.

This scatter plot shows that wt and drat has an approximate linear relationship

scatter.plot2<-ggplot(mtcars, aes(x=wt, y=drat))+geom_point()+geom_smooth(method=lm)
scatter.plot2