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.

Part 1 - ggplot Basics

Find the mtcars data in R. This is the dataset that you will use to create your graphics.

  1. Create a box plot using ggplot showing the range of values of 1/4 mile time (qsec) for each tansmission type (am, 0 = automatic, 1 = manual) from the mtcars data set.
library(ggplot2)

data("mtcars")
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 ...
mtcars$am <- as.factor(mtcars$am)

ggplot(mtcars, aes(am, qsec)) +
  geom_boxplot() +
  labs(x="Transmission type",
       y="1/4 mile time (seconds)")  + 
  scale_x_discrete(labels = c("Automatic", "Manual")) + 
  theme_minimal()

  1. Create a bar graph using ggplot, that shows the number of each carb type in mtcars.
ggplot(mtcars, aes(carb)) +
  geom_bar() +
  labs(x="Number of Carburetors") + 
  scale_y_continuous(breaks = seq(0, 10, by = 2)) + 
  theme_minimal()

  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.
unique(mtcars$cyl)
## [1] 6 4 8
unique(mtcars$gear)
## [1] 4 3 5
mtcars$gear <- as.factor(mtcars$gear)
mtcars$cyl <- as.factor(mtcars$cyl)
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 : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
##  $ 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  : Factor w/ 2 levels "0","1": 2 2 2 1 1 1 1 1 1 1 ...
##  $ gear: Factor w/ 3 levels "3","4","5": 2 2 2 1 1 1 1 2 2 2 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
ggplot(mtcars, aes(gear)) +
  geom_bar(aes(fill=cyl)) +
  labs(x="Number of Gear", fill="Cylinders") + theme_minimal()

  1. Draw a scatter plot using ggplot showing the relationship between wt and mpg.
ggplot(mtcars, aes(wt,mpg)) +
  geom_point() + theme_minimal()

  1. Design a visualization of your choice using ggplot using the data and write a brief summary about why you chose that visualization.
# I am plotting a scatterplot pf horsepower vs 1/4 mile time to see if there is any relation between the two. Looks like there is some relation and 1/4 mile time decreases as horsepower increases.
ggplot(mtcars, aes(hp,qsec)) +
  geom_point() + geom_smooth(se = FALSE) +
  labs(x="Horsepower",
       y="1/4 mile time") +
  theme_minimal()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Part 2 - Anscombe’s Quartet

  1. Anscombes quartet is a set of 4 \(x,y\) data sets that were published by Francis Anscombe in a 1973 paper Graphs in statistical analysis. For this first question load the anscombe data that is part of the library(datasets) in R. And assign that data to a new object called data.
library(datasets)
data <- datasets::anscombe
  1. Summarise the data by calculating the mean, variance, for each column and the correlation between each pair (eg. x1 and y1, x2 and y2, etc) (Hint: use the fBasics() package!)
library("fBasics")
## Loading required package: timeDate
## Loading required package: timeSeries
sapply(data, mean)
##       x1       x2       x3       x4       y1       y2       y3       y4 
## 9.000000 9.000000 9.000000 9.000000 7.500909 7.500909 7.500000 7.500909
sapply(data, sd)
##       x1       x2       x3       x4       y1       y2       y3       y4 
## 3.316625 3.316625 3.316625 3.316625 2.031568 2.031657 2.030424 2.030579
#Pair 1
correlationTest(data$x1,data$y1)
## 
## Title:
##  Pearson's Correlation Test
## 
## Test Results:
##   PARAMETER:
##     Degrees of Freedom: 9
##   SAMPLE ESTIMATES:
##     Correlation: 0.8164
##   STATISTIC:
##     t: 4.2415
##   P VALUE:
##     Alternative Two-Sided: 0.00217 
##     Alternative      Less: 0.9989 
##     Alternative   Greater: 0.001085 
##   CONFIDENCE INTERVAL:
##     Two-Sided: 0.4244, 0.9507
##          Less: -1, 0.9388
##       Greater: 0.5113, 1
## 
## Description:
##  Sun Aug  9 21:09:29 2020
#Pair 2
correlationTest(data$x2,data$y2)
## 
## Title:
##  Pearson's Correlation Test
## 
## Test Results:
##   PARAMETER:
##     Degrees of Freedom: 9
##   SAMPLE ESTIMATES:
##     Correlation: 0.8162
##   STATISTIC:
##     t: 4.2386
##   P VALUE:
##     Alternative Two-Sided: 0.002179 
##     Alternative      Less: 0.9989 
##     Alternative   Greater: 0.001089 
##   CONFIDENCE INTERVAL:
##     Two-Sided: 0.4239, 0.9506
##          Less: -1, 0.9387
##       Greater: 0.5109, 1
## 
## Description:
##  Sun Aug  9 21:09:29 2020
#Pair 3
correlationTest(data$x3,data$y3)
## 
## Title:
##  Pearson's Correlation Test
## 
## Test Results:
##   PARAMETER:
##     Degrees of Freedom: 9
##   SAMPLE ESTIMATES:
##     Correlation: 0.8163
##   STATISTIC:
##     t: 4.2394
##   P VALUE:
##     Alternative Two-Sided: 0.002176 
##     Alternative      Less: 0.9989 
##     Alternative   Greater: 0.001088 
##   CONFIDENCE INTERVAL:
##     Two-Sided: 0.4241, 0.9507
##          Less: -1, 0.9387
##       Greater: 0.511, 1
## 
## Description:
##  Sun Aug  9 21:09:29 2020
#Pair 4
correlationTest(data$x4,data$y4)
## 
## Title:
##  Pearson's Correlation Test
## 
## Test Results:
##   PARAMETER:
##     Degrees of Freedom: 9
##   SAMPLE ESTIMATES:
##     Correlation: 0.8165
##   STATISTIC:
##     t: 4.243
##   P VALUE:
##     Alternative Two-Sided: 0.002165 
##     Alternative      Less: 0.9989 
##     Alternative   Greater: 0.001082 
##   CONFIDENCE INTERVAL:
##     Two-Sided: 0.4246, 0.9507
##          Less: -1, 0.9388
##       Greater: 0.5115, 1
## 
## Description:
##  Sun Aug  9 21:09:29 2020
  1. Create scatter plots for each \(x, y\) pair of data.
#Pair 1
p1 <- ggplot(data, aes(x1,y1)) + geom_point() + theme_minimal()
#Pair 2
p2 <- ggplot(data, aes(x2,y2)) + geom_point() + theme_minimal()
#Pair 3
p3 <- ggplot(data, aes(x3,y3)) + geom_point() + theme_minimal()
#Pair 4
p4 <- ggplot(data, aes(x4,y4)) + geom_point() + theme_minimal()

p1

p2

p3

p4

  1. Now change the symbols on the scatter plots to solid circles and plot them together as a 4 panel graphic
library(gridExtra)
grid.arrange(p1, p2, p3, p4, ncol = 2)

  1. Now fit a linear model to each data set using the lm() function.
lm(x1~y1, data = data)
## 
## Call:
## lm(formula = x1 ~ y1, data = data)
## 
## Coefficients:
## (Intercept)           y1  
##     -0.9975       1.3328
lm(x2~y2, data = data)
## 
## Call:
## lm(formula = x2 ~ y2, data = data)
## 
## Coefficients:
## (Intercept)           y2  
##     -0.9948       1.3325
lm(x3~y3, data = data)
## 
## Call:
## lm(formula = x3 ~ y3, data = data)
## 
## Coefficients:
## (Intercept)           y3  
##      -1.000        1.333
lm(x4~y4, data = data)
## 
## Call:
## lm(formula = x4 ~ y4, data = data)
## 
## Coefficients:
## (Intercept)           y4  
##      -1.004        1.334
  1. Now combine the last two tasks. Create a four panel scatter plot matrix that has both the data points and the regression lines. (hint: the model objects will carry over chunks!)
#Pair 1
p1 <- ggplot(data, aes(x1,y1)) + geom_point() + geom_smooth(method = "lm") + theme_minimal()
#Pair 2
p2 <- ggplot(data, aes(x2,y2)) + geom_point() + geom_smooth(method = "lm") + theme_minimal()
#Pair 3
p3 <- ggplot(data, aes(x3,y3)) + geom_point() + geom_smooth(method = "lm") + theme_minimal()
#Pair 4
p4 <- ggplot(data, aes(x4,y4)) + geom_point() + geom_smooth(method = "lm") + theme_minimal()

grid.arrange(p1, p2, p3, p4, ncol = 2)

  1. Now compare the model fits for each model object.
#Pair 1
lm(x1~y1, data = data)

Call: lm(formula = x1 ~ y1, data = data)

Coefficients: (Intercept) y1
-0.9975 1.3328

#Pair 2
lm(x2~y2, data = data)

Call: lm(formula = x2 ~ y2, data = data)

Coefficients: (Intercept) y2
-0.9948 1.3325

#Pair 3
lm(x3~y3, data = data)

Call: lm(formula = x3 ~ y3, data = data)

Coefficients: (Intercept) y3
-1.000 1.333

#Pair 4
lm(x4~y4, data = data)

Call: lm(formula = x4 ~ y4, data = data)

Coefficients: (Intercept) y4
-1.004 1.334

  1. In text, summarize the lesson of Anscombe’s Quartet and what it says about the value of data visualization.

Though the summary statistics (mean and standard deviation) for the pairs were almost the same and aslo the regression line and model fits are the same, the four different data pairs are very different. It is very easily evident when we plot this data on a scatter plot. It goes on to say that visualizing the data can give us a clearer picture of what the data is and may help uncover characteristics of the data that are not very evident.