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 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.
cylinder<-table(mtcars$cyl)
cylinder
##
## 4 6 8
## 11 7 14
prop.cyl<-round(100*cylinder/sum(cylinder), 1)
prop.cyl
##
## 4 6 8
## 34.4 21.9 43.8
pie1<-paste(prop.cyl, "%", sep="")
pie(prop.cyl,
labels=pie1,
col=c(rainbow(3)),
main="Proportion of cars with different cylinder values")
legend("topright", c("4 Cyl","6 Cyl","8 Cyl"),
cex=0.9,
fill=c(rainbow(3)))
This pie chart shows us that 34.4% of the cars from mtcars dataset have a 4-cylinder, 21.9% have a 6-cylinder, and 43.8% have an 8-cylinder car.
table1<-table(mtcars$carb)
table1
##
## 1 2 3 4 6 8
## 7 10 3 10 1 1
barplot(table1,
main="Carburetor distribution",
xlab="Carb types in a car",
ylab="Frequency",
names.arg=c("1-carb","2-carb","3-carb","4-carb","6-carb","8-carb"),
col=c(rainbow(6)))
legend("topright",c("1-carb","2-carb","3-carb","4-carb","6-carb","8-carb"),
cex=0.9,
fill=c(rainbow(6)))
This graph bar shows us that the most common type of carburators in cars (included in this dataset) are the 2 or 4-carb types. While cars with 6 or 8 carburetors are less frequent.
table2<-table(mtcars$cyl,mtcars$gear)
table2
##
## 3 4 5
## 4 1 8 2
## 6 2 4 1
## 8 12 0 2
barplot(table2,
main="Car distribution by gear & cylinder type",
xlab="Gear type",
names.arg=c("3-gears","4-gears","5-gears"),
cex.names=0.9,
ylab="Frequency",
col=c(rainbow(3)))
legend("topright",c("4-cyl","6-cyl","8-cyl"),
cex=0.9,
fill=c(rainbow(3)))
In this stacked bar graph you can see that the large majority of 3-gear cars also have 8-cylinders, compared to only 1 car having 3 gears & 4 cylinders. In the case of the 4-gear cars, more than 2 thirds of those have 4 cylinders. Lastly, 4 & 8 cylinders are equally common in 5-gear cars.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.6.3
scatter=ggplot(mtcars, aes(x=wt, y=mpg))
scatter + geom_point()+
xlab("Weight")+
ylab("Miles per gallon")+
labs(title="Relationship between weight & mpg")
There seems to be a negative relationship between weight and miles per gallon. The more the car weights, the less miles per gallon it has.
pie2<-table(mtcars$carb)
pie2
##
## 1 2 3 4 6 8
## 7 10 3 10 1 1
prop.pie2<-round(100*pie2/sum(pie2), 1)
prop.pie2
##
## 1 2 3 4 6 8
## 21.9 31.2 9.4 31.2 3.1 3.1
newpie<-paste(prop.pie2, "%", sep="")
pie(prop.pie2,
labels=newpie,
border="white",
col=c(rainbow(6)),
main="Proportion of cars with different carburetor type")
legend("topright", c("1-carb","2-carb","3-carb","4-carb","6-carb","8-carb"),
cex=0.9,
fill=c(rainbow(6)))
I think pie charts are a really great way of displaying data. Prior to this assignment, I wasn’t familiar with creating pie charts in RStudio. So i wanted to play around with this a little bit more. AS you can see, among all the cars in this dataset, the 2 & 4-carb type cars are the most common, whilst 6 & 8-carb types are the least frequent.