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.
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")
ggvis_percent(addhealth, 'age') %>% layer_bars() %>% add_axis("x", title = "Age", title_offset="50") %>% add_axis("y", title = "Percent", title_offset="50")
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")
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.
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.