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.
Create a pie chart showing the proportion of cars from the mtcars
data set that have different carb
values.
There are 6 carbs in the data set. Carb-2 and Carb-4 have the same largest proportion 31.2% followed by carb-1 (21.9%). Carb-3 is 9.4%. Carb-6 and Carb-8 have the smallest proportion 3.1%.
mtcars_carb <- table(mtcars$carb)
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 ...
pct_levels <- round(100*prop.table(mtcars_carb),1)
p_levels <- paste(pct_levels, "%", sep="")
pie(mtcars_carb,col = rainbow(length(mtcars_carb)), labels = p_levels , main = 'Pie Chart carb', cex = 0.8)
legend("topright", c("carb-1","carb-2","carb-3","carb-4","carb-6","Carb-8"), cex=0.6, fill= rainbow(length(mtcars_carb)))
Create a bar graph, that shows the number of each gear
type in mtcars
.
We have 3 types of gear. Type 3 is the most popular one followed by Type 4. Type 5 has the smallest amount of cars.
mtcars_gear<- table(mtcars$gear)
barplot(mtcars_gear, xlab="Number of Gears", ylab="Number of Cars", col=rainbow(3))
axis(2,at=seq(0, 15, 1))
Next show a stacked bar graph of the number of each gear
type and how they are further divided out by cyl
.
The graph shows gear 3 is the most popular type of gear and most of gear 3 cars have cylinders. Gear 4 is the second most popular type of gear but this gear has not 8 cylinders are. Most of the gear 4 cars are 4 cylinders. Gear 5 has the least amount of cars but it has all 3 different cylinder types of cars.
mtcars_cyl<- table(mtcars$cyl, mtcars$gear)
barplot(mtcars_cyl, main="Car Proportion by Gear and Cylinder",
xlab="Number of Gears",
names.arg=c("Gear 3", "Gear 4", "Gear 5"),
cex.names=1,
ylab="Number of Cars",
col=rainbow(3),
legend=rownames(mtcars_cyl), args.legend=list(title="Cylinder"))
axis(2,at=seq(0, 15, 1))
Draw a scatter plot showing the relationship between wt
and mpg
.
The relationship between car weight and mileage of gas is negative obviously. Heavier the car is, less miles it can run with every gallon of gas.
plot(mtcars$wt, mtcars$mpg, xlab="Weight", ylab="Miles per Gallon")
abline(lm(mtcars$mpg~mtcars$wt), col="green")
title("Relationship between car weight and miles per Gallon", line=1)
Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
I want to find the relationship between horse power and the number of cylinders. The relationship is positive. Because cylinder number is descrete so I can’t draw a trendline here.
plot(mtcars$cyl, mtcars$hp, xlab="Cylinder number", ylab="horse power")
abline(lm(mtcars$cyl~mtcars$hp), col="blue")
title("Relationship between horse power and numbers of cylinder", line=1)