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.
A couple of tips, remember that there may be preprocessing involved in your graphics so you may have to do summaries or calculations to prepare, those should be included in your work.
To ensure accuracy pay close attention to axes and labels, you will be evaluated based on the accuracy and expository nature of your graphics. Make sure your axis labels are easy to understand and are comprised of full words with units if necessary.
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.
Find the mtcars data in R. This is the dataset that you will use to create your graphics.
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()
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()
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()
wt and mpg.ggplot(mtcars, aes(wt,mpg)) +
geom_point() + theme_minimal()
# 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'
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
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
#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
library(gridExtra)
grid.arrange(p1, p2, p3, p4, ncol = 2)
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
#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)
#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