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
carb <- table(mtcars$carb)
labels<-names(carb)
Percentage <- round(carb/sum(carb)*100)
labels <- paste(labels, Percentage, sep = " ")
labels <- paste(labels, "%", sep = "")
pie(carb, labels = labels, col = rainbow(length(labels)), main="Proportion of cars with different carbs")
According to the pie chart, we can see cars with 2 carbs and 4 carbs have the largest proportion, with 31% for each and 62% for the total. And we have the least numbers of cars with 6 carbs and 8 carbs.
gear type in mtcars.# place the code to import graphics here
gear <-table(mtcars$gear)
barplot(gear, main = "Number of Cars in each Gear type", xlab = "Number of Gears", ylab = "Number of Cars", names.arg = c("3 Gears", "4 Gears", "5 Gears"), cex.names = 1, col = c("red", "blue", "pink"))
We can result from our bar plot that cars with 3 gears are the most popular cars in our dataset, with an amount larger than 12 and cars with 5 gears occupies the least proportion with an amount arount 4.
gear type and how they are further divided out by cyl.# place the code to import graphics here
counts<-table(mtcars$cyl, mtcars$gear)
barplot(counts, main = "Number of Cars with different Gears and Cylinders", xlab = "Number of Gears", names.arg =c("3 Gears", "4 Gears", "5 Gears"), cex.names = 1, ylab = "Number of Cars", col = c("blue", "pink", "purple"), legend = rownames(counts))
From our plot, we can see that more than 3/4 cars with 3 gears have 8 cylinders. For cars with 4 gears, the number of cars with 4 cylinders is twice as many as cars with 6 cylinders. Cars with 5 gears have nearly the same number of cars with 8 cylinders and 4 cylinders while the number of cars with 6 cylinider is the smallest.
wt and mpg.# place the code to import graphics here
plot(mtcars$wt, mtcars$mpg, main = "Relationship between Weight & MPG", xlab = "Car Weight", ylab = "MPG")
abline(lm(mtcars$mpg~mtcars$wt), col="pink")
lines(lowess(mtcars$wt, mtcars$mpg), col="purple")
The scatter plot shows a negative relationship between car weight and MPG as the amount of MPG decrease with the increase of car weight shown by the plots and the two lines. And the Lowess demonstrates the trend more accurately than regression line.
# place the code to import graphics here
boxplot(mtcars$mpg~mtcars$gear, data = mtcars, main="Relationship between MPG and Gears", xlab="Number of gears", ylab="MPG", names=c("3 Gears", "4 Gears", "5 Gears"))
I used boxplot to reflect the relationship between MPG and number of gears because boxplot can not only show the overall MPG performance situation for cars with different gears, but it can also show the summaries of 5-number statistics.
Based on the boxplot I draw, we can find out much information. For example, the average, maximum and minimal MPG of cars with 4 gears are all higher than the rest two kinds of car, which may make the car with 4 gears a more economic model. However, cars with 3 gears will cost much more gas driving.