R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Objective: 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. ## 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. #Q1 Summary:
    Based on pie chart, we find that the cars with carb 4 and 2 and 1 are the two biggest prpportion in this dataset while 3, 6 and 8 are just small part of the dataset.
pie(table(mtcars$carb),main='Pie Chart of carb')

  1. Create a bar graph, that shows the number of each gear type in mtcarsand write a brief summary. #Q2 Summary: Most of the cars are using number 3 and number 4 gear. The proportion of number 5 gear are relatively small in this dataset.
barplot(table(mtcars$gear),main="Gear Distribution",xlab='Number of Gears')

  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. #Q3 Summary: From the graph, we can tell that most of cars are with cyl 8 under gear3 , while most of them are cyl4 under gear4 cars. But there is no a clear relationship between cyl and gear5 under gear5 cars. As for assumption, if we randomly pick a 8cyl car, it is highly possible that the car is 3 gear. If we randomly pick a 4cyl car, it is highly possible that the car is 4 gear.
Counts <- table(mtcars$cyl, mtcars$gear)
barplot(Counts,main='Car Distribution by Gears and Cyl',xlab='Number of Gears', col=c("blue","red",'green'),legend = rownames(Counts))

  1. Draw a scatter plot showing the relationship between wt and mpgand write a brief summary.

#Q4 Sumary: It is clear that there is a negative relationship between car weight and mpg. For example, when the weight of a car increases, the mpg of this car will decrease.

plot(mtcars$wt, mtcars$mpg, main="Scatterplot of wt and mpg", 
    xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.

#Q5Summary: We design this visualization in order to understand what type of the car is most energy efficient. Based on finding in question 4, the first factor is car weight. A lighter car has a higher mpg. Second, based on the boxplot, cyl4 cars have the highest average mpg but the variance is a little bit high. Third, in terms of gear, gear4 cars have the best average mpg. Last, both card1 and carb2 cars have a good mpg. In conclusion, i will suggest people who cares about mpg that considering a low carbs, low number of gears, cyl4 and a light wright car.

boxplot(mpg~cyl,data=mtcars, main="MPG average and variance by Cyl", 
    xlab="Number of Cylinders", ylab="Miles Per Gallon")

boxplot(mpg~gear,data=mtcars, main="MPG average and variance by Gear", 
    xlab="Number of Gear", ylab="Miles Per Gallon")

boxplot(mpg~carb,data=mtcars, main="MPG average and variance by Carb", 
    xlab="Number of Carb", ylab="Miles Per Gallon")