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 actual steps in the process of making a visualization.

Most of us use software to do this and have done so for so long that we have lost an appreciation for the mechanistic steps involved in accurately graphing data. We will fix that this week by creating a series of analog (meaning you draw them by hand) graphics. The visualizations you create must be numerically and visually accurate and precisely scaled. Because of that the data sets we visualize will be small.

The final product of your homework (this file) should include scanned or photographed images for each question below and a short summary of the process.

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 your will submit the link to that document on Moodle.

Questions

Find the mtcars data in R. This is the dataset that you will use to create your graphics. Use that data to draw by hand graphics for the next 4 questions.

  1. Draw a pie chart showing the proportion of cars from the mtcars data set that have different carb values.
data("mtcars")
carb_values = table(mtcars$carb)
pct <- round(carb_values/sum(carb_values)*100)
lbls <- c("1","2","3","4","6","8")
lbls1 <- paste(pct,"%",sep="") # ad % to labels
pct
## 
##  1  2  3  4  6  8 
## 22 31  9 31  3  3
# calculate degrees for pie chart
degrees= (carb_values*360)/sum(carb_values)
degrees
## 
##      1      2      3      4      6      8 
##  78.75 112.50  33.75 112.50  11.25  11.25
knitr::include_graphics("/Users/parmars/Downloads/1.jpg")

#pie(carb_values,labels = lbls1, col=rainbow(length(lbls)), main="Proportions of cars by carb value")
#legend("topright", c("1","2","3","4","6","8"), cex = 0.8,
  #     fill = rainbow(length(pct)))
  1. Draw a bar graph, that shows the number of each gear type in mtcars.
gear_data <- table (mtcars$gear)
gear_data
## 
##  3  4  5 
## 15 12  5
knitr::include_graphics("/Users/parmars/Downloads/2.jpg")

#barplot(gear_data, main="Gear in MTCARS", xlab="GEAR TYPE", ylab="Count of Each Gear Value")
  1. Next show a stacked bar graph of the number of each gear type and how they are further divded out by cyl.
gear_data <- table (mtcars$gear,mtcars$cyl)
gear_data
##    
##      4  6  8
##   3  1  2 12
##   4  8  4  0
##   5  2  1  2
gear_data_2 <- rbind(as.numeric(names(gear_data)), gear_data)
names(dimnames(gear_data_2)) <- c("gear", "cyl")
gear_data_2
##     cyl
## gear 4 6  8
##    3 1 2 12
##    4 8 4  0
##    5 2 1  2
knitr::include_graphics("/Users/parmars/Downloads/3.jpg")

  1. Draw a scatter plot showing the relationship between wt and mpg.
car_wt_mpg_info <- subset(mtcars, select=c("wt", "mpg"))
car_wt_mpg_info
##                        wt  mpg
## Mazda RX4           2.620 21.0
## Mazda RX4 Wag       2.875 21.0
## Datsun 710          2.320 22.8
## Hornet 4 Drive      3.215 21.4
## Hornet Sportabout   3.440 18.7
## Valiant             3.460 18.1
## Duster 360          3.570 14.3
## Merc 240D           3.190 24.4
## Merc 230            3.150 22.8
## Merc 280            3.440 19.2
## Merc 280C           3.440 17.8
## Merc 450SE          4.070 16.4
## Merc 450SL          3.730 17.3
## Merc 450SLC         3.780 15.2
## Cadillac Fleetwood  5.250 10.4
## Lincoln Continental 5.424 10.4
## Chrysler Imperial   5.345 14.7
## Fiat 128            2.200 32.4
## Honda Civic         1.615 30.4
## Toyota Corolla      1.835 33.9
## Toyota Corona       2.465 21.5
## Dodge Challenger    3.520 15.5
## AMC Javelin         3.435 15.2
## Camaro Z28          3.840 13.3
## Pontiac Firebird    3.845 19.2
## Fiat X1-9           1.935 27.3
## Porsche 914-2       2.140 26.0
## Lotus Europa        1.513 30.4
## Ford Pantera L      3.170 15.8
## Ferrari Dino        2.770 19.7
## Maserati Bora       3.570 15.0
## Volvo 142E          2.780 21.4
knitr::include_graphics("/Users/parmars/Downloads/4.jpg")

  1. Design a visualization of your choice using the data.
# make a list of mpg and hp which have automatic transmission
car_mpg_hp_automatic <- subset(subset(mtcars, select=c("mpg", "hp", "am")), mtcars$am==1)
# make a list of mpg and hp which have manual transmission
car_mpg_hp_manual <- subset(subset(mtcars, select=c("mpg", "hp", "am")), mtcars$am==0)
knitr::include_graphics("/Users/parmars/Downloads/5.jpg")

#plot(mtcars$hp, mtcars$mpg, pch = mtcars$am, xlab = "horsepower"    cex = 1.2, ylab = "miles per gallon", main = "mpg vs. hp by transmission")
#legend("topright", c("automatic", "manual"), pch = c(0,1))