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

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

First let's make a histogram of age in the addhealth data set. This show the number of observations with each age. The age variable is age

ggplot(addhealth, aes( x=age)) + 
  geom_histogram() 

ggplot(addhealth, aes(age)) + 
  geom_histogram(binwidth = 1, aes(  y=(..count..)/sum(..count..))) + 
  labs(y="Percent",     x="Age")

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.

Notice that this is using the count of age

However there is much more we can do.

ggplot(addhealth, aes( x=age)) + 
  geom_histogram() +

  labs(y="Count", x="Age") 

Add a little space between the labels and the axes

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)

 ggplot(addhealth, aes( x=age)) + 
  geom_histogram(fill="pink", color="blue") +

  labs(y="Count", x="Age") 

You can change the location and widths of the bars

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 %>%)

addhealth %>% 
      ggvis(~age) %>% 
      layer_histograms(fill := "pink", stroke :="blue", width = 1, center = .5)    %>%
      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.