Anirudh Gurnani Jan,14,2019

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.
#View(mtcars)
unique(mtcars$carb) # we have 6 unique carb values.
## [1] 4 1 2 3 6 8
carb1 <- length(which(mtcars$carb == 1))
carb2 <- length(which(mtcars$carb == 2))
carb3 <- length(which(mtcars$carb == 3))
carb4 <- length(which(mtcars$carb == 4))
carb6 <- length(which(mtcars$carb == 6))
carb8 <- length(which(mtcars$carb == 8))
carbs <- c(carb1, carb2, carb3, carb4, carb6, carb8)
pcnt <- sprintf("%1.1f%%",100*(carbs/sum(carbs)))
lbl <- c("carbval_1", "carbval_2", "carbval_3", "carbval_4", "carbval_6", "carbval_8")
lbl <- paste(lbl, pcnt, sep="\n")
pie(carbs, labels=lbl, col=rainbow(6), radius=1, main="Car Proportions\n (by carb values)")

Summary: From the above Pie chart we can find that mtcars has 6 different values which is 1,2,3,4,6 and 8. The majority of carsb values from 2 amd 4 with 31.1%.where as carb value 1 and 3 which has 21.9% and 9.4% respectively, 6 and 8 carb value has 3.1% which is smallest among all.

  1. Create a bar graph, that shows the number of each gear type in mtcarsand write a brief summary.
Gear <- table(mtcars$gear)
barplot(Gear, xlab="Number of Gears", ylab="Number of Cars", col=rainbow(3))
axis(2,at=seq(0, 15, 1))

Summary: From the above graph we can conclude that there are 3 types of gear number which is 3-Gear,4-Gear,5-Gear. We also analyzed that 3-gear type has the largest number of cars which is 15 ,where as for 4-gear and 5-gear has 12 and 5 cars repectively.

  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
counts <- table(mtcars$cyl, mtcars$gear)
barplot(counts, main="Car Distribution 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("blue","red","yellow"),
    legend = rownames(counts))

Summary: From the above stacked bar chart shows that : 1) the majority of 3 gears cars have 8 cylinders. 2) the number of 4 gears cars with 4 cylinders is as twice as the number of 4 gears cars with 6 cylinders. 3) Amoung 5 gear cars, the propotion of 6 cylinder cars is the smallest.

  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, main="Weight VS Mpg", 
    xlab="Car Weight ", ylab="Miles Per Gallon ", pch=18)
abline(lm(mtcars$mpg~mtcars$wt), col="red") # regression line (y~x) 
lines(lowess(mtcars$wt,mtcars$mpg), col="blue") # lowess line (x,y)

Summary: From the scatter plot we can see clearly see that there is a negative relationship between weight and mpg.So we can say that when the weight of a car increases, the mpg of the car is decreases.

  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="MPG VS Number of Cylinders", 
    xlab="Number of Cylinders", ylab="Miles Per Gallon",names=c("4 cyl", "6 cyl", "8 cyl"))

Summary: The reason for chossing the boxplot is because it provide us the detailed information in a single chart. It tells us not only the distribution of the surveyed cars by cylinders, but also the 5-number statistical summary about mpg of each cylinder group. The middle portion of each box is the interquartile range, the bold line inside of it indicates the median, the lower rim of the rectangle represents the first quartile and the upper rim of the rectangle stands for the third quartile. The lower line attached to the rectangle indicates the minimum and the upper line inidcates the maximum. From the boxplot we can say that the 4 cylinder group, the average mpg value is about 26, the minimum mpg is about 22, and the maximum mpg is about 34. Compare with 4 cylinder group, the cars in 6 and 8 cylinder group have much lower mpg value. The average mpg of cars in 6 cylinder group is about 20 and this value in 8 cylinder group is 15.