This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_3.R
179 lines (155 loc) · 6.02 KB
/
exercise_3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
library(readxl)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggfortify)
library(tidyverse)
library(gtsummary)
library(gt)
library(knitr)
library(flextable)
#Load NOAA Climate Data Online data
climate <- read_excel("data/climate.xlsx")
#omit NA in temperature recordings
climate <- climate[complete.cases(climate[,6]),]
#create column for month
climate <- mutate(climate, month=month(climate$date))
#create column for year
climate <- mutate(climate, Year=year(climate$date))
#create column for julian date
climate$julian_date <- yday(climate$date)
#create column with the meanTMAX & SD
climate <- climate %>%
group_by(Year) %>%
mutate(meanTMAX = mean(TMAX)) %>%
mutate(TMAXsd=sd(TMAX))
#Load seedling data
seedlings <- read_excel("data/seedlings.xlsx")
#create column with the mean height
seedlings <- seedlings %>%
group_by(species) %>%
mutate(mean_height = mean(height))
#create dataframe for SD
sd_seedlings <- seedlings %>%
group_by(species) %>%
summarize(mean_height = mean(height),
seedling_sd=sd(height))
LT50 <- read_excel("data/LT50_data.xlsx")
#omit NA in before/after column
LT50 <- LT50[complete.cases(LT50[,8]),]
#create dataframe for SD
LT50_sd <- LT50%>%
group_by(state, species) %>%
dplyr::summarise(meanLT50=mean(LT50),
sdLT50=sd(LT50))
table1 <- climate %>%
select(Year, TMAX) %>%
tbl_summary(by = Year,
missing= "no",
digits = all_continuous() ~1,
label = list(TMAX ~ "Temperature (°C)"),
statistic = list(all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n}")) %>%
add_p(pvalue_fun = ~ style_pvalue(.x, digits = 3)) %>% #number of digits displayed for p-values
modify_caption("Table 1. Mean high temperature for 2022 and 2023.") %>%
modify_footnote(everything() ~ NA) %>%
modify_header(
update = list(
label ~ '',
stat_1 ~ '**2022**', #is markdown **bold** formatting
stat_2 ~ '**2023**',
p.value ~ '**P-value**')) %>%
as_gt() %>%
gt::tab_options(heading.align = "left")
table1
table1_plot <-ggplot(data = climate, (aes(x=Year, y=meanTMAX, group = Year, colour = factor(Year)))) +
geom_point()+
geom_errorbar(aes(x= Year, ymin= meanTMAX-TMAXsd, ymax=meanTMAX+TMAXsd, group=Year, width= 0.5))+
ylab("Temperature (°C)")+
xlab("Year")+
labs(color = "Year")+
scale_x_continuous(breaks = c(2022, 2023))+
scale_fill_discrete(name="Year")+
theme_bw()+
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.title.x = element_blank())+
ggtitle("Maximum Temperatures by Year")
table1_plot
table2 <- seedlings %>%
select(species, height, mean_dia) %>%
tbl_summary(by = species,
missing= "no",
digits = all_continuous() ~1,
label = list(mean_dia ~ "Mean diameter (cm)",
height ~ "height (cm)"),
statistic = list(all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n}")) %>%
add_p(pvalue_fun = ~ style_pvalue(.x, digits = 3)) %>% #number of digits displayed for p-values
modify_caption("Table 2. Mean stem diameter and overall height measurements (cm) of three tree seedling species.") %>%
modify_footnote(everything() ~ NA) %>%
modify_header(
update = list(
label ~ '',
stat_1 ~ '***Acer saccharum***', #is markdown **bold** formatting
stat_2 ~ '***Quercus alba***',
stat_3 ~ '***Quercus coccinea***',
p.value ~ '**P-value**')) %>%
as_gt() %>%
gt::tab_options(heading.align = "left")
table2
table2_plot <- ggplot(data= seedlings, aes(x=species, y= height, group = species))+
geom_boxplot()+
geom_errorbar(aes(x= species, ymin= mean_height-sd_seedlings$seedling_sd, ymax=mean_height+sd_seedlings$seedling_sd, group=species, width= 0.5))+
ylab("Height (cm)")+
xlab("Species")+
theme_bw()+
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.title.x = element_blank())+
ggtitle("Seedling height by species")
table2_plot
### table 3
table3 <- LT50 %>%
select(species, LT50) %>%
tbl_summary(by = species,
missing= "no",
digits = all_continuous() ~1,
label = list(species ~ "Species",
LT50 ~ "Temperature"),
statistic = list(all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n}")) %>%
add_p(pvalue_fun = ~ style_pvalue(.x, digits = 3)) %>% #number of digits displayed for p-values
modify_caption("Table 3. Mean LT50 for three hardwood tree species")%>%
modify_footnote(everything() ~ NA) %>%
modify_header(
update = list(
label ~ '',
stat_1 ~ '***Acer saccharum***',
stat_2 ~ '***Fagus grandifolia***',
stat_3 ~ '***Liriodendron tulipifera***',
p.value ~ '**P-value**')) %>%
as_gt() %>%
gt::tab_options(heading.align = "left")
table3
table3_plot <- ggplot(data= LT50_sd, aes(x=state, y= meanLT50, group = species, color = species))+
geom_violin(trim = FALSE, position = position_dodge(width = 1))+
geom_errorbar(aes(x= state, ymin= meanLT50-LT50_sd$sdLT50, ymax=meanLT50+LT50_sd$sdLT50, group=species, width= 0.5), position = position_dodge(width = 1))+
scale_color_manual(values = c("Acer saccharum" = "red", "Liriodendron tulipifera" = "blue", "Fagus grandifolia" = "black"))+
labs(y=expression("LT"["50"]/"Temperature (°C)"))+
xlab("Species")+
theme_bw()+
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.title.x = element_blank())+
ggtitle("LT50 by state and species")
table3_plot