library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.1
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.4 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## Warning: package 'tibble' was built under R version 4.1.1
## Warning: package 'readr' was built under R version 4.1.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(openintro)
## Warning: package 'openintro' was built under R version 4.1.1
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
data('arbuthnot', package='openintro')
glimpse(arbuthnot)
## Rows: 82
## Columns: 3
## $ year <int> 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639~
## $ boys <int> 5218, 4858, 4422, 4994, 5158, 5035, 5106, 4917, 4703, 5359, 5366~
## $ girls <int> 4683, 4457, 4102, 4590, 4839, 4820, 4928, 4605, 4457, 4952, 4784~
arbuthnot$boys
## [1] 5218 4858 4422 4994 5158 5035 5106 4917 4703 5359 5366 5518 5470 5460 4793
## [16] 4107 4047 3768 3796 3363 3079 2890 3231 3220 3196 3441 3655 3668 3396 3157
## [31] 3209 3724 4748 5216 5411 6041 5114 4678 5616 6073 6506 6278 6449 6443 6073
## [46] 6113 6058 6552 6423 6568 6247 6548 6822 6909 7577 7575 7484 7575 7737 7487
## [61] 7604 7909 7662 7602 7676 6985 7263 7632 8062 8426 7911 7578 8102 8031 7765
## [76] 6113 8366 7952 8379 8239 7840 7640
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
R has some powerful functions for making graphics. We can create a simple plot of the number of girls baptized per year with the command.
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_line()
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.)
There is a pattern of undulating trend in the number of girls baptized over the years. However, there is more of the upward rise or trend than the downward. Over the years, the number of girls baptized increase because there is more of upward trend than downward.
We’ll be using this new vector to generate some plots, so we’ll want to save it as a permanent column in our data frame.
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
arbuthnot <- arbuthnot %>%
mutate(boy_ratio = boys / total)
Now, generate a plot of the proportion of boys born over time. What do you see?
ggplot(data = arbuthnot, aes(x = year, y = boy_ratio)) + geom_point() +
geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
From the plot, and checking the summary of the data, it can be seen that the proportion of boys born over time is roughly between 0.51 and 0.53
data('present', package='openintro')
What years are included in this data set? What are the dimensions of the data frame? What are the variable (column) names?
summary(present)
## year boys girls
## Min. :1940 Min. :1211684 Min. :1148715
## 1st Qu.:1956 1st Qu.:1799857 1st Qu.:1711405
## Median :1971 Median :1924868 Median :1831679
## Mean :1971 Mean :1885600 Mean :1793915
## 3rd Qu.:1986 3rd Qu.:2058524 3rd Qu.:1965538
## Max. :2002 Max. :2186274 Max. :2082052
dim(present)
## [1] 63 3
colnames(present)
## [1] "year" "boys" "girls"
The years included in this data set are from: 1940 to 2002.
The dimension of the data set is 63 x 3
The Column names are “year”, “boys”, “girls”
How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?
dim(arbuthnot)
## [1] 82 5
dim(present)
## [1] 63 3
summary(arbuthnot)
## year boys girls total boy_ratio
## Min. :1629 Min. :2890 Min. :2722 Min. : 5612 Min. :0.5027
## 1st Qu.:1649 1st Qu.:4759 1st Qu.:4457 1st Qu.: 9199 1st Qu.:0.5118
## Median :1670 Median :6073 Median :5718 Median :11813 Median :0.5157
## Mean :1670 Mean :5907 Mean :5535 Mean :11442 Mean :0.5170
## 3rd Qu.:1690 3rd Qu.:7576 3rd Qu.:7150 3rd Qu.:14723 3rd Qu.:0.5210
## Max. :1710 Max. :8426 Max. :7779 Max. :16145 Max. :0.5362
The count of “arbuthnot” dimension 82 x 3 while the count of ’“present” is 63 x 3 dimension. They are not of similar magnitude because the magnitude of “present” is much bigger(millions) than the magnitude of “arbuthnot” which is in thousands.
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.
present <- present %>%
mutate(total = boys + girls)
present <- present %>%
mutate(boy_ratio = boys / total)
ggplot(data = present, aes(x = year, y = boy_ratio)) + geom_point() +
geom_line()
From the plot, it can be seen that the proportion of boy birth is roughly 0.51 which means that the arbuthnot’s data holds closely in the U.S.
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.
present %>%
arrange(desc(total))
## # A tibble: 63 x 5
## year boys girls total boy_ratio
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1961 2186274 2082052 4268326 0.512
## 2 1960 2179708 2078142 4257850 0.512
## 3 1957 2179960 2074824 4254784 0.512
## 4 1959 2173638 2071158 4244796 0.512
## 5 1958 2152546 2051266 4203812 0.512
## 6 1962 2132466 2034896 4167362 0.512
## 7 1956 2133588 2029502 4163090 0.513
## 8 1990 2129495 2028717 4158212 0.512
## 9 1991 2101518 2009389 4110907 0.511
## 10 1963 2101632 1996388 4098020 0.513
## # ... with 53 more rows
The year with the most total number of births in the U.S. is 1961