ggplot(data = df) + geom_density(aes(x = Year, y=Percent, ,stat='identity',fill='blue',alpha=0.2) + facet_wrap(~StateRegion) } ) A function will allow for more customization of this code. Here, a function called BabyNamePlot() is being created. This function will take two parameters, name and firstyear. The default settings for these are Chris and 1910. > #Creating a custom function in R for added flexibility BabyNamePlot<- function(name='Chris', firstyear=1910){ namedata %>% filter(Name==name & Year > firstyear ) %>% group_by(Gender,Year,StateRegion) %>% summarize(Total=sum(Count)) %>% left_join(output2) %>% mutate(Percent=Total/Total2) %>% (function(df){ggplot(data = df) + geom_density(aes(x = Year, y=Percent, color=Gender),stat='identity',fill='blue',alpha=0.2) + facet_wrap(~StateRegion) }) } After successfully creating the BabyNamePlot() function, we can put it to use in the following ways. BabyNamePlot(name="Emma",firstyear=1995) BabyNamePlot(name="Henry",firstyear=2000) Questions: How much regional variation exists in the baby name Emma over the past 20 years? Discuss. The graphic provided at the beginning of this handout suggested that Henry was a popular baby name in the Midwest? Is this support by our analysis? Discuss. Task Complete the following task the baby names dataset. How much regional differences exist in your name? Create a plot using the BabyNamePlot() function to investigate any differences.
ggplot(data = df) + geom_density(aes(x = Year, y=Percent, ,stat='identity',fill='blue',alpha=0.2) + facet_wrap(~StateRegion) } ) A function will allow for more customization of this code. Here, a function called BabyNamePlot() is being created. This function will take two parameters, name and firstyear. The default settings for these are Chris and 1910. > #Creating a custom function in R for added flexibility BabyNamePlot<- function(name='Chris', firstyear=1910){ namedata %>% filter(Name==name & Year > firstyear ) %>% group_by(Gender,Year,StateRegion) %>% summarize(Total=sum(Count)) %>% left_join(output2) %>% mutate(Percent=Total/Total2) %>% (function(df){ggplot(data = df) + geom_density(aes(x = Year, y=Percent, color=Gender),stat='identity',fill='blue',alpha=0.2) + facet_wrap(~StateRegion) }) } After successfully creating the BabyNamePlot() function, we can put it to use in the following ways. BabyNamePlot(name="Emma",firstyear=1995) BabyNamePlot(name="Henry",firstyear=2000) Questions: How much regional variation exists in the baby name Emma over the past 20 years? Discuss. The graphic provided at the beginning of this handout suggested that Henry was a popular baby name in the Midwest? Is this support by our analysis? Discuss. Task Complete the following task the baby names dataset. How much regional differences exist in your name? Create a plot using the BabyNamePlot() function to investigate any differences.
R_Handout3_SP18_Updated.docx