In this exercise we weill use the Add Health data to try some graphing.

# This first part loads the programs we need.
library("ggvis")
library('lehmansociology')

First let's make a histogram of age in the addhealth data set.

Notice, we start with the name of the data set, then add information about what we want.

In this case we want one variable, age. This variable comes from the dataset addhealth.

We want percents, so we will use a shortcut.
Notice that the name of the variable is in quotation marks but the name of the dataset is not.

ggvis_percent(addhealth, 'age') %>% layer_bars()

However there is much more we can do.

You can add nicer labels

Put these next %>% add_axis("x", title = "Age") %>% add_axis("y", title = "Percent")

 ggvis_percent(addhealth, 'age') %>% 
  layer_bars()  %>% 
  add_axis("x", title = "Age") %>% 
  add_axis("y", title = "Percent") 

Add a little space between the labels and the axes

 ggvis_percent(addhealth, 'age') %>% 
  layer_bars()  %>%
   add_axis("x", title = "Age", title_offset="50") %>% 
   add_axis("y", title = "Percent", title_offset="50") 

You can add options inside the layer_histogram parentheses

You can change the colors of the bars

stroke := (This is the outline. Put the name of a color or number of a color in quotation marks) fill := (This is what goes in the middle)

 ggvis_percent(addhealth, 'age') %>% 
  layer_bars(fill := "pink", stroke="blue")

You can change the location and widths of the bars

width (changes how wide the bars are, based on the values of your variable) center (usually 0 or .5)

  ggvis_percent(addhealth, 'age') %>% 
    layer_bars(width=5)
  ggvis_percent(addhealth, 'age') %>% 
    layer_bars(width=1)

You can combine these together. (You can break up the lines, makes sure to put the %>% at the end of each line. Never start a line with %>%)

ggvis_percent(addhealth, 'age') %>% 
      layer_bars(fill := "pink", stroke :="blue", width = 1)    %>%
      add_axis("x", title = "Age", title_offset = "50", properties = axis_props(labels = list(fill = "blue"))) %>% 
      add_axis("y", title = "Count", title_offset = "60") 

You can get more ideas for making changes of the axes on this page: http://ggvis.rstudio.com/axes-legends.html

Below make 5 different versions of the histogram for age.


Which do you think is the best one? Why?

Type your answer below:

Look at the histogram you picked. What does the distribution of age look like in your histogram?

Type answer below:

Make a new histogram

The variable name for grade is grade.

Make a histogram for grade.

Choose the options that you think make sense and look good.


Look at your histogram. What does the distribution of grade look like in your histogram?






lehmansociology/lehmansociology documentation built on May 21, 2022, 9:06 p.m.