-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
128 lines (90 loc) · 3.63 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# MMmotley
<!-- badges: start -->
[![Codecov test coverage](https://codecov.io/gh/milanmlft/MMmotley/branch/master/graph/badge.svg)](https://codecov.io/gh/milanmlft/MMmotley?branch=master)
[![R build status](https://github.com/milanmlft/MMmotley/workflows/R-CMD-check/badge.svg)](https://github.com/milanmlft/MMmotley/actions)
<!-- badges: end -->
This package contains various, not necessarily related, functions to make some data analysis tasks a little easier.
Why "motley"? Because "misc" or "miscellaneous" is already used [too often](https://github.com/search?q=misc+language%3AR&type=Repositories), so I went looking for a [synonym](https://www.lexico.com/synonym/miscellaneous).
Also, this is more like an educational sandbox for myself to learn more about R package development and everything related to that.
## Installation
You can install MMmotley from [GitHub](https://github.com/milanmlft/MMmotley) with:
``` r
# install.packages("devtools")
devtools::install_github("milanmlft/MMmotley")
```
## Contents
```{r}
library(MMmotley)
library(ggplot2)
```
### `gg_pval_hist`: ggplot2-based p-value histograms with better default layout
```{r}
## Generate some random uniform p-values
p_values <- runif(1000)
```
By default, `ggplot2::geom_histogram()` centers the first and last bins of
the histogram on the x-axis lower and upper limits, respectively.
```{r}
ggplot(mapping = aes(x = p_values)) +
geom_histogram(binwidth = 0.05)
```
In case of p-values, which lie in the interval [0, 1], it makes more
sense to set the boundaries of the first and last bins at 0 and 1.
This is what `gg_pval_hist` does, with some additional tweaks to improve the layout.
By default, the binwidths are set at 0.05, commonly used as a cut-off for significance,
so that the height of the first bar represents the number of p-values lying
between 0 and 0.05, i.e. the number of cases that would be deemed significant.
```{r}
p <- gg_pval_hist(p_values, binwidth = 0.05)
p
```
### `save_plots`: save multiple plots to a single PDF
This is a convenience function if you have a list of plots and want to save them
to a PDF file with each plot ending up on its own page.
```{r, eval=FALSE}
# Generate list of plots
mtcars_split <- split(mtcars, mtcars$cyl)
plot_list <- lapply(mtcars_split, function(d) {
ggplot(d, aes(mpg, wt)) +
geom_point()
})
# Save plots
file <- tempfile()
save_plots(plot_list, file)
```
### `format_percentage`: Format numbers as percentages
This is a simple wrapper around `scales::label_percent()` to convert numeric input to percentages and return them as characters.
```{r}
x <- seq(0, 1, by = 0.25)
format_percentage(x)
```
### `stat_propzero`: Summarise proportion of zeros
Annotate ggplots with the proportion of zeroes in `y` for each `x`.
```{r}
## Generate some random zero-inflated data
set.seed(42)
n <- 100
p <- 0.123
y <- ifelse(rbinom(n, size = 1, prob = p) > 0, 0, rpois(n, lambda = 2))
df <- data.frame(x = rep(c("A", "B"), each = n / 2), y = y)
## Make plot
p <- ggplot(df, aes(x, y)) + geom_jitter(alpha = 0.2, width = 0.1)
p + stat_propzero(fontface = "bold", col = "dodgerblue")
```
### RMarkdown templates
This package also contains 2 [templates for RMarkdown][rmd-templates]:
- Default template: default analysis template
- Rmd presentation template: template for RMarkdown presentation in *ioslides* format
[rmd-templates]: https://bookdown.org/yihui/rmarkdown/document-templates.html