Directions

During ANLY 512 we will be studying the theory and practice of data visualization. We will be using R and the packages within R to assemble data and construct many different types of visualizations. We begin by studying some of the theoretical aspects of visualization. To do that we must appreciate the basic steps in the process of making a visualization.

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.

The final product of your homework (this file) should include a short summary of each graphic.

To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Moodle. Please make sure that this link is hyperlinked and that I can see the visualization and the code required to create it.

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
attach(mtcars)
View(mtcars)
library(MASS)

carb=mtcars$carb
summary(carb)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   2.000   2.000   2.812   4.000   8.000
carb.freq=table(carb)

#pie chart to show the proportion of different carb 
pie(carb.freq,
    main="Proportion of carbs")

#calculate the percentage of each type
percent<-round(carb/sum(carb)*100)
pie_labels<-paste(percent,"%",sep="")

#pie chart shows the percentage of each proportion
pie(carb.freq,
    main="Proportion of carbs",
    labels=pie_labels)

  1. Create a bar graph, that shows the number of each gear type in mtcars.
# place the code to import graphics here

library(MASS)
gear=mtcars$gear
gear.freq=table(gear)

#creat a bar chart to show the number of gear type
barplot(gear.freq,
        main="Gear types")

  1. Next show a stacked bar graph of the number of each gear type and how they are further divided out by cyl.
# place the code to import graphics here

#create data frame
cyl=mtcars$cyl
gear_cyl_table=table(cyl,gear)
barplot(gear_cyl_table,
        main="Car Distribution by gears and cyl",
        xlab="Number of gears",
        ylab="cylinders types",
        legend=rownames(gear_cyl_table))

  1. Draw a scatter plot showing the relationship between wt and mpg.
# place the code to import graphics here
wt=mtcars$wt
mpg=mtcars$mpg

#Scatter plot 
plot(mpg,wt,
     main="Relationship between Car wt and mpg",
     xlab="Miles per Gallen", ylab="Car Weight",pch=19)

  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 of mpg by Car Cylindars
boxplot(mpg~cyl, data=mtcars, main="Car Milage", xlab="#of Cylinders", ylab="Miles per Gallen",varwidth=TRUE)

#I creat this Boxplot to shows the relationship betwen Cylinders and Miles per Gallen.