Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base theme for forestploter #61

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ License: GPL (>= 3)
Depends:
R (>= 2.10)
Imports:
forestploter,
ggplot2,
stringr,
systemfonts,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export(oxford_brewer_palettes)
export(oxford_fonts)
export(oxford_theme_palettes)
export(theme_oxford)
export(theme_oxford_forest)
importFrom(forestploter,forest_theme)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_line)
importFrom(ggplot2,element_rect)
Expand Down
4 changes: 2 additions & 2 deletions R/oxford_theme.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#'
#' A generic/base [ggplot2] theme for the oxthema package
#' A generic/base [ggplot2[] theme for the oxthema package
#'
#' This generic/base [ggplot2] theme is the template from which all other
#' This generic/base [ggplot2[] theme is the template from which all other
#' themes in the [oxthema] package is built on. This theme is inspired by
#' Bob Rudis' [hrbrthemes](https://github.com/hrbrmstr/hrbrthemes) package
#' drawing heavily on its typography-centric focus. This function uses the
Expand Down
74 changes: 74 additions & 0 deletions R/oxford_theme_forest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#'
#' Oxford theme for [forestploter] package
#'
#' @param base_size The size of text
#' @param base_family The font family to use for the text. Default to
#' main Oxford font *Roboto*
#' @param ci_col Colour of the CI. A vector of colour should be provided for
#' the grouped forest plot. Default set to *Oxford blue*
#' @param ci_fill Colour fill the point estimation. A vector of colour should be
#' provided for the grouped forest plot. Default to value of `ci_col`.
#' @param refline_col Line colour for the reference line. Default set to
#' *Oxford sienna*.
#' @param vertline_col Line colour for the extra vertical line. Default set to
#' *Oxford sienna*.
#' @param summary_col Colour for borders of the summary diamond shape. Default
#' set to *Oxford dusk*
#' @param summary_fill Colour for filling the summary diamond shape. Default set
#' to value of `summary_col`.
#' @param footnote_cex Multiplier applied to font size for footnote.
#' @param footnote_fontface The font face for footnote.
#' @param footnote_col Colour of the footnote. Default set to
#' *Oxford sage green*.
#' @param title_col Colour of title. Default set to *Oxford blue*
#' @param title_fontfamily Font family of title. Default set to value of
#' `base_family`.
#' @param arrow_fill Filling colour of the arrow head. Default set to
#' *Oxford blue*.
#' @param arrow_col Line and text colour of the arrow. Default set to value of
#' `arrow_fill`.
#'
#' @returns A list of specified theme parameters.
#'
#' @examples
#' theme_oxford_forest()
#'
#' @rdname theme_forest
#' @export
#'
theme_oxford_forest <- function(base_size = 11,
base_family = "Roboto",
ci_col = get_oxford_colour("Oxford blue"),
ci_fill = ci_col,
refline_col = get_oxford_colour("red"),
vertline_col = get_oxford_colour("red"),
summary_col = get_oxford_colour("ash"),
summary_fill = summary_col,
footnote_cex = 0.7,
footnote_fontface = "bold",
footnote_col = get_oxford_colour("royal"),
title_col = get_oxford_colour("Oxford blue"),
title_fontfamily = base_family,
arrow_fill = get_oxford_colour("Oxford blue"),
arrow_col = arrow_fill) {
forestploter::forest_theme(
base_size = base_size,
base_family = base_family,
ci_col = ci_col,
ci_fill = ci_fill,
refline_lwd = 1.5,
refline_col = refline_col,
vertline_col = vertline_col,
summary_col = summary_col,
summary_fill = summary_fill,
footnote_cex = footnote_cex,
footnote_fontface = footnote_fontface,
footnote_col = footnote_col,
title_col = title_col,
title_fontfamily = title_fontfamily,
arrow_fill = arrow_fill,
arrow_col = arrow_col,
arrow_lwd = 1.25

Check warning on line 71 in R/oxford_theme_forest.R

View check run for this annotation

Codecov / codecov/patch

R/oxford_theme_forest.R#L54-L71

Added lines #L54 - L71 were not covered by tests
)
}

1 change: 1 addition & 0 deletions R/oxthema.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
#' @importFrom systemfonts system_fonts
#' @importFrom ggplot2 theme margin element_rect element_text element_line
#' element_blank
#' @importFrom forestploter forest_theme
#'
"_PACKAGE"
4 changes: 2 additions & 2 deletions man/oxford_theme.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions man/theme_forest.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ navbar:
href: articles/oxford-palettes.html
- text: "Oxford Theme"
href: articles/oxford-theme.html
- text: "Add-on Themes"
href: articles/addon-themes.html
twitter:
icon: "fab fa-twitter fa-lg"
href: http://twitter.com/OxIHTM
Expand Down Expand Up @@ -69,6 +71,10 @@ reference:
contents:
- theme_oxford

- title: Add-on Themes
contents:
- theme_oxford_forest

- title: Utilities
contents:
- cmyk2rgb
Expand Down
68 changes: 68 additions & 0 deletions vignettes/addon-themes.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: "Add-on Themes"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Add-on Themes}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

```{r setup, echo = FALSE}
library(oxthema)
library(forestploter)
library(grid)
```

## Oxford theme for `forestploter`

```{r fp-data, echo = FALSE}
dt <- read.csv(system.file("extdata", "example_data.csv", package = "forestploter"))

# Indent the subgroup if there is a number in the placebo column
dt$Subgroup <- ifelse(is.na(dt$Placebo),
dt$Subgroup,
paste0(" ", dt$Subgroup))

# NA to blank
dt$Treatment <- ifelse(is.na(dt$Treatment), "", dt$Treatment)
dt$Placebo <- ifelse(is.na(dt$Placebo), "", dt$Placebo)
dt$se <- (log(dt$hi) - log(dt$est))/1.96

# Add a blank column for the forest plot to display CI.
# Adjust the column width with space.
dt$` ` <- paste(rep(" ", 20), collapse = " ")

# Create confidence interval column to display
dt$`HR (95% CI)` <- ifelse(is.na(dt$se), "",
sprintf("%.2f (%.2f to %.2f)",
dt$est, dt$low, dt$hi))
```

```{r fp-theme, fig.align = "center", fig.width = 8, fig.height = 6}
# Define theme
tm <- theme_oxford_forest()

p <- forest(dt[,c(1:3, 20:21)],
est = dt$est,
lower = dt$low,
upper = dt$hi,
sizes = dt$se,
ci_column = 4,
ref_line = 1,
arrow_lab = c("Placebo Better", "Treatment Better"),
xlim = c(0, 4),
ticks_at = c(0.5, 1, 2, 3),
footnote = "This footnote is in Oxford ash grey",
theme = tm)

# Print plot
plot(p)
```

6 changes: 3 additions & 3 deletions vignettes/oxford-theme.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ library(oxthema)
library(ggplot2)
```

The `oxthema` package has a generic/base [ggplot2] theme which is the template from which all other themes in the [oxthema] package is built on. This theme is inspired by Bob Rudis' [hrbrthemes](https://github.com/hrbrmstr/hrbrthemes) package drawing heavily on its typography-centric focus. This function uses the *Roboto* Google font which is the core Oxford san serif font. It is freely downloadable and easily installed on any system.
The `oxthema` package has a generic/base `ggplot2` theme which is the template from which all other themes in the `oxthema` package is built on. This theme is inspired by Bob Rudis' [hrbrthemes](https://github.com/hrbrmstr/hrbrthemes) package drawing heavily on its typography-centric focus. This function uses the *Roboto* Google font which is the core Oxford san serif font. It is freely downloadable and easily installed on any system.

## Oxford theme palettes for use in plotting

Expand All @@ -46,7 +46,7 @@ ggplot(
x = "Engine shape",
y = "Counts"
) +
theme_oxford()
theme_oxford(grid = "Y")
```

### Scatter plot
Expand All @@ -65,7 +65,7 @@ ggplot(data = mtcars, mapping = aes(x = mpg, y = disp, colour = factor(cyl))) +
title = "This title is in Oxford Blue",
subtitle = "This subtitle is in Oxford Ash Grey"
) +
theme_oxford()
theme_oxford(grid = "XY")
```

### Plotting maps using Oxford palettes
Expand Down