-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPasteurCanteenDessertsAnalysis.Rmd
260 lines (207 loc) · 8.31 KB
/
PasteurCanteenDessertsAnalysis.Rmd
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
---
author: "Rebecca Stevick"
date: "2/21/2022"
title: "Sweeter than Science:"
subtitle: "A Survey of Canteen Desserts at Institut Pasteur"
output:
html_document:
toc: true
toc_float:
collapsed: false
smooth_scroll: false
github_document:
toc: true
---
# Setup and Oragnize
## Load libraries and settings
```{r setup, warning=FALSE, message=FALSE}
# load libraries
library(tidyverse)
library(nationalparkcolors)
library(ggtext)
library(readxl)
# global theme
theme_set(theme_minimal()+
theme(text=element_text(family="Avenir"),
panel.grid = element_line(color="white")))
# rmd settings
knitr::opts_chunk$set( warning=FALSE, message=FALSE)
```
## Import data
```{r import}
dessertsdata <- read_excel("PasteurCanteenDesserts.xlsx")
# check data
dim(dessertsdata)
str(dessertsdata)
colnames(dessertsdata)
```
## Organize data for phylogeny & PCA
```{r phylog}
# make a matrix
subset <- dessertsdata %>% select(DessertName, DessertType, Nuts, Fruit:Animation) %>%
separate_rows(Fruit, sep=", ")
# convert to binary
binarytable <- subset %>%
pivot_wider(names_from = "Fruit",
values_from = 'Fruit',
values_fill = 0,
values_fn = function(x) 1) %>%
rename("No.Fruit"="none") %>%
pivot_wider(names_from = "DessertType",
values_from = 'DessertType',
values_fill = 0,
values_fn = function(x) 1) %>%
mutate(Nuts=recode(Nuts, "Yes"=1,"No"=0),
Chocolate=recode(Nuts, "Yes"=1,"No"=0),
Chantilly=recode(Nuts, "Yes"=1,"No"=0),
Animation=recode(Nuts, "Yes"=1,"No"=0)) %>%
rename_all(make.names)
# without fruits as individual categories
binary2 <- dessertsdata %>% select(DessertName, DessertType, Nuts, Fruit:Animation) %>%
mutate(Fruit = case_when(Fruit == "none" ~ 0,
TRUE ~1)) %>%
pivot_wider(names_from = "DessertType",
values_from = 'DessertType',
values_fill = 0,
values_fn = function(x) 1) %>%
mutate(Nuts=recode(Nuts, "Yes"=1,"No"=0),
Chocolate=recode(Nuts, "Yes"=1,"No"=0),
Chantilly=recode(Nuts, "Yes"=1,"No"=0),
Animation=recode(Nuts, "Yes"=1,"No"=0))
#dessertsdata %>% write_csv("AllDesserts.csv")
#write_csv(binarytable, "binaryDesserts.csv")
```
# PCA plot
```{r pca, results=FALSE, fig.show='hide'}
library(vegan)
library(gridExtra)
library(ggfortify)
table <- binarytable %>% column_to_rownames("DessertName")
pca_result <- prcomp(table)
pca_result$rotation <- -pca_result$rotation # rotate PC axes
head(pca_result$rotation) # check on variable
pca_result$x <- - pca_result$x # rotate x-axis
autoplot(pca_result) # plot basic result
# generate basic plot
aplot<-autoplot(pca_result,
size=7, alpha=0.8, # size of points
data=dessertsdata, # identify metadata to use for plotting
fill="DessertType",shape="DessertType", # change point fill and shape by site
loadings=TRUE, # include arrows
loadings.label=TRUE, # include arrow labels
loadings.label.fontface="bold",
loadings.label.family="Avenir",
loadings.label.repel=TRUE, # don't overlap arrow labels
loadings.label.size=4,
loadings.label.colour="black",
loadings.colour=c("grey45"))
```
```{r pcaplot}
aplot +
scale_fill_manual(values=c("#EC8FA3", "tan2", "bisque3", "#8D7F99", "#8C9D57", "#163343","cadetblue3"))+
scale_shape_manual(values = c(21,22,23,24,25, 21,22))+
labs(fill = "Dessert Type", shape="Dessert Type")+
theme(legend.position = "bottom")
#ggsave("Figures/DessertsPCA.svg", bg="white", width=6, height=7)
```
# Overall Plots
## Bar plot per type
```{r overallplots}
# bar chart
dessertsdata %>%
group_by(DessertType) %>% count() %>%
ggplot(aes(y=reorder(DessertType,n), x=n, fill=DessertType))+
geom_col()+
geom_text(aes(label=DessertType, x=n-2.7), color="white", family="Avenir", hjust=0)+
scale_fill_manual(values=c("#EC8FA3", "tan2", "bisque3", "#8D7F99", "#8C9D57", "#163343","cadetblue3"))+
theme(legend.position = "none", axis.text.y = element_blank(),
panel.grid.major.y = element_blank())+
labs(x="Number of desserts", y=NULL)
#ggsave("Figures/dessertbargraph.svg",bg="transparent", width=6, height=3.5)
```
## Pie chart per type
```{r piechart}
# pie chart
dessertsdata %>%
group_by(DessertType) %>% count() %>%
arrange(desc(DessertType)) %>%
ungroup() %>%
mutate(prop = n / sum(n) *100) %>%
mutate(ypos = cumsum(prop)- 0.55*prop) %>%
ggplot(aes(x="", y=prop, fill=DessertType)) +
geom_bar(stat="identity", width=1, color="white") +
coord_polar("y", start=0) +
theme_void() +
theme(text=element_text(), legend.position="none") +
geom_text(aes(y = ypos, label = DessertType), color = "white", size=5, family="Avenir") +
scale_fill_manual(values=c("#EC8FA3", "tan2", "bisque3", "#8D7F99", "#8C9D57", "#163343","cadetblue3"))
#ggsave("Figures/dessertspiechart.svg",bg="transparent", width=6, height=5)
```
## Bar plot per fruit
```{r fruit}
dessertsdata %>%
separate_rows(Fruit, sep=", ") %>%
mutate(Fruit = case_when(Fruit == "none" ~ "No Fruit",
TRUE ~ Fruit)) %>%
group_by(Fruit, DessertType) %>% count() %>%
group_by(DessertType) %>% mutate(prct=n/sum(n)) %>%
ggplot(aes(x=DessertType, y=prct, fill=reorder(Fruit,prct)))+
geom_col()+
# geom_text(aes(label=Fruit))+
scale_y_continuous(labels=scales::label_percent())+
scale_fill_manual(values=c("#8bade1", "#9cb19a", "#cc9ebc", "#b0ae7b",
"#96b0b8", "#61bab9", "#e49f90", "#dea586",
"#d7b484", "#74c4e9", "#b2c58f", "#faaeb9",
"#ccbcf3", "#7adbf0", "#ddcf97", "#ffc4ec",
"#b6e4b5", "#dce0ff", "#ffd8ed", "#98f9f9",
"#ffefcf", "#fbf8bd", "#d5ffe6", "#dbffff",
"#f8ffff","grey30"))+
labs(x=NULL, y="Percent of fruit per dessert type", fill=NULL)+
theme(legend.position = "bottom")
#ggsave("Figures/fruitspercent.svg", bg="transparent", width=5.5, height=4.5)
dessertsdata %>%
mutate(FruitYesNo = case_when(Fruit == "none" ~ "No",
TRUE ~ "Yes")) %>%
ggplot(aes(x=DessertType, fill=FruitYesNo))+
geom_bar(position="fill")+
scale_y_continuous(labels=scales::label_percent())+
scale_fill_manual(values=c("black", "rosybrown2"))+
labs(x=NULL, y="Percent of desert type with fruit", fill=NULL)
```
# Chocolate and Chantilly plots
## Chocolate
```{r chocolate}
dessertsdata %>%
ggplot(aes(x=DessertType, fill=Chocolate))+
scale_y_continuous(labels=scales::label_percent())+
scale_fill_manual(values=c("grey80", "brown"))+
geom_bar(position="fill")+
theme(legend.position=c(0.83, 0.8), legend.background = element_rect(fill="white", color="transparent"))+
labs(x=NULL, y="Percent of desert type with chocolate", fill=NULL)
#ggsave("Figures/chocolatechart.svg",bg="transparent", width=4.5, height=3)
```
## Chantilly
```{r chantilly, echo=FALSE}
dessertsdata %>% drop_na(Chantilly) %>%
ggplot(aes(x=DessertType, fill=Chantilly))+
geom_bar(position="fill")+
scale_fill_manual(values=c("grey80", "beige"))+
geom_bar(position="fill")+
scale_y_continuous(labels=scales::label_percent())+
theme(legend.position=c(0.83, 0.8), legend.background = element_rect(fill="white", color="transparent"))+
labs(x=NULL, y="Percent of desert type with chantilly", fill=NULL)
#ggsave("Figures/chantillychart.svg",bg="transparent", width=4.5, height=3)
```
## Together
```{r chocolatechantilly}
dessertsdata %>%
unite("ChocChan", Chocolate, Chantilly) %>%
ggplot(aes(x=DessertType, fill=ChocChan))+
scale_y_continuous(labels=scales::label_percent())+
scale_fill_manual(values=c("grey80","beige", "brown", "tan3"),
labels=c("None","Only chantilly", "Only chocolate", "Both chocolate and chantilly"))+
geom_bar(position="fill")+
theme(legend.position=c(0.8, 0.8), legend.background = element_rect(fill="white", color="transparent"))+
labs(x=NULL, y="Percent of desert type with chocolate and/or chantilly", fill=NULL)
#ggsave("Figures/chocolatechantillychart.svg",bg="transparent", width=4.5, height=4.5)
```