-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bar plots_Kieber Lab_2023.Rmd
232 lines (188 loc) · 8.21 KB
/
Bar plots_Kieber Lab_2023.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
---
title: "Bar plots_Kieber Lab_2023"
author: "Emily J Tallerday"
date: "`r Sys.Date()`"
output:
powerpoint_presentation:
slide_level: 1
---
## Setup
This is for the R markdown file settings. You shouldn't need to change anything
```{r setup, include = F}
knitr::opts_chunk$set(echo = T ,
warning = F #, # including this is the ONLY way to get rid of the warnings in the flowering time plot! doing stuff inside that code chunk itself does NOT work.
# message = F #,
# results = "hide" ,
# error = F ,
# include = F
)
knitr::opts_chunk$set(tidy.opts = list(width.cutoff = 60), tidy = TRUE)
```
These are the packages we need. You will likely have to install most of these in your version of R/Rstudio
```{r, include = F}
library(rmarkdown)
#library(pandoc)
library(formatR)
packages <- c("tidyverse","gridExtra","ggpubr","viridis","ggthemes",
"here","gplots","cowplot","ggtext","ggsignif")
lapply(packages, require, character.only=T)
here()
library(ggokabeito) # https://ggplot2.tidyverse.org/reference/scale_colour_discrete.html
theme_set(theme_light()) # set theme
```
Set theme for ggplot
```{r, include = F}
theme_set(theme_light()) # set theme for ggplot2
```
Check location of your WD (where your data files should be):
```{r, include = F}
here()
```
## Functions
The base function for generating the bar plots with most of the settings populated. You shouldn't need to change this, but you do have to run it
```{r, include = F}
## FUNCTION to make Joe's nice bar plots (for presentations)
Bar_plot_data_by_genotype_signif_okabe <- function(data, mapping, compare, test_type) {
ggplot(data = data, mapping = mapping) +
stat_summary(fun = mean, geom = "bar", alpha = 0.75) +
geom_jitter(shape = 21, size = 2, alpha = 0.5, width = 0.3, height = 0.15, color = "black", fill = "black") +
### stats
stat_summary(fun.data = mean_se, geom = "errorbar", color = "black", width = 0.1, size = 0.75) + # adding error bars
geom_signif(comparisons = compare, test = test_type,
map_signif_level = c("***"=0.001,"**"=0.01, "*"=0.05),
textsize = 6, color = "black", margin_top = 0.05, step_increase = 0.14, tip_length = 0.02 )
}
```
For reference
```{r, include = F}
# ## Running example
# p2 <- Bar_plot_data_by_genotype_signif_okabe(data = (double_shoot_data %>% dplyr::filter(Number_shoots > 0)), aes(x = Genotype, y = Number_shoots, color = Genotype, fill = Genotype), test_type = wilcox.test, compare = list(c("rr3,5del3,6,7,11","rr3,5,6,7,11"), c("WT", "rr3,5,6,7,11")) )
#
# ## How to add labels, etc. ***
# p2 +
# ylim(NA,3.5) +
# labs(
# title = "No. of shoots present in seedlings (7 DAG)",
# x = "Genotype",
# y = "No. of shoots",
# color = "Genotype"
# ) +
#
# ### Setting legend names to italics while keeping the okabe coloring
# scale_x_discrete(labels = c("WT","<i>rr3,5del3,6,7,11</i>","<i>rr3,5,6,7,11</i>") ) +
# theme(axis.text.x = element_markdown()) +
#
# scale_color_hue(labels = c("WT","<i>rr3,5del3,6,7,11</i>","<i>rr3,5,6,7,11</i>") ) + # , name = "Genotype"
# theme(legend.text = element_markdown()) +
#
# scale_fill_okabe_ito(labels = c("WT","<i>rr3,5del3,6,7,11</i>","<i>rr3,5,6,7,11</i>"))
```
## Data analysis
### Load the data
I'm including some of our chlorophyll concentration data as an example -- make sure this csv file is in your current working directory (what `here()` says)
```{r, include = F}
CCI <- read.csv(file = here("Data/Opti-Sciences_CCI_full_04242023.csv") ) # read in the file
CCI <- CCI %>% na.omit() # clean it up by removing NA rows
```
NOTE: when you use your own data you'll want to format it in the "tidy" format like this csv file.
### Format data
Set names and formatting for some labels:
```{r, include = F}
## Char vec naming genotypes w MD formatting:
Genotype <- c("<i>hk3,4,5</i>", "<i>rr3,5,6,7,11</i>", "WT")
Date <- c("4/24/2023", "4/25/2023", "4/26/2023")
```
Set data classes for certain columns, as needed:
```{r, include = F}
## Manage data classes:
CCI$Rep <- as.factor(CCI$Rep)
CCI$Date <- as.factor(CCI$Date)
CCI$Genotype <- as.factor(CCI$Genotype)
CCI <- dplyr::filter(CCI, Genotype != "")
CCI$CCI <- as.numeric(CCI$CCI)
```
### Prep stats to run
Set what stats you want to run, and which things to compare. See [here](https://stackoverflow.com/questions/57567118/how-to-create-in-r-a-vector-automatically-which-contains-all-groups-to-be-compar) for details. The following will generate a list with all possible `Genotype` names paired (`2`), and this will be used to run the t.tests/equivalent later on.
```{r, include = F}
pairs <- combn(as.character(unique(CCI$Genotype)), 2, simplify = F)
```
# Making your plot
```{r, echo = F, dev = "png", dpi = 300}
Bar_plot_data_by_genotype_signif_okabe(
data = CCI %>%
dplyr::filter(Date == "4/26/2023" ), # df to plot from, and I've filtered it so only data from that date are included
### Set axis variables and coloring:
aes(x = Genotype,
y = CCI,
color = Genotype,
fill = Genotype), # color = line color, fill = bar fill color
### Give test info:
test_type = wilcox.test, # the default is a standart t-test. I've set wilcox here because we have more than 2 genotypes
compare = pairs # This must match the list "pairs" made above
) +
### Adjust labels:
labs(
title = "Chlorophyll content in 9-week old plants after 5 days dark treatment",
x = "Genotype",
y = "Chlorophyll content (CCI)",
color = "Genotype", # in legend
fill = "Genotype" # in legend
) +
theme(axis.text.x = element_text(angle = 45, hjust=1)) + # turns x-axis labels 45 degrees to fit
### Setting legend names to italics while keeping the special okabe coloring
# just make sure these line up with what you set to color and fill at the top!
scale_x_discrete(labels = Genotype) + # x-axis variable
theme(axis.text.x = element_markdown()) + # will take MD formatting you provided to do italics/bold/whatever -- here I've set it above to make the mutant names in italics
scale_color_hue(labels = Genotype) + # color variable
theme(legend.text = element_markdown()) +
scale_fill_okabe_ito(labels = Genotype) # fill variable
```
You can copy and paste the plot above into another document. This document is set to make a presentation, and all plots generated will be made as png's with a DPI of 300 (`dev = "png", dpi = 300` at top of this plot chunk).
```{r}
# knit doc early to exclude everything below from going into ppt file
knitr::knit_exit()
```
## Trying to optimize stats visuals with iris dataset
```{r, include = F}
## Char vec naming genotypes w MD formatting:
Species <- c("<i>setosa</i>", "<i>versicolor</i>", "virginica")
```
```{r, echo = F, dev = "png", dpi = 300}
# pairs <- combn(as.character(unique(iris$Species)), 2, simplify = F)
#
# Bar_plot_data_by_genotype_signif_okabe(
# data = iris %>%
# ### Set axis variables and coloring:
# aes(x = Species,
# y = `Petal.Length`,
# color = Species,
# fill = Species), # color = line color, fill = bar fill color
#
# ### Give test info:
# test_type = wilcox.test, # the default is a standart t-test. I've set wilcox here because we have more than 2 genotypes
# compare = pairs # This must match the list "pairs" made above
# ) +
#
# ### Adjust labels:
# labs(
# title = "",
# x = "Species",
# y = "Petal Length",
# color = "Species", # in legend
# fill = "Species" # in legend
# ) +
# theme(axis.text.x = element_text(angle = 45, hjust=1)) + # turns x-axis labels 45 degrees to fit
#
#
# ### Setting legend names to italics while keeping the special okabe coloring
# # just make sure these line up with what you set to color and fill at the top!
# scale_x_discrete(labels = Species) + # x-axis variable
# theme(axis.text.x = element_markdown()) + # will take MD formatting you provided to do italics/bold/whatever -- here I've set it above to make the mutant names in italics
# scale_color_hue(labels = Species) + # color variable
# theme(legend.text = element_markdown()) +
# scale_fill_okabe_ito(labels = Species) # fill variable
```
## Session info
```{r, include = F}
sessionInfo()
```