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 of your graphics.
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.
Find the mtcars data in R. This is the dataset that you will use to create your graphics.
mtcars data set that have different cylinder (cyl) values.# place the code to import graphics here
summary(mtcars)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
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 ...
cylfreq=table(mtcars$cyl)
percentlabels=round(100*cylfreq/sum(cylfreq),1)
pielabels=paste(percentlabels,"%",sep="")
pie(cylfreq,labels = pielabels , col=terrain.colors(length(cylfreq)), main = 'Cars with Different Cylinder Values', cex = 0.8)
legend("topright", rownames(cylfreq), cex=0.6, fill=terrain.colors(length(cylfreq)))
The pie chart indicates that cars with 8 cylinders have the largest proportion (43.8%), followed by 4 cylinders (34.4%) and 6 cylinders (21.9%).
carb type in mtcars.# place the code to import graphics here
carbfreq = table(mtcars$carb)
carbfreq1=as.numeric(carbfreq)
xx=barplot(carbfreq1,main = 'Cars with Different Carb Values', ylab = 'Frequency', xlab = 'Number of Carb')
text(x = xx, y = carbfreq1, label = carbfreq1, pos = 1, cex = 0.8, col = "red")
axis(1, at=xx, labels=rownames(carbfreq), tick=FALSE, las=1, line=-0.5, cex.axis=0.8)
The bar graph shows that cars with 2 and 4 carbs have the highest frequency of 10 while 6 and 8 carbs are least common, each having only one presence.
gear type and how they are further divided out by cyl.# place the code to import graphics here
gearcyl <- table(mtcars$cyl, mtcars$gear)
barplot(gearcyl, main = "Cars by Gears vs Cyl", xlab = "Number of Gears",ylab= "Frequency", col=rainbow(length(gearcyl)),legend = rownames(gearcyl))
The stacked bar graph indicates that most of the cars with 3 gears have 8 cylinders, most cars with 4 gears have 4 cylinders, and cars with 5 gears have a pretty even distribution of cylinders.
wt and mpg.# place the code to import graphics here
plot(mtcars$wt , mtcars$mpg, xlab = 'Weight', ylab = 'Miles per Gallon', main = 'Relationship between Weight and MPG')
According to the scatter plot, weight and mpg are negatively related. As weight increases, mpg decreases and vice versa.
# place the code to import graphics here
summary(mtcars)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mtcars$cyl2=as.factor(mtcars$cyl)
mtcars %>%
group_by(gear,cyl2) %>%
summarise(weight=mean(wt)) %>%
ggplot(aes(gear,weight,fill=cyl2)) +
geom_bar(stat='identity', position='dodge') + theme(axis.line = element_line(colour = "black"), panel.background = element_blank()) +
ggtitle("MPG by Gear and Cylinder")+
theme(plot.title = element_text(hjust = 0.5))
Trying to understand relationship between weight and both gear & cylinder. Based on the bar chart, the more gears a car has, the lighter it is. While the more cylinders it has, the heavier it is. Cars with 3 gears and 8 cylinders are the most heavy.