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.
library(plotrix)
## Warning: package 'plotrix' was built under R version 3.4.4
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
data("mtcars")

head(mtcars, 10)
##                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
carstable <- table(mtcars$carb)
lbls <- names(carstable)
percentage <- round(carstable/sum(carstable)*100, 2)
lbls <- paste(lbls, percentage, sep = "    ") # carb and car proportion to labels 
lbls <- paste(lbls,"%",sep=" ") # % to labels 

pie3D(percentage,labels=lbls,explode=0.1,
      main="Proportion of Cars have different carburetor 
        and % ")

The pie chart demonstrate the proportion of cars based on the carburetor. Cars in each category of carburetor 2 and 4 shows the maximum proportions of cars, which is approximately 31%.

  1. Create a bar graph, that shows the number of each gear type in mtcarsand write a brief summary.
Geartable= table(mtcars$gear)
barplot(Geartable, xlab = "Number of Gears", ylab = "No. of Cars", 
        col = c("red", "blue", "grey"))

The Bar Chart shows the number of gears (3, 4, and 5) for all the cars category. With X axis show the number of gears and y - axis shows the no. of cars belong to the different gear category.

  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.
Gearcountcylinderwise <- table(mtcars$gear, mtcars$cyl)
barplot(Gearcountcylinderwise,main="Car Distribution by Gears and Cyl",xlab="Number of Gears", col=c("yellow","red","green"),legend = rownames(Gearcountcylinderwise)
    , args.legend = list(title = "Number of Cylinders"))

This stacked bar plot is the extension of the previous bar chart and it contain multiple informations.The X -axis shows number of gears along with the number of cylinders and y- axis show the number of cars belong to the different category of cylinders and different number of gears. With 4 and 8 gears four and three cylinders respectively show the maximum proportion for all the cars,

  1. Draw a scatter plot showing the relationship between wt and mpgand write a brief summary.
attach(mtcars)
## The following object is masked from package:ggplot2:
## 
##     mpg
plot(wt, mpg, main="Scatterplot Example", 
    xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)

Scatter plot show the car’s miles per gallon distribution with respect to the car weight. Less car weight show maximum miles per gallon. Large number of cars fall under 3 -4 car weight with miles per gallon vary from 15-25.

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
ggplot(mtcars, aes(x = wt, y = mpg))+
  geom_point(aes(shape=factor(cyl), colour=factor(cyl))) +
  scale_shape_identity(name="Cylinders") +
  scale_colour_hue(name="Cylinders")

boxplot(mtcars$hp ~ mtcars$cyl, main = "Box Plot of HorsePower vs Number of Cylinders", xlab = "Number of Cylinders", ylab = "Horse Power", 
        col = "red")

I have chosen the two plots, first plot is to get the visualization for the mile per gallon variation as per the weight change and also to know how the weight effect by the number of cylinders. Through plot you can clearly see that mileage of car changes with the increase or decrease in weight and if you increase the number of cylinders than its obvious that your car weight is going to increase. Second plot show how horse power are related to the number of cylinders. Horsepower increases with the increase in number of cylinders. In summary, you can increase car horsepower with increase in number of cylinders but at the same time you have to compromise with the mileage of car.