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")
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 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.
Type your answer below:
Type answer below:
The variable name for grade is grade.
Make a histogram for grade.
Choose the options that you think make sense and look good.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.