Student name: Xiaopeng Ruan

Find the mtcars data in R. This is the dataset that you will use to create your graphics.

Dataset description:

Motor Trend Car Road Tests: The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). Format: A data frame with 32 observations on 11 variables.

data("mtcars")
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000
  1. Create a pie chart showing the proportion of cars from the mtcars data set that have different carb values.
counts <- table(mtcars$gear)
x <- c(7,10,3,10,1,1)
label <- c("carb1","carb2","carb3","carb4","carb6","carb8")
piepercent <- round(100*x/sum(x),1)
piepercent <- paste(piepercent, "%", sep = "")
pie(x, labels = piepercent, main = "carb percent pie chart",,col= terrain.colors(length(x)))
legend("topright",label, cex=0.8, fill=terrain.colors(length(x)))

The pie chart is a way to show the composition of a total, From this chart, we can see the percentage breakdown of cars in the mtcars data set based on different number of carburetors. This pie chart constitutes of six parts, Each part displays the number of carburetors and the percentage associated with it is the proportion of actual number of cars in the entire mtcars data set.

  1. Create a bar graph, that shows the number of each gear type in mtcars.
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution", xlab="Number of Gears",col="red")

The bar graph is a way to compare values of different categories, This above bar chart shows the number of gears into its different 3 levels on the x-axis , Also it shows the total number of cars that belong to each level of gears on the y-axis.

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
library(vcd)
## Warning: package 'vcd' was built under R version 3.4.3
## Loading required package: grid
counts <- table(mtcars$cyl,mtcars$gear)
barplot(counts, main = "stacked Bar plot gear&cyl",xlab = "gear", ylab = "Frequency", col = c("red","blue","green"),legend=rownames(counts))

This stacked chart displays the number of cars on the y-axis , and shows the categorization of number of gears on the x-axis. Nevertheless, this chart adds another layer of visualization in each gear group into cars that have different number of cylinders (4, 6 and 8). This is highlighted with different colors to show number of cars with different cylinders in each gear level.

  1. Draw a scatter plot showing the relationship between wt and mpg.
plot(mtcars$wt , mtcars$mpg, xlab = 'Weight of Cars', ylab = 'Miles per Gallon', main = 'Scatter Plot Wt Vs MPG')

Scatter plot is to understand the distribution of the data, This scatter plot provides a visualization for change in the miles per gallon, depicted on the y-axis, as the car weight is changed on the x-axis. This graph illustrates that the mileage (mpg) of a car decreases as the weight of car is increased on an average.

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
boxplot(mpg ~ cyl, data = mtcars, xlab = "Number of cylinders",ylab = "Miles/(US) gallon",main = "Number of cylinders VS Miles/(US) gallon",
pch = 20,cex = 2,border = "red")

Boxplot is a very useful plot for visualization, We can use a boxplot to visualize the relationship between a numerical and categorical variable. Furthermore,a box plot provides a measure of mean of response variable for each level of independent variable on the x-axis. Here mpg is a numerical variable and Number of cylinders is categorical. It shows the min and max ranges of the cylinder’s values for each level, effectively highlighting the outliers at a glance. We see this for cylinder group 8 where one outlier has very low mpg value, which is displayed below the box.We can also infer that there is high variability in the mpg values of cars with 4 cylinders as compared to cars with 6 or 8 cylinders. This can be inferred by the bigger interquartile range for number of cylinders group 4 vs that of group 6 and 8. We can make the box plot more attractive by setting some of its parameters.