Load packages

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0     ✔ purrr   0.3.5
## ✔ tibble  3.1.8     ✔ stringr 1.5.0
## ✔ tidyr   1.2.1     ✔ forcats 0.5.2
## ✔ readr   2.1.3     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(ggplot2)
library(tidyr)
library(patchwork)

Load in previous data from assignment 5

part1 <- read_csv("https://raw.githubusercontent.com/mbtoomey/Biol_7263/main/Data/assignment6part1.csv")
## Rows: 2 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): ID
## dbl (20): Sample1_Male_Control, Sample2_Male_Control, Sample3_Male_Control, ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
part2 <- read_csv("https://raw.githubusercontent.com/mbtoomey/Biol_7263/main/Data/assignment6part2.csv")
## Rows: 1 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): ID
## dbl (16): Sample16.Treatment, Sample12.Control, Sample3.Control, Sample6.Tre...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
part1NEW <- part1  %>% pivot_longer(cols = "Sample1_Male_Control":"Sample20_Female_Treatment", names_to = c("Sample#", "Sex", "TreatmentType"), names_sep = "_", values_drop_na = TRUE) %>% pivot_wider(names_from = ID, values_from = value)

part2NEW <- part2 %>% pivot_longer(cols = "Sample16.Treatment":"Sample13.Control", names_to = c("Sample#", "TreatmentType"), names_sep = "\\.", values_drop_na = TRUE) %>% pivot_wider(names_from = ID, values_from = value)

Finaldoc <- part1NEW %>% full_join(part2NEW, by = c("Sample#", "TreatmentType"))

#1. Using my ebird dataset

MBT_ebird<- read_csv("https://github.com/mbtoomey/Biol_7263/blob/main/Data/MBT_ebird.csv?raw=true")
## New names:
## Rows: 6595 Columns: 14
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (4): list_ID, common_name, scientific_name, location dbl (8): ...1, count,
## duration, latitude, longitude, count_tot, month, year date (1): date time (1):
## time
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`

First calculate the total number of species seen each month of each year in each location Then plot the number of species seen each month with the color of the points indicating year and facet this plot by location

sorted_ebird <- MBT_ebird %>% group_by(location, year, month) %>% summarize(n_distinct(common_name), .groups = "drop")
species <- sorted_ebird$`n_distinct(common_name)`
ggplot(sorted_ebird, aes(x=month, y=species, color=year)) + geom_point() + facet_wrap(~location)

#2. Plot a comparison of mass by treatment including the individual observations, the mean, and standard error of the mean. Use point color or shape to indicate the sex.

ggplot(Finaldoc, aes(x=Finaldoc$TreatmentType, y=Finaldoc$mass, color = Sex)) + geom_point() + stat_summary(geom = "point", fun.y = "mean", col = "black", size = 3, shape = 24, fill = "purple") +  stat_summary(geom = "errorbar", width = 0.3)
## Warning: The `fun.y` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
## ℹ Please use the `fun` argument instead.
## Warning: Use of `Finaldoc$TreatmentType` is discouraged.
## ℹ Use `TreatmentType` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Use of `Finaldoc$TreatmentType` is discouraged.
## ℹ Use `TreatmentType` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Use of `Finaldoc$TreatmentType` is discouraged.
## ℹ Use `TreatmentType` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Removed 4 rows containing non-finite values (`stat_summary()`).
## Removed 4 rows containing non-finite values (`stat_summary()`).
## No summary function supplied, defaulting to `mean_se()`
## Warning: Removed 4 rows containing missing values (`geom_point()`).

#3. Generate a scatter plot of age and mass, indicate treatment with point shape or color, and fit separate regression lines (without CI) to each treatment.

ggplot(Finaldoc, aes(x=Finaldoc$age, y=Finaldoc$mass, color = TreatmentType)) + geom_point() + stat_summary(geom = "point", fun.y = "mean", col = "black", size = 3, shape = 24, fill = "purple") + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
## Warning: Use of `Finaldoc$age` is discouraged.
## ℹ Use `age` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Use of `Finaldoc$age` is discouraged.
## ℹ Use `age` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Use of `Finaldoc$age` is discouraged.
## ℹ Use `age` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Removed 4 rows containing non-finite values (`stat_summary()`).
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 4 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 4 rows containing missing values (`geom_point()`).

#4. Combine the plots from 2 and 3 using patchwork tag each panel with and number or letter and include a title for the overall plot.

p1 <- ggplot(Finaldoc, aes(x=Finaldoc$TreatmentType, y=Finaldoc$mass, color = Sex)) + geom_point() + stat_summary(geom = "point", fun = "mean", col = "black", size = 3, shape = 24, fill = "purple") +  stat_summary(geom = "errorbar", width = 0.3)

p2 <- ggplot(Finaldoc, aes(x=Finaldoc$age, y=Finaldoc$mass, color = TreatmentType)) + geom_point() + stat_summary(geom = "point", fun = "mean", col = "black", size = 3, shape = 24, fill = "purple") + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)


p1 + p2 + plot_annotation(title = "mass by age and treatment type", tag_levels = "1")
## Warning: Use of `Finaldoc$TreatmentType` is discouraged.
## ℹ Use `TreatmentType` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Use of `Finaldoc$TreatmentType` is discouraged.
## ℹ Use `TreatmentType` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Use of `Finaldoc$TreatmentType` is discouraged.
## ℹ Use `TreatmentType` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Removed 4 rows containing non-finite values (`stat_summary()`).
## Removed 4 rows containing non-finite values (`stat_summary()`).
## No summary function supplied, defaulting to `mean_se()`
## Warning: Removed 4 rows containing missing values (`geom_point()`).
## Warning: Use of `Finaldoc$age` is discouraged.
## ℹ Use `age` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Use of `Finaldoc$age` is discouraged.
## ℹ Use `age` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Use of `Finaldoc$age` is discouraged.
## ℹ Use `age` instead.
## Warning: Use of `Finaldoc$mass` is discouraged.
## ℹ Use `mass` instead.
## Warning: Removed 4 rows containing non-finite values (`stat_summary()`).
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 4 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 4 rows containing missing values (`geom_point()`).