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.

data(mtcars) str(mtcars) summary(mtcars)

  1. Create a pie chart showing the proportion of cars from the mtcars data set that have different carb values.
mtcarscarb <- table(mtcars$carb)
pct <- round(mtcarscarb/sum(mtcarscarb)*100)
pie(mtcarscarb,main="Proportion of cars by carb values",labels = paste(pct, "%", sep  = ""), col = rainbow(length(mtcarscarb)))
legend("topright", c("Carb1","Carb2","Carb3","Carb4","Carb6","Carb8"), fill=  rainbow(length(mtcarscarb)))

##Highest proportion of cars have either Carb2 or Carb4 (31%), while lowest proportion of cars have Carb6 or Carb8 (3%).
  1. Create a bar graph, that shows the number of each gear type in mtcars.
mtcarsgear <- table(mtcars$gear)
barplot(mtcarsgear, main = "Count of gear types", ylab = "Count", xlab = "Number of gears", col = rainbow(length(mtcarsgear)))

##More cars have 3 or 4 gears while very few cars have 5 gears.
  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
mtcarsgearcyl <- table(mtcars$cyl, mtcars$gear)
barplot(mtcarsgearcyl, main = "Distribution of gears by cyl", xlab = "Number of gears",ylab= "Count", col = rainbow(length(mtcarsgearcyl)), legend = rownames(mtcarsgearcyl))

##Majority of 3 gear cars have 8 cylinders, while majority of 4 gear cars and 5 gear cars have 4 cylinders and 4 or 8 cylinders, respectively.
  1. Draw a scatter plot showing the relationship between wt and mpg.
plot(mtcars$wt , mtcars$mpg, xlab = 'Weight', ylab = 'Mpg', main = 'Weight vs Mpg', pch =10)
abline(lm(mtcars$mpg~mtcars$wt), col="red")

##Weight and mpg have a negative relationship, i.e. as weight increases, mpg decreases.
  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
boxplot(mpg~cyl, data = mtcars, main="Mpg VS Number of cylinders", xlab="Number of cylinders", ylab="Mpg",names=c("4cyl", "6cyl", "8cyl"))

##Boxplot is a good representation to understand the distribution of mileage of cars (mpg) by the number of cylinders. 4 clyinder cars have the best mileage, with an average of 26 mpg, minimum of 21 mpg and maximum of 34 mpg; while 6 cylinder cars give 20 mpg and 8 cylinders give 15 mpg on average.