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:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

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

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.
# place the code to import graphics here
mtcarscarb = table(mtcars$carb)
percentlabels<- round(100*mtcarscarb/sum(mtcarscarb), 1)
pielabels<- paste(percentlabels, "%", sep="")
pie(mtcarscarb,col = rainbow(length(mtcarscarb)), labels = pielabels , main = 'MTCars - Carb Pie Chart', cex = 0.8)
legend("topright", c("Carb-1","Carb-2","Carb-3","Carb-4","Carb-6","Carb-8"), cex=0.6, fill=  rainbow(length(mtcarscarb)))

  1. Create a bar graph, that shows the number of each gear type in mtcars.
# place the code to import graphics here
mtcarsgear= table(mtcars$gear)
barplot(mtcarsgear, main = 'Bar Chart for MTCars Gear Types', col = c("red", "blue", "green"), ylab = 'Frequency' ,xlab = "Gear Type",  ylim = c(0,20))

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
gearcyl <- table(mtcars$cyl, mtcars$gear)
barplot(gearcyl, main = "Bar Graph: MTCars distribution by Gears and Cyl", xlab = "Number of Gears",ylab= "Frequency", col = c("orange", "blue", "red"), ylim = c(0,20),legend = paste("cyl =",rownames(gearcyl)))

  1. Draw a scatter plot showing the relationship between wt and mpg.
plot(mtcars$wt, mtcars$mpg, main="Weight - Mpg",     xlab="Car Wt ", ylab="Miles/Gallon ", pch=18)
abline(lm(mtcars$mpg~mtcars$wt), col="blue")

  1. Design a visualization of your choice using the data and write a brief summary about why you chose that visualization.
# place the code to import graphics here

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,
col = "lightgreen",
border = "red")

#I was interested in boxplot visualization to find the relationship between a numerical variable mpg and categorical variable Number of cylinders.Example,for 4 cylinders boxlplot visualization shows that max gallon value is 40, upper quartile range is 30, median = 26, lower quartile = 24, minimum = 22