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 carb values and write a brief summary.
# place the code to import graphics here
View(mtcars)
unique(mtcars$carb) #This is to get the unique carb values. We have 6 unique carb values
## [1] 4 1 2 3 6 8
carbvalue4 <- length(which(mtcars$carb == 4))
carbvalue1 <- length(which(mtcars$carb == 1))
carbvalue2 <- length(which(mtcars$carb == 2))
carbvalue3 <- length(which(mtcars$carb == 3))
carbvalue6 <- length(which(mtcars$carb == 6))
carbvalue8 <- length(which(mtcars$carb == 8))
carbsvalues <- c(carbvalue4, carbvalue1, carbvalue2, carbvalue3, carbvalue6, carbvalue8)
carbsvalues
## [1] 10  7 10  3  1  1
colors <- c("cyan","red","yellow","Green","grey","brown")
carb_percentage <- carbsvalues/sum(carbsvalues)*100
label <- c("carbvalue4","carbvalue1","carbvalue2","carbvalue3","carbvalue6","carbvalue8") 
label <- paste(label,sep="   ", carb_percentage )
label<-paste(label,"%")
pie(carbsvalues,label=label,main="Proportions of Cars by Carb Values",radius =1,col=colors)

Summary:

From the above pie chart, we can see that there are 6 carb values namely 4,1,2,3,6,8. 31.25% of the cars have carb value2 which is also same as carb value4 followed by cars having carb value1 and carb value3 which is 21.875% and 9.375% respectively. Carb value6 and carb value8 have equual proportion of cars which is 3.125%

  1. Create a bar graph, that shows the number of each gear type in mtcarsand write a brief summary.
# place the code to import graphics here
gear<-table(mtcars$gear)
barplot(gear,main = "The Bar plot for mtcars gears",xlab ="Number of Gears",ylab = "Number of Cars", col=c("Blue","Grey",'Yellow'))

Summary:

From the above bar graph we can see, there are 3 types of gears used in mtcars dataset. These are 3gear, 4gear, and 5gear. The majority of cars have 3gear and 4gear and the least number of cars have 5gear.

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyland write a brief summary.
# place the code to import graphics here
Cyl <- table(mtcars$cyl, mtcars$gear)
barplot(Cyl, main="Cars Distribution by Gears and Cylinders",
        xlab="Number of Gears", 
        ylab="Number of Cars",
        col=c("Blue","Grey",'Yellow'),
        legend=rownames(Cyl), args.legend=list(title="Number of Cylinders"))

Summary :

Out of the 15 3-gear cars, there are 12 cars with 8 cylinders, 2 cars with 6 cylinders and 1 car with 4 cylinders. Similarly, there are 12 4-gear cars with 8 cars having 4 cylinders and 4 cars with 6 cyliners. Likewise, there are only 5 5-gear cars with 2 cars having 4 cylinders and 8 cylinders each and 1 car with 6 cyliners.

  1. Draw a scatter plot showing the relationship between wt and mpgand write a brief summary.
# place the code to import graphics here
plot(mtcars$wt, mtcars$mpg, xlab="Weight", ylab="Miles per Gallon")
title("Relationship between Weight (wt) and Miles per Gallon (mpg)")

Summary :

From the above scatter plot we see that there is a negative relationship between weight and miles per gallon.

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
# place the code to import graphics here
boxplot(mpg~cyl,data=mtcars, main="Boxplot of Mpg v/s Number of Cylinders", 
    xlab="Number of Cylinders", ylab="Miles Per Gallon",names=c("4 cyl", "6 cyl", "8 cyl"))

Summary:

Boxplot show us the median, interquartile ranges, range and the outliers. It is clear from the above box plots that mpg decreases as we increase the cylinder. For 4 cylinder, the median mpg value is about 26 and the maximum and minimum mpg values are 34 and 22 respectively. Similarly other values for 6 and 8 cylinders could be obtained.