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 is preprocessing involved in many 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 carb values.# place the code to import graphics here
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 ...
carbtype <- unique(mtcars$carb)
type1 <- length(which(mtcars$carb==1))
type2 <- length(which(mtcars$carb==2))
type3 <- length(which(mtcars$carb==3))
type4 <- length(which(mtcars$carb==4))
type6 <- length(which(mtcars$carb==6))
type8 <- length(which(mtcars$carb==8))
AllTypes <- c(type1,type2,type3,type4,type6,type8)
Portion <- sprintf("%1.1f%%",100*(AllTypes/sum(AllTypes)))
Lable <- c("Carb 1","Carb 2","Carb 3","Carb 4","Carb 6","Carb 8")
Lable <- paste(Lable,Portion,sep="\n")
pie(AllTypes,labels=Lable,col = rainbow(6),radius = 1,main = "Carb Pie Chart")
## Summary ### In our dataset there are 6 differnet types of carb valued from 1,2,3,4,6,8. from charte we can see type 2 and type 4 has the most cars and followed by type 1. type 2 and 4 each represent 31.2% of cars and type 1 represent 21.9%
gear type in mtcars.# place the code to import graphics here
gear <- table(mtcars$gear)
unique(gear)
## [1] 15 12 5
barplot(gear, main = "Gear Distribution", xlab = "Number of Gears",ylab = "Number of Cars",names.arg = c("3 Gears","4 Gears","5 Gears"), cex.names = 1,col = c("yellow","red","blue") )
gear type and how they are further divided out by cyl.# place the code to import graphics here
df <- table(mtcars$cyl,mtcars$gear)
barplot(df,main = "Bar graph by Gears and Cyl",xlab = "Number of Gears",col = c("pink","yellow","orange"),legend= rownames(df))
wt and mpg.# place the code to import graphics here
plot(mtcars$wt,mtcars$mpg,main = "Scatter Relationship Between wt and mpg", xlab = "Car Weight",ylab = "MPG")
# place the code to import graphics here
library(ggplot2)
ggplot(mtcars,aes(mtcars$mpg,mtcars$hp))+ geom_line() + geom_point() + ggtitle("Relationship between MPG and Horse Power")