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.

data(mtcars)
View(mtcars)

#To get unique carb values and their frequency
table(mtcars$carb)
## 
##  1  2  3  4  6  8 
##  7 10  3 10  1  1
carbvalue1 = length(which(mtcars$carb == 1))
carbvalue2 = length(which(mtcars$carb == 2))
carbvalue3 = length(which(mtcars$carb == 3))
carbvalue4 = length(which(mtcars$carb == 4))
carbvalue6 = length(which(mtcars$carb == 6))
carbvalue8 = length(which(mtcars$carb == 8))
carbsvalues = c(carbvalue1, carbvalue2, carbvalue3, carbvalue4, carbvalue6, carbvalue8)
carbsvalues
## [1]  7 10  3 10  1  1
carb_percentage = carbsvalues/sum(carbsvalues)*100
#colors = c("cyan","red","yellow","Green","grey","brown")
colors = c("palegreen","pink","lightskyblue1","lightgoldenrod1","lavender","mediumorchid1")
label = c("carbvalue 1","carbvalue 2","carbvalue 3","carbvalue 4","carbvalue 6","carbvalue 8") 
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 different carb values 1, 2, 3, 4, 6 and 8. 31.25% of the cars have carb value 2 and carb value 4. 21.875% cars have carb value 1 and 9.375% have carb value 3. 3.125% of cars have carb value 6 and carb calue 8.

2. Create a bar graph, that shows the number of each gear type in mtcars.

noofgears = table(mtcars$gear)
barplot(noofgears,main = "Frequency of Cars for number of gears",xlab ="Number of Gears",ylab = "Number of Cars", names.arg=c("3 Gears", "4 Gears", "5 Gears"), cex.names=0.8, col=c("lightskyblue1","palegreen",'pink'))

Summary:

From the above bar graph we can see that cars in mtcars dataset have 3 type of gears, 3, 4 nd 5. There are 15 of 3 gears cars, 12 of 4 gears cars and the number of 5 gears cars is 5.

3. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.

noofcyl = table(mtcars$cyl, mtcars$gear)
barplot(noofcyl, 
        main="Frequency of Cars by Gears and Cylinders",
        xlab="Number of Gears", 
        names.arg=c("3 Gears", "4 Gears", "5   Gears"),
        cex.names=0.8,
        ylab="Number of Cars",
        col=c("ivory","peachpuff","lightgray"),
        legend = rownames(noofcyl))

Summary:

The stacked bar chart above shows: 1) 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. 2) There are 12 4-gear cars with 8 cars having 4 cylinders and 4 cars with 6 cyliners. 3) There are 5 5-gear cars with 2 cars having 4 cylinders and 8 cylinders each and 1 car with 6 cyliners.

4. Draw a scatter plot showing the relationship between wt and mpg.

plot(mtcars$wt ~ mtcars$mpg, main="Relationship between Weight (wt) and Miles per Gallon (mpg)", xlab="Car Weight ", ylab="Miles Per Gallon ", pch = 19) + 
abline (lm (mtcars$wt ~ mtcars$mpg))

## integer(0)

Summary :

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

5. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.

boxplot(mtcars$mpg ~ mtcars$gear, main="Boxplot of Mpg v/s Number of Gears", 
    xlab="Number of Gears", ylab="Miles Per Gallon",names=c("3 Gear", "4 Gear", "5 Gear"))

Summary:

Boxplot show us the median, interquartile ranges, range and the outliers. It is clear from the above box plots that mpg is maximum for 4-gear cars. For 4 gear, the median mpg value is about 23 and the maximum and minimum mpg values are 34 and 18 mpg respectively. Similarly for 3 gear car, median is around 16, maximum is arund 22 and minimum is aroung 11 mpg. And for 5 gear car, median is around 19, maximum is around 30 and minimum is around 16 mpg.