Skip to content

Commit

Permalink
Merge pull request #76 from neilstats/dev
Browse files Browse the repository at this point in the history
minor updates
  • Loading branch information
neilstats authored Aug 9, 2022
2 parents 1611216 + 8d3dd5a commit 0ba9c74
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 172 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- uses: r-lib/actions/setup-r@v1
with:
use-public-rspm: true
r-version: '4.1.2'

- uses: r-lib/actions/setup-r-dependencies@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Title: Create CKB Plots
Description: ckbplotr provides functions to help create and style plots in R.
It is being developed by, and primarily for, China Kadoorie Biobank
researchers.
Version: 0.6.5
Version: 0.6.6
Authors@R:
person(given = "Neil",
family = "Wright",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# ckbplotr 0.6.6

* make_forest_plot() addtext argument can now add multiple tests results and/or text under the same row.
* README updated.

# ckbplotr 0.6.5

* Added arguments to control the colour of non-data components of a plot.
* Use R markdown to render plot code to display in Viewer pane of RStudio. (Replacing use of highlight package.)
* Updates to vignettes.
Expand Down
224 changes: 119 additions & 105 deletions R/make_forest_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,29 @@
#' @export

make_forest_data <- function(
panels,
col.key = "key",
row.labels = NULL,
row.labels.levels = c("heading1", "heading2", "heading3"),
rows = NULL,
panel.names = NULL,
col.estimate = "estimate",
col.stderr = "stderr",
col.lci = NULL,
col.uci = NULL,
col.left = NULL,
col.right = NULL,
col.keep = NULL,
ci.delim = ", ",
digits = 2,
exponentiate = TRUE,
blankrows = c(1, 1, 0, 0),
scalepoints = FALSE,
minse = NULL,
addtext = NULL,
cols = panels,
headings = NULL,
colnames = NULL
panels,
col.key = "key",
row.labels = NULL,
row.labels.levels = c("heading1", "heading2", "heading3"),
rows = NULL,
panel.names = NULL,
col.estimate = "estimate",
col.stderr = "stderr",
col.lci = NULL,
col.uci = NULL,
col.left = NULL,
col.right = NULL,
col.keep = NULL,
ci.delim = ", ",
digits = 2,
exponentiate = TRUE,
blankrows = c(1, 1, 0, 0),
scalepoints = FALSE,
minse = NULL,
addtext = NULL,
cols = panels,
headings = NULL,
colnames = NULL
){

# legacy arguments
Expand Down Expand Up @@ -120,13 +120,13 @@ make_forest_data <- function(
if(!is.null(row.labels) & !all(sapply(row.labels[row.labels.levels], is.character))) stop("row.labels.levels columns must be character")

# Make vector of keys after which extra rows are added for addtext
extrarowkeys <- c()
addtextcols <- tibble::tibble(text = character(),
het_dof = character(),
het_stat = character(),
het_p = character(),
trend_stat = character(),
trend_p = character())
extrarowkeys <- c()
if (!is.null(addtext)) {
for (i in 1:length(addtext)) {
addtext[[i]] <- dplyr::bind_rows(addtextcols, addtext[[i]]) %>%
Expand All @@ -147,12 +147,18 @@ make_forest_data <- function(
)) %>%
dplyr::select(key = !!rlang::sym(col.key),
.data$addtext) %>%
dplyr::mutate(key = as.character(.data$key))

extrarowkeys <- c(extrarowkeys, addtext[[i]][["key"]])
dplyr::mutate(key = as.character(.data$key)) %>%
dplyr::group_by(.data$key) %>%
dplyr::mutate(addtextrow = 1:dplyr::n() - 1) %>%
dplyr::ungroup()
}
extrarowkeys <- purrr::reduce(purrr::map(addtext,
~ dplyr::count(., .data$key)),
dplyr::bind_rows) %>%
dplyr::group_by(.data$key) %>%
dplyr::summarise(n = max(.data$n))
extrarowkeys <- rep(extrarowkeys$key, extrarowkeys$n)
}
extrarowkeys <- unique(extrarowkeys)

# create data frame of row numbers and labels
if (is.null(row.labels)) {
Expand Down Expand Up @@ -247,6 +253,11 @@ make_forest_data <- function(
}
}

out <- out %>%
dplyr::group_by(.data$extrarowkey) %>%
dplyr::mutate(addtextrow = 1:dplyr::n() - 1) %>%
dplyr::ungroup()

# add a blank heading at bottom if needed
if (utils::tail(out$row.label, 1) != "") {
out <- out %>%
Expand All @@ -255,7 +266,7 @@ make_forest_data <- function(

out <- out %>%
dplyr::mutate(row = 1:dplyr::n()) %>%
dplyr::select(.data$row, .data$row.label, .data$key, .data$extrarowkey)
dplyr::select(.data$row, .data$row.label, .data$key, .data$extrarowkey, .data$addtextrow)

# make datatoplot
datatoplot <- tibble::tibble()
Expand Down Expand Up @@ -284,7 +295,10 @@ make_forest_data <- function(
dplyr::mutate(panel = panel.names[[i]])

if (!is.null(addtext)){
out1 <- merge(out1, addtext[[i]], by.x = "extrarowkey", by.y = "key", all.x = TRUE)
out1 <- merge(out1, addtext[[i]],
by.x = c("extrarowkey", "addtextrow"),
by.y = c("key", "addtextrow"),
all.x = TRUE)
} else {
out1 <- dplyr::mutate(out1, addtext = as.character(NA))
}
Expand Down Expand Up @@ -344,7 +358,7 @@ make_forest_data <- function(
ci.delim,
format(round(uci_transformed, digits), nsmall = digits, trim = T),
")"))) %>%
dplyr::select(-.data$extrarowkey) %>%
dplyr::select(-.data$extrarowkey, -.data$addtextrow) %>%
dplyr::arrange(panel, row)


Expand Down Expand Up @@ -480,80 +494,80 @@ make_forest_data <- function(


make_forest_plot <- function(
panels,
row.labels = NULL,
row.labels.levels = c("heading1", "heading2", "heading3"),
rows = NULL,
exponentiate = TRUE,
logscale = exponentiate,
panel.names = NULL,
panel.headings = panel.names,
col.key = "key",
col.estimate = "estimate",
col.stderr = "stderr",
col.lci = NULL,
col.uci = NULL,
col.left = NULL,
col.right = NULL,
col.right.parse = FALSE,
col.left.heading = "",
col.right.heading = "HR (95% CI)",
col.left.pos = NULL,
col.right.pos = NULL,
col.left.hjust = 1,
col.right.hjust = 0,
col.heading.space = 0,
estcolumn = TRUE,
col.keep = NULL,
ci.delim = ", ",
digits = 2,
title = "",
xlab = "HR (95% CI)",
xlim = NULL,
xticks = NULL,
nullval = NULL,
blankrows = c(1, 1, 0, 0),
col.diamond = NULL,
diamond = NULL,
col.bold = NULL,
bold.labels = NULL,
scalepoints = FALSE,
minse = NULL,
pointsize = 3,
shape = NULL,
plotcolour = "black",
colour = NULL,
cicolour = colour,
fill = NULL,
ciunder = NULL,
addtext = NULL,
left.space = NULL,
right.space = NULL,
mid.space = unit(5, "mm"),
plot.margin = margin(8, 8, 8, 8, "mm"),
panel.width = NULL,
base_size = 11,
base_line_size = base_size/22,
stroke = 0,
printplot = TRUE,
showcode = TRUE,
addcode = NULL,
addaes = NULL,
addarg = NULL,
envir = NULL,
cols = panels,
headings = NULL,
colnames = NULL,
colheadings = colnames,
boldheadings = NULL,
heading.space = NULL,
panel.space = NULL,
label.space = NULL,
plot.space = NULL,
col.right.space = NULL,
col.left.space = NULL,
margin = NULL,
units = NULL
panels,
row.labels = NULL,
row.labels.levels = c("heading1", "heading2", "heading3"),
rows = NULL,
exponentiate = TRUE,
logscale = exponentiate,
panel.names = NULL,
panel.headings = panel.names,
col.key = "key",
col.estimate = "estimate",
col.stderr = "stderr",
col.lci = NULL,
col.uci = NULL,
col.left = NULL,
col.right = NULL,
col.right.parse = FALSE,
col.left.heading = "",
col.right.heading = "HR (95% CI)",
col.left.pos = NULL,
col.right.pos = NULL,
col.left.hjust = 1,
col.right.hjust = 0,
col.heading.space = 0,
estcolumn = TRUE,
col.keep = NULL,
ci.delim = ", ",
digits = 2,
title = "",
xlab = "HR (95% CI)",
xlim = NULL,
xticks = NULL,
nullval = NULL,
blankrows = c(1, 1, 0, 0),
col.diamond = NULL,
diamond = NULL,
col.bold = NULL,
bold.labels = NULL,
scalepoints = FALSE,
minse = NULL,
pointsize = 3,
shape = NULL,
plotcolour = "black",
colour = NULL,
cicolour = colour,
fill = NULL,
ciunder = NULL,
addtext = NULL,
left.space = NULL,
right.space = NULL,
mid.space = unit(5, "mm"),
plot.margin = margin(8, 8, 8, 8, "mm"),
panel.width = NULL,
base_size = 11,
base_line_size = base_size/22,
stroke = 0,
printplot = TRUE,
showcode = TRUE,
addcode = NULL,
addaes = NULL,
addarg = NULL,
envir = NULL,
cols = panels,
headings = NULL,
colnames = NULL,
colheadings = colnames,
boldheadings = NULL,
heading.space = NULL,
panel.space = NULL,
label.space = NULL,
plot.space = NULL,
col.right.space = NULL,
col.left.space = NULL,
margin = NULL,
units = NULL
){

# legacy arguments
Expand Down
28 changes: 2 additions & 26 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -99,42 +99,18 @@ install.packages('ckbplotr',
This will also install dependencies from the CRAN repository.

### From github
The latest version of `ckbplotr` can be installed from
The latest version of `ckbplotr` can also be installed from
github using the `remotes` package.
```{r install from github, eval = FALSE}
install.packages('remotes')
remotes::install_github('neilstats/ckbplotr')
```

If you get an error that reads "Error: Failed to install 'unknown package' from GitHub: HTTP error 404. No commit found for the ref master" then make sure to update to the latest version of the `remotes` package, or try `remotes::install_github('neilstats/ckbplotr@main')`.

If you get an error that reads "Error: (converted from warning) package 'ggplot2' was built under R version ..." you can avoid this by first running `Sys.setenv("R_REMOTES_NO_ERRORS_FROM_WARNINGS" = "true")`. (This is a [known issue](https://github.com/r-lib/remotes/issues/403) with the `remotes` package.)

### Or from source package
`ckbplotr` can also be installed from its source package. The R packages `ggplot2`, `magrittr`, `readr`, `tibble`, `dplyr`, `purrr`, `rlang`, and `ggtext` must first be installed.
```{r install tidyverse, eval = FALSE}
# The easiest way is to install the whole tidyverse and ggtext:
install.packages("tidyverse", "ggtext")
# # Or install just these packages:
# install.packages(c("ggplot2", "readr", "dplyr", "purrr", "ggtext"))
```
Then `ckbplotr` can be installed from its source package using the code:
```{r install from source, eval = FALSE}
install.packages("ckbplotr.tar.gz", repos = NULL, type = "source")
```
Or, in RStudio, open the "Tools" menu and select "Install Packages...".
In the "Install from..." box select "Package Archive File", and in the
"Package archive" box browse to the ckbplotr.tar.gz file.

The source package for the latest release version is available [here](https://github.com/neilstats/ckbplotr/releases/latest).


## Get started

Read `vignette("ckbplotr")` to see how to use the `make_shape_plot()`, `make_forest_plot()`, and `plot_like_ckb()` functions.


## ggplot2 code

The `make_shape_plot()` and `make_forest_plot()` functions return both a plot and the ggplot2 code used to create the plot. In RStudio the ggplot2 code used to create the plot will be shown in the Viewer pane (with syntax highlighting if the [highlights](https://cran.r-project.org/package=highlight) package is installed).
The `make_shape_plot()` and `make_forest_plot()` functions return both a plot and the ggplot2 code used to create the plot. In RStudio the ggplot2 code used to create the plot will be shown in the Viewer pane.
Loading

0 comments on commit 0ba9c74

Please sign in to comment.