When completed, name your final output .html file as: YourName_ANLY512-52-2018.html and upload it to the “Visualization Coding Exercise #7 (Homework #7)” assignment in Week #7 on Moodle.

This assignment is worth 20 points. Each problem is worth 3 points each.

All of these exercises will be using the Vocab data frame that you were introduced to in class this past week. Remember, you have to reference the data frame by carData::Vocab (case sensitive)

Bar Plots with color ramp - Part 1

In these exercises of creating bar plots, you will fill each segment according to an ordinal variable. The best way to do that is with a sequential color series.You’ll be using the carData::Vocab dataset. Since this is a much larger dataset with more categories, you’ll also compare it to a simpler dataset, mtcars. Both datasets have variables that are ordinal.

  1. Perform the following:

You will be using the mtcars data frame.

  1. The bar plot from one of the exercises in Homework #6 is provided - cyl is on the x-axis and filled according to transmission type, am. Variables cyl and am have been converted to categorical variables. The measurment level of these variables is ordinal. Notice how you can set the color palette used to fill the bars with scale_fill_brewer(). For a full list of possible color sets, have a look at ?brewer.pal. Run the code and examine the output.

  2. Explore carData::Vocab with str(). Notice that the education and vocabulary are numeric variables. You will need to convert them to categorical variables to create the plot in part c.

  3. For this plot, make sure education and vocabulary are categorical variables. Make a filled bar chart with the Vocab dataset. -Map education to x and vocabulary to fill. -Inside geom_bar(), make sure to set position = “fill”. -Allow color brewer to choose a default color palette by using the appropriate scale function, without arguments. Notice how this generates a warning message and an incomplete plot.

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
library(car)
## Warning: package 'car' was built under R version 3.4.4
## Loading required package: carData
## Warning: package 'carData' was built under R version 3.4.4
library(carData)

#part a
ggplot(mtcars, aes(x = factor(cyl), fill = factor(am))) +
  geom_bar() +
  scale_fill_brewer(palette = "Set1")

#part b
str(carData::Vocab)
## 'data.frame':    30351 obs. of  4 variables:
##  $ year      : num  1974 1974 1974 1974 1974 ...
##  $ sex       : Factor w/ 2 levels "Female","Male": 2 2 1 1 1 2 2 2 1 1 ...
##  $ education : num  14 16 10 10 12 16 17 10 12 11 ...
##  $ vocabulary: num  9 9 9 5 8 8 9 5 3 5 ...
##  - attr(*, "na.action")=Class 'omit'  Named int [1:32115] 1 2 3 4 5 6 7 8 9 10 ...
##   .. ..- attr(*, "names")= chr [1:32115] "19720001" "19720002" "19720003" "19720004" ...
#part c
# Plot education on x and vocabulary on fill
ggplot(Vocab, aes(x = education, fill = vocabulary)) +
  geom_bar(position = 'fill') + 
  scale_fill_brewer()

##Bar Plots with color ramp - Part 2

In the previous exercises (Part 1), you ended up with an incomplete bar plot. This was because for continuous data, the default RColorBrewer palette that scale_fill_brewer() calls is “Blues”. There are only 9 colours in the palette, and since you have 11 categories, your plot looked strange.

In this exercise, you’ll manually create a color palette that can generate all the colours you need. To do this you’ll use a function called colorRampPalette().

The input is a character vector of 2 or more colour values, e.g. “#FFFFFF” (white) and “#0000FF” (pure blue).

The output is itself a function! So when you assign it to an object, that object should be used as a function. To see what I mean, execute the following three lines in the console:

new_col <- colorRampPalette(c(“#FFFFFF”, “#0000FF”)) new_col(4) # the newly extrapolated colours munsell::plot_hex(new_col(4)) # Quick and dirty plot

new_col() is a function that takes one argument: the number of colours you want to extrapolate. You want to use nicer colours, so I have assigned the entire “blues” color palette from the RColorBrewer package to the character vector blues (in part a below)

  1. Perform the following:

The variables education and vocabulary should be converted to categorical variables.

  1. The last plot that should have been created in exercise 1c is provided for you. Run the code and remind yourself of the output. In addition, a new function entitled “blues” has been created for you as well that has the entire blue color pallette from RColorBrewer.

  2. Like in the example code given in part a, create a new function called blue_range that uses colorRampPalette() to extrapolate over all 9 values of the blues character vector.

  3. Take the plot code from part a (provided), and change scale_fill_brewer() to be scale_fill_manual(). Set the argument values = blue_range(11) inside scale_fill_manual()

library(ggplot2)
library(car)
library(carData)
library(RColorBrewer)

#part a
ggplot(Vocab, aes(x = education, fill = vocabulary)) +
  geom_bar(position = "fill") +
  scale_fill_brewer()

# Definition of a set of blue colors
blues <- brewer.pal(9, "Blues") # from the RColorBrewer package

#part b
#make a color range using colorRampPalette() and the set of blues

blue_range <- colorRampPalette(blues)
munsell::plot_hex(blue_range(11)) 

#part c
#use blue_range to adjust the color of the bars, use scale_fill_manual()
ggplot(Vocab, aes(x = education, fill = vocabulary)) +
  geom_bar(position = "fill") +
  scale_fill_manual(values = blue_range(11))