Visualization analysis using mtcars dataset

  1. Draw a pie chart showing the proportion of cars from the mtcars data set that have different carb values.

The solution requires us to first create a table that shows distribution of cars based on the number of carburetors. We then calculate proportion of each carburetor number of the total. We then multiply that proportion with 360 to get the section of the pie for each carburetor number in the pie chart.

#Solution through R code

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
car_carb<-table(mtcars$carb)
car_carb
## 
##  1  2  3  4  6  8 
##  7 10  3 10  1  1
pie(car_carb,main = "Cars with different carburetors")

# Hand drawn image

knitr::include_graphics("D:/Google Drive Sync/Harrisburg University/CourseWork/Trimester 3/Data Visualization/Week 1/Homework/Homework images/compressed images/answer_1.jpg")

  1. Draw a bar graph, that shows the number of each gear type in mtcars.

The solution requires us to first create a frequency distribution table of cars based on the type of gears. We then draw that out in form of a bar chart.

#Solution through R code

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
gear_type<-table(mtcars$gear)
gear_type
## 
##  3  4  5 
## 15 12  5
barplot(gear_type,main = "Cars with different gear types")

# Hand drawn image

knitr::include_graphics("D:/Google Drive Sync/Harrisburg University/CourseWork/Trimester 3/Data Visualization/Week 1/Homework/Homework images/compressed images/answer_2.jpg")

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

The solution requires us to first create a frequency distribution table of cars gear type based on the number of cylinders. The columns are cylinders and rows are gears in that table. We then draw that out in form of a stacked bar graph

#Solution through R code

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
table1<-table(mtcars$gear,mtcars$cyl)
table1
##    
##      4  6  8
##   3  1  2 12
##   4  8  4  0
##   5  2  1  2
barplot(table1,main = "Car gear types with different cylinders")

# Hand drawn image

knitr::include_graphics("D:/Google Drive Sync/Harrisburg University/CourseWork/Trimester 3/Data Visualization/Week 1/Homework/Homework images/compressed images/answer_3.jpg")

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

This solution requires us to draw a graph with weight on x axis and mileage on y axis. We then plot the values on the graph.

#Solution through R code

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
plot(mtcars$wt,mtcars$mpg) 

# Hand drawn image

knitr::include_graphics("D:/Google Drive Sync/Harrisburg University/CourseWork/Trimester 3/Data Visualization/Week 1/Homework/Homework images/compressed images/answer_4.jpg")

  1. Design a visualization of your choice using the data.

I have decided to use boxplot to show the spread of car horse power based on car cylinders.

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
boxplot(mtcars$hp~mtcars$cyl)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.