Skip to content

Commit fb13f1f

Browse files
Vignette with example plots
1 parent aa2b155 commit fb13f1f

File tree

10 files changed

+165
-18
lines changed

10 files changed

+165
-18
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
export(extract_stan_fit)
3+
export(extract_fit)
44
export(plot_volcano)
55
export(prepare_volcano_df)
66
import(ggplot2)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' extract_stan_fit
1+
#' extract_fit
22
#'
33
#' Wrapper function to extract parameter draws from two common Stan interfaces.
44
#' This function requires the respective stan interface (rstan, brms)
@@ -21,7 +21,7 @@
2121
#' # posterior <- extract_stan_fit(fit, "b_Intercept")
2222
#' # End(Not run)
2323

24-
extract_stan_fit <- function(fit, parameter_name) {
24+
extract_fit <- function(fit, parameter_name) {
2525
# rstan
2626
if (inherits(fit, "stanfit")) {
2727
if (!requireNamespace("rstan", quietly = TRUE)) {

R/plot_volcano.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ plot_volcano <- function(result,
9595
## get threshold
9696
t <- result$meta$threshold
9797

98-
subtitle <- paste0("vertical black line: threshold for pi=",t)
98+
subtitle <- paste0("vertical black line: threshold for pi = ",t)
9999

100100
p <- ggplot(df,(aes(x=parameter.median,y=pi.value))) +
101101
geom_point()+
@@ -113,7 +113,7 @@ plot_volcano <- function(result,
113113
"errorbar: CrI ",result$meta$CrI.low,", ",result$meta$CrI.high)
114114

115115
p <- p+
116-
geom_errorbar(aes(xmin=parameter.low,xmax=parameter.high))+
116+
geom_errorbar(aes(xmin=parameter.low,xmax=parameter.high),col="grey")+
117117
ggtitle(title, subtitle)
118118
}
119119

@@ -149,7 +149,8 @@ plot_volcano <- function(result,
149149
}
150150

151151
subtitle <- paste0(subtitle,'\n',
152-
"grey lines: label thresholds")
152+
paste0("grey lines: label thresholds, |parameter| > ",
153+
label.parameter.threshold,", pi > ",label.pi.threshold))
153154

154155

155156
p <- p+

R/prepare_volcano_df.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' information (e.g., cell line, time point) based on parameter names and a user-provided
66
#' annotation data frame, and returns a data frame that is ready for plotting.
77
#'
8-
#' @param posterior A data frame of posterior draws (one row per draw) [extract_stan_fit()].
8+
#' @param posterior A data frame of posterior draws (one row per draw) [extract_fit()].
99
#' @param annotation_df A data frame with at least one column:
1010
#' \itemize{
1111
#' \item \code{parameter}: the parameter name (e.g., `doubling.1`, `logOR.treatment`)
@@ -41,7 +41,7 @@
4141
#' }
4242
#'}
4343
#'
44-
#' @seealso [extract_stan_fit()]
44+
#' @seealso [extract_fit()]
4545
#'
4646
#' @import magrittr
4747
#' @importFrom tidyr pivot_longer

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ small sample sizes, and complex experimental designs.
1313
However, Bayesian posteriors of models with many parameters are often difficult
1414
to interpret at a glance.
1515

16-
One way to quickly identify important biological changes of frequentist analysis
16+
One way to quickly identify important biological changes based on frequentist analysis
1717
are volcano plots (using fold-changes and p-values).
1818

1919
Bayesian volcano plots bring together the uncertainty aware power of Bayesian

data/annotation_df.rda

1.08 KB
Binary file not shown.

man/annotation_df.Rd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ data("posterior")
2222

2323
annotation_df <- as.data.frame(cbind(parameter=colnames(posterior),
2424
label=levels(as.factor(longitudinalMetabolomics_df$metabolite)),
25-
group=rep(c("A","B"))))
25+
group=rep(c("A","B")),value=rnorm(98,10,3)))
26+
annotation_df$value <- as.numeric(annotation_df$value)
2627
}
2728

2829
\keyword{datasets}
Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/prepare_volcano_df.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/BayesVolcano.Rmd

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
title: "BayesVolcano"
3-
author: Katja Danielzik (katja.danielzik@uni-due.de)
3+
package: BayesVolcano
4+
author: "Katja Danielzik (katja.danielzik@uni-due.de)"
5+
46
output: rmarkdown::html_vignette
57
vignette: >
68
%\VignetteIndexEntry{BayesVolcano}
@@ -15,6 +17,149 @@ knitr::opts_chunk$set(
1517
)
1618
```
1719

20+
# Data requirements
21+
22+
BayesVolcano requires as input:
23+
- posteriors of a Bayesian model fit stored in a data frame, where columns are parameters and rows are iterations. A wrapper function to extract the posterior exists for fits generated by [rstan](https://mc-stan.org/rstan) or [brms](https://paulbuerkner.com/brms) with the helper function extract_fit.R
24+
1825
```{r setup}
1926
library(BayesVolcano)
2027
```
28+
```{r}
29+
data("posterior")
30+
head(posterior[,1:4])
31+
```
32+
33+
- a data frame mapping parameter names to biological entities with at least a column named "parameter" and a column named "label". Additional columns can be provided that might be helpful for later visualization (can be categorical (here: group) or numerical (here:value))
34+
35+
```{r}
36+
data("annotation_df")
37+
head(annotation_df)
38+
```
39+
40+
# Background
41+
Bayesian models are used to estimate effect sizes (e.g., gene expression changes,
42+
protein abundance differences, drug response effects) while accounting for uncertainty,
43+
small sample sizes, and complex experimental designs.
44+
However, Bayesian posteriors of models with many parameters are often difficult to interpret at a glance.
45+
46+
One way to quickly identify important biological changes based on frequentist analysis
47+
are volcano plots (using fold-changes and p-values).
48+
49+
Bayesian volcano plots bring together the uncertainty aware power of Bayesian
50+
models and the familiar visualization of volcano plots.
51+
52+
# Workflow
53+
## Preparation of plotting dataframe
54+
55+
The prepare_volcano_df() function takes as input the posterior and the annotation
56+
data frame. For every parameter listed in the annotation data frame the function
57+
calculates one pi-value on basis of the following formula:
58+
59+
With *i* being one entity that was modeled, *param* the estimated parameter
60+
and *t* the threshold for which to calculate the pi-value.
61+
62+
![equation](https://latex.codecogs.com/svg.image?%5Cpi_%7Bi%7D=2*max%5Cleft(%5Cint_%7Bparam_%7Bi%7D=-%5Cinfty%7D%5E%7Bt%7Dp(param_%7Bi%7D)%5C,dparam_%7Bi%7D,%5Cint_%7Bparam_%7Bi%7D=t%7D%5E%7B%5Cinfty%7Dp(param_%7Bi%7D)%5C,dparam_%7Bi%7D%5Cright)-1)
63+
64+
The user further can specify which credible interval (CrI) should be calculated
65+
from the posterior. This can later be visualized in the volcano plot.
66+
67+
```{r}
68+
library(BayesVolcano)
69+
results <- prepare_volcano_df(posterior=posterior,
70+
annotation_df = annotation_df,
71+
threshold = 0.5, # for demonstration purposes, with centered parameters usually 0 is a valid choice
72+
CrI.low = 0.025, #2.5%
73+
CrI.high = 0.975) # 97.5%, in combination with CrI.low translates to 95% CrI
74+
75+
```
76+
77+
This returns a list with two elements:
78+
- a data frame with the parameter, pi.value, parameter.median, parameter.low (CrI.low),
79+
parameter.high (CrI.high), label (from annotation data frame) and additional
80+
columns (here "group") from the annotation data frame
81+
82+
```{r}
83+
head(results$result)
84+
```
85+
- the meta information (set threshold, CrI.low, and CrI.high)
86+
```{r}
87+
results$meta
88+
```
89+
90+
## Volcano Plot
91+
The plot_volcano() function is a wrapper function for convenient plotting
92+
of the results$results data frame with [ggplot2](https://ggplot2.tidyverse.org/)
93+
that automatically includes the
94+
meta information. It returns a ggplot object that can be further customized by
95+
the user.
96+
97+
The simplest version is:
98+
99+
```{r}
100+
plot_volcano(results)
101+
```
102+
103+
Where each point corresponds to one parameter of the Bayesian posterior. The
104+
set threshold for pi is displayed as vertical black line and described in the
105+
subtitle.
106+
107+
Titles and x-axis labels can be set with:
108+
```{r}
109+
plot_volcano(results,
110+
title = "My amazing plot",
111+
xlab = "My informative parameter")
112+
```
113+
114+
115+
To add the set CrI use:
116+
```{r}
117+
plot_volcano(results,
118+
CrI = TRUE)
119+
```
120+
121+
This adds the credible intervals as grey lines and adds a description of the errorbar
122+
to the subtitle.
123+
124+
To color code the points the user can choose a character of numerical column
125+
from the annotation_df
126+
127+
```{r}
128+
plot_volcano(results,
129+
CrI = FALSE,
130+
color = "group")
131+
132+
plot_volcano(results,
133+
CrI = FALSE,
134+
color = "value")
135+
```
136+
137+
To add labels the [ggrepel](https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html)
138+
is employed:
139+
140+
```{r}
141+
plot_volcano(results, label = "label")
142+
```
143+
144+
To only display labels above thresholds for the parameter value and pi set:
145+
146+
```{r}
147+
plot_volcano(results,
148+
label = "label",
149+
label.parameter.threshold = 1.5,
150+
label.pi.threshold = 0.95)
151+
```
152+
153+
Grey lines are added to visualize the threshold and a subtitle is added.
154+
155+
Of course color and labels can be combined:
156+
```{r}
157+
plot_volcano(results,
158+
color = "group",
159+
label = "label",
160+
label.parameter.threshold = 1.5,
161+
label.pi.threshold = 0.95,
162+
title = "My plot",
163+
xlab = "My informative parameter")
164+
```
165+

0 commit comments

Comments
 (0)