From 4f0e1dc39972d41d01dd9b85077fc98ddfd4d77f Mon Sep 17 00:00:00 2001 From: Chris Beeley Date: Thu, 22 Apr 2021 17:26:02 +0100 Subject: [PATCH 1/4] Add nottshc theme --- R/theme_nottshc.R | 33 +++++++++++++++++++++++++++++++++ man/theme_nottshc.Rd | 11 ++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 R/theme_nottshc.R diff --git a/R/theme_nottshc.R b/R/theme_nottshc.R new file mode 100644 index 0000000..6185c02 --- /dev/null +++ b/R/theme_nottshc.R @@ -0,0 +1,33 @@ +#' CDU data science team ggplot2 theme +#' +#' A ggplot2 theme with a white panel background, no grid lines, +#' large axis and legend titles, +#' and increased text padding for better readability. +#' +#' @param base_size Numeric. Base font size; other font sizes and margins +#' are adjusted relative to this. +#' @param base_family Character. Base font family. +#' @param box Logical. Indicates whether to draw a box around the plot. +#' +#' @details Some of this theme is borrowed with thanks from +#' https://github.com/crsh/papaja/blob/master/R/theme_apa.R +#' +#' @export +#' + +theme_nottshc <- function(base_size = 12, base_family = "", box = FALSE) { + adapted_theme <- ggplot2::theme_bw(base_size, base_family) + + ggplot2::theme( + plot.title = ggplot2::element_text(size = ggplot2::rel(1.1), + margin = ggplot2::margin(0, 0, ggplot2::rel(14), 0)) + + ) + + if(box) { + adapted_theme <- adapted_theme + ggplot2::theme(panel.border = ggplot2::element_rect(color = "black")) + } else { + adapted_theme <- adapted_theme + ggplot2::theme(panel.border = ggplot2::element_blank()) + } + + adapted_theme +} diff --git a/man/theme_nottshc.Rd b/man/theme_nottshc.Rd index fa3d2d2..6be68fb 100644 --- a/man/theme_nottshc.Rd +++ b/man/theme_nottshc.Rd @@ -1,9 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/theme.R +% Please edit documentation in R/theme.R, R/theme_nottshc.R \name{theme_nottshc} \alias{theme_nottshc} \title{NOTTSHC CDU data science team ggplot2 theme} \usage{ +theme_nottshc(base_size = 12, base_family = "", box = FALSE) + theme_nottshc(base_size = 12, base_family = "", box = FALSE) } \arguments{ @@ -15,6 +17,10 @@ are adjusted relative to this.} \item{box}{Logical. Indicates whether to draw a box around the plot.} } \description{ +A ggplot2 theme with a white panel background, no grid lines, +large axis and legend titles, +and increased text padding for better readability. + A ggplot2 theme with a white panel background, no grid lines, large axis and legend titles, and increased text padding for better readability. @@ -22,4 +28,7 @@ and increased text padding for better readability. \details{ Some of this theme is borrowed with thanks from https://github.com/crsh/papaja, see https://github.com/crsh/papaja/blob/master/R/theme_apa.R. + +Some of this theme is borrowed with thanks from +https://github.com/crsh/papaja/blob/master/R/theme_apa.R } From f2446995cfc520ec44f95d3aa805ee0e01ca41bf Mon Sep 17 00:00:00 2001 From: Chris Beeley Date: Wed, 28 Apr 2021 09:33:51 +0100 Subject: [PATCH 2/4] Revert "Add nottshc theme" --- R/theme_nottshc.R | 33 --------------------------------- man/theme_nottshc.Rd | 11 +---------- 2 files changed, 1 insertion(+), 43 deletions(-) delete mode 100644 R/theme_nottshc.R diff --git a/R/theme_nottshc.R b/R/theme_nottshc.R deleted file mode 100644 index 6185c02..0000000 --- a/R/theme_nottshc.R +++ /dev/null @@ -1,33 +0,0 @@ -#' CDU data science team ggplot2 theme -#' -#' A ggplot2 theme with a white panel background, no grid lines, -#' large axis and legend titles, -#' and increased text padding for better readability. -#' -#' @param base_size Numeric. Base font size; other font sizes and margins -#' are adjusted relative to this. -#' @param base_family Character. Base font family. -#' @param box Logical. Indicates whether to draw a box around the plot. -#' -#' @details Some of this theme is borrowed with thanks from -#' https://github.com/crsh/papaja/blob/master/R/theme_apa.R -#' -#' @export -#' - -theme_nottshc <- function(base_size = 12, base_family = "", box = FALSE) { - adapted_theme <- ggplot2::theme_bw(base_size, base_family) + - ggplot2::theme( - plot.title = ggplot2::element_text(size = ggplot2::rel(1.1), - margin = ggplot2::margin(0, 0, ggplot2::rel(14), 0)) - - ) - - if(box) { - adapted_theme <- adapted_theme + ggplot2::theme(panel.border = ggplot2::element_rect(color = "black")) - } else { - adapted_theme <- adapted_theme + ggplot2::theme(panel.border = ggplot2::element_blank()) - } - - adapted_theme -} diff --git a/man/theme_nottshc.Rd b/man/theme_nottshc.Rd index 6be68fb..fa3d2d2 100644 --- a/man/theme_nottshc.Rd +++ b/man/theme_nottshc.Rd @@ -1,11 +1,9 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/theme.R, R/theme_nottshc.R +% Please edit documentation in R/theme.R \name{theme_nottshc} \alias{theme_nottshc} \title{NOTTSHC CDU data science team ggplot2 theme} \usage{ -theme_nottshc(base_size = 12, base_family = "", box = FALSE) - theme_nottshc(base_size = 12, base_family = "", box = FALSE) } \arguments{ @@ -17,10 +15,6 @@ are adjusted relative to this.} \item{box}{Logical. Indicates whether to draw a box around the plot.} } \description{ -A ggplot2 theme with a white panel background, no grid lines, -large axis and legend titles, -and increased text padding for better readability. - A ggplot2 theme with a white panel background, no grid lines, large axis and legend titles, and increased text padding for better readability. @@ -28,7 +22,4 @@ and increased text padding for better readability. \details{ Some of this theme is borrowed with thanks from https://github.com/crsh/papaja, see https://github.com/crsh/papaja/blob/master/R/theme_apa.R. - -Some of this theme is borrowed with thanks from -https://github.com/crsh/papaja/blob/master/R/theme_apa.R } From aa666955f52451f1eedf74fedee50217a6ea2d6b Mon Sep 17 00:00:00 2001 From: Chris Beeley Date: Wed, 28 Apr 2021 09:39:31 +0100 Subject: [PATCH 3/4] Add the sample_vector function --- NAMESPACE | 1 + R/sample_vector.R | 21 +++++++++++++++++++++ man/sample_vector.Rd | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 R/sample_vector.R create mode 100644 man/sample_vector.Rd diff --git a/NAMESPACE b/NAMESPACE index d432d64..54fb271 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2: do not edit by hand export(hash) +export(sample_vector) export(theme_nottshc) diff --git a/R/sample_vector.R b/R/sample_vector.R new file mode 100644 index 0000000..4a0a405 --- /dev/null +++ b/R/sample_vector.R @@ -0,0 +1,21 @@ +#' CDU data science team ggplot2 theme +#' +#' A ggplot2 theme with a white panel background, no grid lines, +#' large axis and legend titles, +#' and increased text padding for better readability. +#' +#' @param values Vector- the thing you want to sample from +#' @param weights vector of integers the same length as values- the proportions +#' with which you want to sample +#' @param length integer- how long you want the return value to be- e.g. the +#' number of rows in a dataframe +#' +#' @export +sample_vector <- function(values, weights, length){ + + sample(values, length, replace = TRUE, prob = weights / sum(weights)) +} + +sample_vector(values = c(NA, "Male", "Female", "Other"), + weights = c(10, 50, 50, 2), + length = 100) diff --git a/man/sample_vector.Rd b/man/sample_vector.Rd new file mode 100644 index 0000000..1a89557 --- /dev/null +++ b/man/sample_vector.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sample_vector.R +\name{sample_vector} +\alias{sample_vector} +\title{CDU data science team ggplot2 theme} +\usage{ +sample_vector(values, weights, length) +} +\arguments{ +\item{values}{Vector- the thing you want to sample from} + +\item{weights}{vector of integers the same length as values- the proportions +with which you want to sample} + +\item{length}{integer- how long you want the return value to be- e.g. the +number of rows in a dataframe} +} +\description{ +A ggplot2 theme with a white panel background, no grid lines, +large axis and legend titles, +and increased text padding for better readability. +} From b987207525144aa70b6b662627b7b4b92a78737b Mon Sep 17 00:00:00 2001 From: Chris Beeley Date: Wed, 28 Apr 2021 09:41:21 +0100 Subject: [PATCH 4/4] Increment version number --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index abd8509..5637dd8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: nottshcMethods Title: Helper functions for working with NHS healthcare data by the Data Science Team of the Nottinghamshire Healthcare NHS Foundation Trust -Version: 0.0.1 +Version: 0.1.0 Date: 2021-03-29 Authors@R: c( person("Milan", "Wiedemann", email = "milan.wiedemann@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-1991-282X")),