-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotplot_lipidomics.qmd
177 lines (140 loc) · 5.34 KB
/
dotplot_lipidomics.qmd
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
# Box/dot plots with Contrasts
```{r}
#| label: setup
#| echo: false
#| message: false
source("_common.R")
library(knitr)
```
## Libraries
```{r}
#| label: load-lib
#| echo: true
#| message: false
library(here)
library(tidyverse)
```
## Data Format
The examples below require a tidy ('long-format') table with following columns: `SubjectID`, `Group`, `Lipid` and `Conc`. See chapters xxxx on how to prepare data in this format. In this example a test dataset is used that is converted into the tidy format.
```{r}
#| label: read-testdata
#| echo: true
#| message: false
# Load and format test data
d_long <- read_csv(here("data/Metabolites-1614644-supplementary.csv")) |>
pivot_longer(cols = -c(SubjectID, Group), names_to = "Lipid", values_to = "Conc")
```
```{r}
#| label: tbl-data
#| echo: false
#| tbl-cap: "Test data in tidy format"
kable(head(d_long, n = 5),digits = 2, caption = NULL)
```
## Single faceted box/dot plot
This is for one page (means one plot with n rows and m columns)
```{r select-rename-columns}
#| label: plot-1
#| echo: true
#| message: false
#| fig-height: 10
#| results: hide
# OPTIONAL: order your groups as you wish (otherwise it will be alphabetical)
d_long$Group <- factor(d_long$Group,
levels = c("Healthy", "Cushing", "Hypothyroidism"))
# OPTIONAL: define specific color for each group,
my_colors <- c(Healthy = "grey50", Cushing = "red", Hypothyroidism = "#1b80a1")
# OPTIONAL: Subset your data
d_plot <- d_long |> filter(str_detect(Lipid, "CE"))
# IMPORTANT: Change variable names to match your data (i.e. Group, Conc, Lipid)
# IMPORTANT: Change group names and add/remove comparisons in `ggsignif()`
ggplot(d_plot, aes(x = Group, y = Conc, color = Group,fill = Group)) +
geom_boxplot(alpha = 0.2, lwd=0.3, outlier.shape = NA) +
geom_jitter(size = 0.5,width = 0.2) +
facet_wrap(~Lipid, scales = "free", nrow = 4, ncol = 5) +
scale_y_continuous(limits = c(0,NA), expand = expansion(mult = c(0, .15)))+
scale_color_manual(values = my_colors) +
scale_fill_manual(values = my_colors) +
ggsignif::geom_signif(
comparisons = list(
c("Healthy", "Cushing") ,
c("Cushing", "Hypothyroidism"),
c("Healthy", "Hypothyroidism")
),
test = "t.test",
test.args = list(paired=FALSE, var.equal = FALSE, na.rm = TRUE),
map_signif_level = c("***"=0.001, "**"=0.01, "*"=0.05, " " = 1),
margin_top = 0.05, step_increase = 0.1, vjust = 0.5, size = 0.2,
tip_length = 0.05, textsize = 4, color = "black") +
theme_bw(base_size = 7) +
theme(
axis.text.x = element_text( size=6, angle = 45,vjust = 1, hjust = 1),
panel.grid = element_blank(),
legend.position="none"
)
```
## Multi-page faceted box/dot plot
Here we make the same plots as before but over multiple pages/figures, like this we can make boxplots for each lipid If you adopt this for your own data, you need to set number of plots per page (number of rows and columns), also you need to update it with your group names and colors.
```{r select-rename-columns}
#| label: plot-2
#| echo: true
#| message: false
#| fig-height: 10
#| results: hide
# Order the groups as you wish (otherwise it will be alphabetical), if you are
d_long$Group <- factor(d_long$Group,
levels = c("Healthy", "Cushing", "Hypothyroidism"))
# Define your color for each group,
# can also get colors (e.g. #1b80a1) via e.g. https://g.co/kgs/DQH6Fr
my_colors <- c(Healthy = "grey50", Cushing = "red", Hypothyroidism = "#1b80a1")
# In this example we subet to have 3 pages, remove the filter for all lipids
d_plot <- d_long |> filter(str_detect(Lipid, "LPC|LPE"))
# Define a function to plot one faceted plot (=one page)
plot_one_page <- function(data, n_row, n_col){
ggplot(data, aes(x = Group, y = Conc, color = Group,fill = Group)) +
geom_boxplot(alpha = 0.2, lwd=0.3, outlier.shape = NA) +
geom_jitter(size = 0.5,width = 0.2) +
facet_wrap(~Lipid, scales = "free", nrow = n_row, ncol = n_col) +
scale_y_continuous(limits = c(0,NA), expand = expansion(mult = c(0, .15)))+
scale_color_manual(values = my_colors) +
scale_fill_manual(values = my_colors) +
ggsignif::geom_signif(
comparisons = list(
c("Healthy", "Cushing") ,
c("Cushing", "Hypothyroidism"),
c("Healthy", "Hypothyroidism")
),
test = "t.test",
test.args = list(paired=FALSE, var.equal = FALSE, na.rm = TRUE),
map_signif_level = c("***"=0.001, "**"=0.01, "*"=0.05, " " = 1),
margin_top = 0.05, step_increase = 0.1, vjust = 0.5, size = 0.2,
tip_length = 0.05, textsize = 4, color = "black") +
theme_bw(base_size = 7) +
theme(
axis.text.x = element_text( size=6, angle = 45,vjust = 1, hjust = 1),
panel.grid = element_blank(),
legend.position="none"
)
}
# Set how many plots per page (number of rows and columns)
rows_page = 3
columns_page = 5
my_plots <- d_plot %>%
group_by(Lipid) |>
mutate(page_no = ceiling(cur_group_id()/ (rows_page * columns_page))) |>
group_by(page_no) %>%
nest() %>%
mutate(plt = map(data, ~ plot_one_page(.,n_row = rows_page, n_col = columns_page)))
my_plots$plt
```
## Save multi-page plot a PDF
```{r select-rename-columns}
#| label: save-to-pdf
#| echo: true
#| message: false
#| fig-height: 10
#| results: hide
#| eval: false
pdf(file = "myplot.pdf", onefile = TRUE, paper = "A4r", width = 11)
my_plots$plt
dev.off()
```