Directions

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.

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.

Questions

Find the mtcars data in R. This is the dataset that you will use to create your graphics.

  1. Create a pie chart showing the proportion of cars from the mtcars data set that have different cylinder (cyl) values.
mtcarscyl = table(mtcars$cyl)
percentlabels<- round(100*mtcarscyl/sum(mtcarscyl), 1)
pielabels<- paste(percentlabels, "%", sep="")
pie(mtcarscyl,col = rainbow(length(mtcarscyl)), labels = pielabels , main = 'Pie Chart for MTCars distribution of Cylinder', cex = 0.8)
legend("topright", c("Carburetor-1","Carburetor-2","Carburetor-3"), cex=0.6, fill=  rainbow(length(mtcarscyl)))

Based on the pie chart, it can be inferred that the majority of the car models had the cylinder value of Carburetor of 3 and 1, followed by 2.

  1. Create a bar graph, that shows the number of each carb type in mtcars.
mtcarscarb = table(mtcars$carb)
barplot(mtcarscarb, main = "Bar Plot for MTCars Carb", ylab = 'Frequency' ,xlab = "Carb Type")

The bar chart above indicates that the carb number of the majority of the cars were 2 or 4.

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
gearcyl <- table(mtcars$cyl, mtcars$gear)
barplot(gearcyl, main = "Stacked BarPlot for MTCars distribution by Gears Vs Cyl", xlab = "Number of Gears",ylab= "Frequency", col = c("yellow", "red", "green"), legend = rownames(gearcyl))

Based on the stacked bar graph above, it can be implied that the majority of cars having number of gears as 3 had 8 cylinders, and for cars having number of gears, most of them had 4 cylinders. But for cars having number of gears as 5, the distribution of number of cylinders in was equal.

  1. Draw a scatter plot showing the relationship between wt and mpg.
plot(mtcars$wt , mtcars$mpg, xlab = 'Weight of Cars', ylab = 'Miles per Gallon', main = 'Scatter Plot for MTCars Weight Vs MPG')
abline(lm(mtcars$mpg~mtcars$wt), col="blue")

From the scartter plot above, we can tell from the linear regression line in blue color that the car weight and mpg share negtive correlation with each other.

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
plot(mtcars$hp,mtcars$cyl,main="Relationship between hp and cyl", xlab="hp",ylab="cyl", pch=20)

The graph above is designed to show the relationship between horsepower and cylinder, and it indicated a posotive relationship - therefore, the cars with more powerful hp would more likely be equipped with more cyl.