Skip to content

A motley collection of R functions for data analysis

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

vera-gomes/MMmotley

 
 

Repository files navigation

MMmotley

Codecov test coverage R build status

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, so I went looking for a synonym.

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 with:

# install.packages("devtools")
devtools::install_github("milanmlft/MMmotley")

Contents

library(MMmotley)
library(ggplot2)

gg_pval_hist: ggplot2-based p-value histograms with better default layout

## 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.

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.

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.

# 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.

x <- seq(0, 1, by = 0.25)
format_percentage(x)
#> [1] "0%"   "25%"  "50%"  "75%"  "100%"

stat_propzero: Summarise proportion of zeros

Annotate ggplots with the proportion of zeroes in y for each x.

## 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:

  • Default template: default analysis template
  • Rmd presentation template: template for RMarkdown presentation in ioslides format

About

A motley collection of R functions for data analysis

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 93.0%
  • TeX 7.0%