library(tidyverse)
## -- Attaching packages -------------------------------------------------------------------------------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.1     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ----------------------------------------------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
source('http://www.openintro.org/stat/data/arbuthnot.R')
#Exercise 1: What command would you use to extract just the counts of girls baptized? Try it!
arbuthnot$girls
##  [1] 4683 4457 4102 4590 4839 4820 4928 4605 4457 4952 4784 5332 5200 4910 4617
## [16] 3997 3919 3395 3536 3181 2746 2722 2840 2908 2959 3179 3349 3382 3289 3013
## [31] 2781 3247 4107 4803 4881 5681 4858 4319 5322 5560 5829 5719 6061 6120 5822
## [46] 5738 5717 5847 6203 6033 6041 6299 6533 6744 7158 7127 7246 7119 7214 7101
## [61] 7167 7302 7392 7316 7483 6647 6713 7229 7767 7626 7452 7061 7514 7656 7683
## [76] 5738 7779 7417 7687 7623 7380 7288
# Exercise 2: Is there an apparent trend in the number of girls baptized over the years? How would you describe it? (To ensure that your lab report is comprehensive, be sure to include the code needed to make the plot as well as your written interpretation.)

#make the ggplot
ggplot(data = arbuthnot, aes(x = year, y = girls)) + geom_line() + ggtitle("Girls baptism rates") + theme(plot.title = element_text(hjust = 0.5))

#Interpretation:  It looks like the # of girls being baptized over the years took a steep decline in the mid 1600's then steadily increased.  overall the trend is upward.
# Exercise 3: Now, generate a plot of the proportion of boys born over time. What do you see?

#make the ggplot
ggplot(data = arbuthnot, aes(x = year, y = boys)) + geom_line() + ggtitle("Boys baptism rates") + theme(plot.title = element_text(hjust = 0.5))

#Interpretation: The line for the boys baptisms looks very similar to the girls line, which makes me think that the dip in the mid 1600's is unrelated to gender.
# Exercise 4: What years are included in this data set?  What are the dimensions of the data frame?  What are the variable (column) names?

#Years: 1940 to 2002
dim(present)
## [1] 63  3
present
## # A tibble: 63 x 3
##     year    boys   girls
##    <dbl>   <dbl>   <dbl>
##  1  1940 1211684 1148715
##  2  1941 1289734 1223693
##  3  1942 1444365 1364631
##  4  1943 1508959 1427901
##  5  1944 1435301 1359499
##  6  1945 1404587 1330869
##  7  1946 1691220 1597452
##  8  1947 1899876 1800064
##  9  1948 1813852 1721216
## 10  1949 1826352 1733177
## # ... with 53 more rows
#Dimensions: 63 rows, 3 columns
#Variable column names: year, boys, girls

Exercise 5: How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?

These counts are drastically bigger than the Arbuthnot data set. they are not similar in magnitude, the present data set is far bigger

# Exercise 6: Make a plot that displays the proportion of boys born over time. What do you see? Does Arbuthnot’s observation about boys being born in greater proportion than girls hold up in the U.S.? Include the plot in your response. Hint: You should be able to reuse your code from Exercise 3 above, just replace the dataframe name.

#make the ggplot
ggplot() + 
  geom_line(data = present, aes(x = year, y = boys), color = "blue") +
  geom_line(data = present, aes(x = year, y = girls), color = "red") +
  xlab('Dates') +
  ylab('percent.change') +
  ggtitle("All birth rates") + theme(plot.title = element_text(hjust = 0.5))

Exercise 6: Yes this shows that more boys are being born more than girls, but the ratio of boys to girls remains constant over time.

# Exercise 7: In what year did we see the most total number of births in the U.S.? Hint: First calculate the totals and save it as a new variable. Then, sort your dataset in descending order based on the total column. You can do this interactively in the data viewer by clicking on the arrows next to the variable names. To include the sorted result in your report you will need to use two new functions: arrange (for sorting). We can arrange the data in a descending order with another function: desc (for descending order). The sample code is provided below.

#add column
present$Total = present$boys + present$girls

library(dplyr)

arrange(present, desc(present$Total)) 
## # A tibble: 63 x 4
##     year    boys   girls   Total
##    <dbl>   <dbl>   <dbl>   <dbl>
##  1  1961 2186274 2082052 4268326
##  2  1960 2179708 2078142 4257850
##  3  1957 2179960 2074824 4254784
##  4  1959 2173638 2071158 4244796
##  5  1958 2152546 2051266 4203812
##  6  1962 2132466 2034896 4167362
##  7  1956 2133588 2029502 4163090
##  8  1990 2129495 2028717 4158212
##  9  1991 2101518 2009389 4110907
## 10  1963 2101632 1996388 4098020
## # ... with 53 more rows

Exercise 7: the year with the most total births was 1961