Chandi Prasanna Malepu

January 12, 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.
# place the code to import graphics here
#View(mtcars)
unique(mtcars$carb) # we have 6 unique carb values.
## [1] 4 1 2 3 6 8
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
colors <- c("green","blue","yellow","red","grey","orange")
carb_percentage <- carbsvalues/sum(carbsvalues)*100
label <- c("carbvalue1","carbvalue2","carbvalue3","carbvalue4","carbvalue6","carbvalue8") 
label <- paste(label, carb_percentage ,sep=" ")
label<-paste(label,"%")
pie(carbsvalues,label=label,main="The Pie Chart for Carb Values",col=colors)
legend("left", c("carbvalue1","carbvalue2","carbvalue3","carbvalue4","carbvalue6","carbvalue8"), fill = colors, cex=0.8)

Summary:

From the pie chart, we can see 6 different carb values they are 1, 2, 3, 4, 6 and 8. The majority of the cars have carb value of 2 and 4 which is 31.25%. Followed by carb value of 1 and 3 which is 21.875% and 9.375% , 6 and 8 carburetors are the smallest, which is only 3.125%.

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

Summary:

From the bar graph we can see 3 types of gears, they are 3gear, 4gear, and 5gear. The majority of cars have 3gear and 4gear. 15 number of cars has 3gear and 12 cars has 4gear. The least number of cars has 5gear.

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

Summary:

For the 3gear, majority of the cars have 8 cyl. The number of 4 gears cars with 4 cyl is as twice as the number of 4 gears cars with 6 cyl. For the 5gear cars, small proportin of cars have 6 cyl.

  1. Draw a scatter plot showing the relationship between wt and mpg.
# place the code to import graphics here
plot(mtcars$wt,mtcars$mpg,main="Scatter plot of  relationship between  wt and mpg ",xlab="Weight of the car",ylab="Miles per Gallon",pch=20,col='red')

Summary:

From the scatter plot we can see clearly that there is a negative relationship between weight and mpg.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 Cyl", 
    xlab="Number of Cylinders", ylab="Miles Per Gallon",names=c("4 cyl", "6 cyl", "8 cyl"))

Summary:

I have choosen boxplots for this visualization because it provides a lot of information in a single chart. We can see the range, the median, interquartile ranges and the outliers easily and it is very effective for visual exploratory analysis. From the box plot it tells the 4 cylinder group, the median(average) mpg value is about 26 and the maximum mpg value is 34 and the minimum mpg value is 22. Compared with 4 cylinder group, the cars in 6 and 8 cylinder group have much lower mpg value. The median(average) mpg of cars in 6 cylinder group is about 20 and for 8 cylinder group it is 15.