# 1. Draw a pie chart showing the proportion of cars from the `mtcars` data set that have different `carb` values.
data('mtcars')
# Calculate the proportion of each carb category
table(mtcars$carb)/nrow(mtcars)
##
## 1 2 3 4 6 8
## 0.21875 0.31250 0.09375 0.31250 0.03125 0.03125
# Calculate the angle for each category used in the pie chart
degree_carb=table(mtcars$carb)/nrow(mtcars)*360
degree_carb
##
## 1 2 3 4 6 8
## 78.75 112.50 33.75 112.50 11.25 11.25
library(knitr)
knitr::include_graphics("C:/Users/Hannah/Desktop/Assignment/ANLY512/Week1 Course Intro/Graph1.JPG")
Summary: First, I calculated the proportion of each carb category. According to the proportions, I then calculated the angle for each category that I would use in the pie chart. The circle was devided into 6 parts for the 6 carb categories.
# 2. Draw a bar graph, that shows the number of each `gear` type in `mtcars`.
# Calculate the frequency of each type
table(mtcars$gear)
##
## 3 4 5
## 15 12 5
knitr::include_graphics("C:/Users/Hannah/Desktop/Assignment/ANLY512/Week1 Course Intro/Graph2.JPG")
Summary: I computed the frequency of each gear type. Each bar presented one gear type. Then the height of each bar was scaled according to the frequency.
# 3. Next show a stacked bar graph of the number of each `gear` type and how they are further divded out by `cyl`.
# Crosstable for gear and cyl
table(mtcars$gear, mtcars$cyl)
##
## 4 6 8
## 3 1 2 12
## 4 8 4 0
## 5 2 1 2
knitr::include_graphics("C:/Users/Hannah/Desktop/Assignment/ANLY512/Week1 Course Intro/Graph3.JPG")
Summary: First, I computed the frequency of each gear type divided by cyl type. Second, a bar graph was drawn in the same way as the previous graph. Then I drew the three parts of each bar (stacked bar) according to the frequency of cyl type for each gear type.
# 4. Draw a scatter plot showing the relationship between `wt` and `mpg`.
# Check the range for the two variables
range(mtcars$wt)
## [1] 1.513 5.424
range(mtcars$mpg)
## [1] 10.4 33.9
# Use only the two variables
cbind(mtcars$wt, mtcars$mpg)
## [,1] [,2]
## [1,] 2.620 21.0
## [2,] 2.875 21.0
## [3,] 2.320 22.8
## [4,] 3.215 21.4
## [5,] 3.440 18.7
## [6,] 3.460 18.1
## [7,] 3.570 14.3
## [8,] 3.190 24.4
## [9,] 3.150 22.8
## [10,] 3.440 19.2
## [11,] 3.440 17.8
## [12,] 4.070 16.4
## [13,] 3.730 17.3
## [14,] 3.780 15.2
## [15,] 5.250 10.4
## [16,] 5.424 10.4
## [17,] 5.345 14.7
## [18,] 2.200 32.4
## [19,] 1.615 30.4
## [20,] 1.835 33.9
## [21,] 2.465 21.5
## [22,] 3.520 15.5
## [23,] 3.435 15.2
## [24,] 3.840 13.3
## [25,] 3.845 19.2
## [26,] 1.935 27.3
## [27,] 2.140 26.0
## [28,] 1.513 30.4
## [29,] 3.170 15.8
## [30,] 2.770 19.7
## [31,] 3.570 15.0
## [32,] 2.780 21.4
knitr::include_graphics("C:/Users/Hannah/Desktop/Assignment/ANLY512/Week1 Course Intro/Graph4.JPG")
Summary: First, I drew the x-axis based on the range of wt, and y-axis based on the range of mpg. Then for each case in the data set, I found the corresponding point in the graph and marked it.
# 5. Design a visualization of your choice using the data.
# Side-by-side bars to compare the numer of cyl type for cars across different am.
# Cross table for cyl and am
table( mtcars$cyl, mtcars$am)
##
## 0 1
## 4 3 8
## 6 4 3
## 8 12 2
knitr::include_graphics("C:/Users/Hannah/Desktop/Assignment/ANLY512/Week1 Course Intro/Graph5.JPG")
Summary: First, I created a cross table to show the frequency of cyl divided by am. Then for am 0, I set the height of each bar according to the frequency of cyl type. Same for am 1. The bars for each cyl type were grouped together, in order to compare the frequency of cyl when am were different.