From 9b1dead30791ecef4090280cf532946c84d25ae0 Mon Sep 17 00:00:00 2001 From: neilstats Date: Wed, 11 May 2022 12:39:13 +0100 Subject: [PATCH 1/9] update vignette --- vignettes/make_forest_plot.Rmd | 38 +++++++++++++++------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/vignettes/make_forest_plot.Rmd b/vignettes/make_forest_plot.Rmd index 09bc00a..31ee603 100644 --- a/vignettes/make_forest_plot.Rmd +++ b/vignettes/make_forest_plot.Rmd @@ -298,9 +298,12 @@ p2 <- forestplot2$plot + theme(axis.text.y = element_blank()) ``` -Step 3: Arrange the plots using `gridExtra` (there may be other packages that also work). Adjust `widths` until the plots are the desired widths in your final output file. +Step 3: Arrange the plots using `gridExtra` (there may be other packages that also work), using `fix_panel()` to set the same width for both panels. (Adjust the `widths` argument until you get a suitable layout.) ```{r} -gridExtra::grid.arrange(p1, p2, nrow = 1, widths = c(1, 0.52)) +gridExtra::grid.arrange(fix_panel(p1, width = unit(20, "mm")), + fix_panel(p2, width = unit(20, "mm")), + nrow = 1, + widths = c(1, 0.5)) ``` Note that if `scalepoints = TRUE` (and `minse` is not specified the same for each plot) then this scaling is on a plot-by-plot basis so box sizes are not comparable between plots. However, if different axis scales are used then confidence intervals are not comparable either so this may be not be a problem. @@ -331,17 +334,17 @@ forestplot <- make_forest_plot(panels = list(resultsA, resultsB), xticks = c(0.9, 1, 1.1), blankrows = c(1, 1, 0, 1), scalepoints = TRUE, - pointsize = 16, + pointsize = 8, col.left = c("n"), col.left.heading = c("No. of\nevents"), col.heading.space = 0, # set panel width + CI colours - panel.width = 25, + panel.width = 20, cicolour = c("black", "white"), printplot = FALSE) -plot <- fix_panel(forestplot$plot, width = unit(25, "mm")) +plot <- fix_panel(forestplot$plot, width = unit(20, "mm")) grid::grid.draw(plot) ``` @@ -365,17 +368,16 @@ forestplot <- make_forest_plot(panels = list(resultsA, resultsB), xticks = c(0.9, 1, 1.1), blankrows = c(1, 1, 0, 1), scalepoints = TRUE, - pointsize = 16, + pointsize = 8, col.left = c("n"), col.left.heading = c("No. of\nevents"), - col.heading.space = 0, # set panel width + CI colours - panel.width = 25, + panel.width = 20, cicolour = c("cicol1", "cicol2"), printplot = FALSE) -plot <- fix_panel(forestplot$plot, width = unit(25, "mm")) +plot <- fix_panel(forestplot$plot, width = unit(20, "mm")) grid::grid.draw(plot) ``` @@ -396,20 +398,19 @@ forestplot <- make_forest_plot(panels = list(resultsA, resultsB), xticks = c(0.9, 1, 1.1), blankrows = c(1, 1, 0, 1), scalepoints = TRUE, - pointsize = 18, + pointsize = 8, col.left = c("n"), col.left.heading = c("No. of\nevents"), - col.heading.space = 0, # set panel width + CI under or over - panel.width = 25, + panel.width = 20, shape = 22, stroke = 0.5, fill = "white", ciunder = c(TRUE, FALSE), printplot = FALSE) -plot <- fix_panel(forestplot$plot, width = unit(25, "mm")) +plot <- fix_panel(forestplot$plot, width = unit(20, "mm")) grid::grid.draw(plot) ``` @@ -438,17 +439,12 @@ forestplot <- make_forest_plot(panels = list(resultsA, resultsB), xticks = c(0.9, 1, 1.1), blankrows = c(1, 1, 0, 1), scalepoints = TRUE, - pointsize = 18, + pointsize = 8, col.left = c("n"), - col.left.space = c(0.02), col.left.heading = c("No. of\nevents"), - col.right.space = 0.02, - col.heading.space = 0, - label.space = 2, - panel.space = 8, # set panel width + CI colours - panel.width = 25, + panel.width = 20, shape = "shape", stroke = 0.5, cicolour = c("cicol1", "cicol2"), @@ -456,7 +452,7 @@ forestplot <- make_forest_plot(panels = list(resultsA, resultsB), fill = "white", printplot = FALSE) -plot <- fix_panel(forestplot$plot, width = unit(25, "mm")) +plot <- fix_panel(forestplot$plot, width = unit(20, "mm")) grid::grid.draw(plot) ``` From d32499f9f546bb30cb7ee9082c75e6403eb32f15 Mon Sep 17 00:00:00 2001 From: neilstats Date: Sat, 28 May 2022 15:31:37 +0100 Subject: [PATCH 2/9] add plotcolour argument to make_forest_plot --- DESCRIPTION | 2 +- R/make_forest_plot.R | 38 ++++++++++++++++++++++------------ man/make_forest_plot.Rd | 9 +++++--- vignettes/make_forest_plot.Rmd | 21 +++++++++++++++++++ 4 files changed, 53 insertions(+), 17 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b74c2de..a2de83e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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.4 +Version: 0.6.4.9000 Authors@R: person(given = "Neil", family = "Wright", diff --git a/R/make_forest_plot.R b/R/make_forest_plot.R index 2a6fa34..a8c2861 100644 --- a/R/make_forest_plot.R +++ b/R/make_forest_plot.R @@ -431,9 +431,10 @@ make_forest_data <- function( #' @param pointsize The (largest) size of box to use for plotting point #' estimates. (Default: 3) #' @param shape Shape of points. An integer, or name of a column of integers. (Default will use shape 22 - squares with fill.) -#' @param colour Colour of points. Name of a colour, or name of a column of colour names. (Default will use black.) -#' @param cicolour Colour of CI lines. Colour of CI lines. Name of a colour, or name of a column of colour names. (Default will use black.) -#' @param fill Fill colour of points. Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use black.) +#' @param plotcolour Colour for all parts of the plot. (Default: "black") +#' @param colour Colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour.) +#' @param cicolour Colour of CI lines. Colour of CI lines. Name of a colour, or name of a column of colour names. (Default will use plotcolour.) +#' @param fill Fill colour of points. Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour.) #' @param ciunder Plot CI lines before points. A logical value, or name of a column of logical values. (Default will plot CI lines after points.) #' @param col.diamond Plot estimates and CIs as diamonds. Name of a column of logical values. #' @param diamond Alternative to col.diamond. A character vectors identify the rows @@ -520,6 +521,7 @@ make_forest_plot <- function( minse = NULL, pointsize = 3, shape = NULL, + plotcolour = "black", colour = NULL, cicolour = colour, fill = NULL, @@ -627,9 +629,11 @@ make_forest_plot <- function( shape <- NULL } + plotcolour <- fixq(plotcolour) + cicolour.aes <- NULL if (is.null(cicolour)) { - cicolour <- fixq("black") + cicolour <- plotcolour } else if (all(cicolour %in% names(panels[[1]]))){ cicolour.aes <- fixsp(cicolour) @@ -640,7 +644,7 @@ make_forest_plot <- function( colour.aes <- NULL if (is.null(colour)) { - colour <- fixq("black") + colour <- plotcolour } else if (all(colour %in% names(panels[[1]]))){ colour.aes <- fixsp(colour) colour <- NULL @@ -650,7 +654,7 @@ make_forest_plot <- function( fill.aes <- NULL if (is.null(fill)) { - fill <- fixq("black") + fill <- plotcolour } else if (fill %in% names(panels[[1]])){ fill.aes <- fixsp(fill) fill <- NULL @@ -1028,7 +1032,8 @@ make_forest_plot <- function( arg = c('geom = "segment"', 'y = -0.7, yend = -Inf', sprintf('x = %s, xend = %s', nullval, nullval), - sprintf('size = %s', base_line_size)) + sprintf('size = %s', base_line_size), + sprintf('colour = %s', plotcolour)) ) } @@ -1204,6 +1209,7 @@ make_forest_plot <- function( sprintf('move_x = unit(%s, "%s")', ..2, ..3), sprintf('hjust = %s', ..5), sprintf('size = %s', base_size/(11/3)), + sprintf('colour = %s', plotcolour), 'na.rm = TRUE', sprintf('parse = %s', ..7)), br = FALSE @@ -1216,6 +1222,7 @@ make_forest_plot <- function( arg = c(sprintf('move_x = unit(%s, "%s")', ..2, ..3), sprintf('hjust = %s', ..5), sprintf('size = %s', base_size/(11/3)), + sprintf('colour = %s', plotcolour), 'fontface = "bold"', sprintf('data = dplyr::tibble(panel = factor(%s', paste(deparse(panel.names), collapse = '')), indent(36, @@ -1259,6 +1266,7 @@ make_forest_plot <- function( makeunit(col.right.pos[[1]])), sprintf('hjust = %s', col.right.hjust[[1]]), sprintf('size = %s', base_size/(11/3)), + sprintf('colour = %s', plotcolour), 'na.rm = TRUE', 'parse = TRUE') ) @@ -1293,6 +1301,7 @@ make_forest_plot <- function( sprintf('move_x = unit(-%s, "%s")', ..2, ..3), sprintf('hjust = %s', ..5), sprintf('size = %s', base_size/(11/3)), + sprintf('colour = %s', plotcolour), 'na.rm = TRUE'), br = FALSE ), @@ -1304,6 +1313,7 @@ make_forest_plot <- function( arg = c(sprintf('move_x = unit(-%s, "%s")', ..2, ..3), sprintf('hjust = %s', ..5), sprintf('size = %s', base_size/(11/3)), + sprintf('colour = %s', plotcolour), 'fontface = "bold"', sprintf('data = dplyr::tibble(panel = factor(%s', paste(deparse(panel.names), collapse = '')), indent(36, @@ -1332,6 +1342,7 @@ make_forest_plot <- function( arg = c(addarg$xlab, 'hjust = 0.5', sprintf('size = %s', base_size/(11/3)), + sprintf('colour = %s', plotcolour), 'vjust = 4.4', 'fontface = "bold"', sprintf('data = dplyr::tibble(panel = factor(%s', paste(deparse(panel.names), collapse = '')), @@ -1349,6 +1360,7 @@ make_forest_plot <- function( 'hjust = 0.5', 'nudge_y = 2', sprintf('size = %s', base_size/(11/3)), + sprintf('colour = %s', plotcolour), 'fontface = "bold"', sprintf('data = dplyr::tibble(panel = factor(%s', paste(deparse(panel.names), collapse = '')), indent(36, sprintf('levels = %s', paste(deparse(panel.names), collapse = ''))), @@ -1374,7 +1386,7 @@ make_forest_plot <- function( codetext$theme <- make_layer( '# Control the overall look of the plots', f = 'theme', - arg = c(sprintf('text = element_text(size = %s)', base_size), + arg = c(sprintf('text = element_text(size = %s, colour = %s)', base_size, plotcolour), sprintf('line = element_line(size = %s)', base_line_size), 'panel.background = element_blank()', 'panel.grid.major = element_blank()', @@ -1384,11 +1396,11 @@ make_forest_plot <- function( } else { 'plot.title.position = "plot"' }, - sprintf('axis.line.x = element_line(size = %s, lineend = "round")', - base_line_size), + sprintf('axis.line.x = element_line(colour = %s, size = %s, lineend = "round")', + plotcolour, base_line_size), 'axis.title = element_blank()', - 'axis.ticks.x = element_line(colour = "black")', - 'axis.text.x = element_text(colour = "black"', + sprintf('axis.ticks.x = element_line(colour = %s)', plotcolour), + sprintf('axis.text.x = element_text(colour = %s,', plotcolour), indent(32, sprintf('margin = margin(t = %s)',base_size/(11/4.4)), 'vjust = 1)'), @@ -1396,7 +1408,7 @@ make_forest_plot <- function( 'axis.line.y = element_blank()', 'axis.text.y = ggtext::element_markdown(hjust = 0', indent(44, - 'colour = "black"', + sprintf('colour = %s', plotcolour), sprintf('margin = margin(r = %s, unit = "%s"))', as.numeric(left.space), makeunit(left.space))), 'panel.border = element_blank()', diff --git a/man/make_forest_plot.Rd b/man/make_forest_plot.Rd index e5b20f5..908fef5 100644 --- a/man/make_forest_plot.Rd +++ b/man/make_forest_plot.Rd @@ -46,6 +46,7 @@ make_forest_plot( minse = NULL, pointsize = 3, shape = NULL, + plotcolour = "black", colour = NULL, cicolour = colour, fill = NULL, @@ -188,11 +189,13 @@ estimates. (Default: 3)} \item{shape}{Shape of points. An integer, or name of a column of integers. (Default will use shape 22 - squares with fill.)} -\item{colour}{Colour of points. Name of a colour, or name of a column of colour names. (Default will use black.)} +\item{plotcolour}{Colour for all parts of the plot. (Default: "black")} -\item{cicolour}{Colour of CI lines. Colour of CI lines. Name of a colour, or name of a column of colour names. (Default will use black.)} +\item{colour}{Colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour.)} -\item{fill}{Fill colour of points. Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use black.)} +\item{cicolour}{Colour of CI lines. Colour of CI lines. Name of a colour, or name of a column of colour names. (Default will use plotcolour.)} + +\item{fill}{Fill colour of points. Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour.)} \item{ciunder}{Plot CI lines before points. A logical value, or name of a column of logical values. (Default will plot CI lines after points.)} diff --git a/vignettes/make_forest_plot.Rmd b/vignettes/make_forest_plot.Rmd index 31ee603..cb3e460 100644 --- a/vignettes/make_forest_plot.Rmd +++ b/vignettes/make_forest_plot.Rmd @@ -86,6 +86,27 @@ The function attempts to set the positions of columns of text and spacing automa The plot will fill the vertical space available. Use `plot.margin` to change the top and bottom margins as needed. +## Plot colour +The colour used for the plot can be changed with the `plotcolour` argument. This can be useful to create plots that fit a colour scheme (or use a dark grey for less contrast when viewing on a screen or projector). See the next section for details on customising the colour(s) of point and confidence interval lines. + +```{r plot-colour} +forestplot <- make_forest_plot(panels = list(resultsA, resultsB), + col.key = "variable", + row.labels = ckbplotr_row_labels, + row.labels.levels = c("heading", "subheading", "label"), + rows = c("Triglycerides concentration", + "Lipoprotein particle concentration"), + exponentiate = TRUE, + panel.headings = c("Analysis A", "Analysis B"), + ci.delim = " - ", + xlim = c(0.9, 1.1), + xticks = c(0.9, 1, 1.1), + blankrows = c(1, 1, 0, 1), + scalepoints = TRUE, + pointsize = 3, + plotcolour = "navyblue") +``` + ## Setting colours and shapes, bold text and diamonds From 181f0c4058060efe703571d2fd34ba385bf9d0df Mon Sep 17 00:00:00 2001 From: neilstats Date: Sat, 28 May 2022 15:54:42 +0100 Subject: [PATCH 3/9] add colour and plotcolour arguments to theme_ckb(), plot_like_ckb(), and make_shape_plot() --- R/make_shape_plot.R | 27 +++++++++++++++++---------- R/plot_like_ckb.R | 22 +++++++++++++--------- man/make_shape_plot.Rd | 9 ++++++--- man/plot_like_ckb.Rd | 5 ++++- man/theme_ckb.Rd | 4 +++- vignettes/plot_like_ckb.Rmd | 5 +++++ 6 files changed, 48 insertions(+), 24 deletions(-) diff --git a/R/make_shape_plot.R b/R/make_shape_plot.R index 1b04b96..4c7c09f 100644 --- a/R/make_shape_plot.R +++ b/R/make_shape_plot.R @@ -13,9 +13,10 @@ #' @param col.n Name of column that provides number to be plotted below CIs. #' @param col.group Name of column that groups the estimates. (Default: NULL) #' @param shape Shape of points. An integer, or name of a column of integers. (Default will use shape 22 - squares with fill.) -#' @param colour Colour of points. Name of a colour, or name of a column of colour names. (Default will use black.) -#' @param cicolour Colour of CI lines. Colour of CI lines. Name of a colour, or name of a column of colour names. (Default will use black.) -#' @param fill Fill colour of points. Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use black.) +#' @param plotcolour Colour for non-data aspects of the plot. (Default: "black") +#' @param colour Colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour) +#' @param cicolour Colour of CI lines. Colour of CI lines. Name of a colour, or name of a column of colour names. (Default will use plotcolour) +#' @param fill Fill colour of points. Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour) #' @param ciunder Plot CI lines before points. A logical value, or name of a column of logical values. (Default will plot CI lines after points.) #' @param lines Plot lines (linear fit through estimates, weighted by inverse variance). (Default: FALSE) #' @param exponentiate Exponentiate estimates (and CIs) before plotting, @@ -73,6 +74,7 @@ make_shape_plot <- function(data, pointsize = 3, col.group = NULL, shape = NULL, + plotcolour = "black", colour = NULL, cicolour = colour, fill = NULL, @@ -126,9 +128,11 @@ make_shape_plot <- function(data, shape <- NULL } + plotcolour <- fixq(plotcolour) + cicolour.aes <- NULL if (is.null(cicolour)) { - cicolour <- fixq("black") + cicolour <- plotcolour } else if (cicolour %in% names(data)){ cicolour.aes <- fixsp(cicolour) @@ -139,7 +143,7 @@ make_shape_plot <- function(data, colour.aes <- NULL if (is.null(colour)) { - colour <- fixq("black") + colour <- plotcolour } else if (colour %in% names(data)){ colour.aes <- fixsp(colour) colour <- NULL @@ -149,7 +153,7 @@ make_shape_plot <- function(data, fill.aes <- NULL if (is.null(fill)) { - fill <- fixq("black") + fill <- plotcolour } else if (fill %in% names(data)){ fill.aes <- fixsp(fill) fill <- NULL @@ -266,7 +270,7 @@ make_shape_plot <- function(data, 'method = "glm"', 'formula = y ~ x', 'se = FALSE', - 'colour = "black"', + sprintf('colour = %s', plotcolour), 'linetype = "dashed"', 'size = 0.25') ) @@ -308,7 +312,8 @@ make_shape_plot <- function(data, sprintf('label = format(round(%s, 2), nsmall = 2)', est_string)), arg = c(addarg$estimates, 'vjust = -0.8', - sprintf('size = %s', base_size/(11/3))) + sprintf('size = %s', base_size/(11/3)), + sprintf('colour = %s', plotcolour)) ) @@ -322,7 +327,8 @@ make_shape_plot <- function(data, sprintf('label = %s', col.n)), arg = c(addarg$n, 'vjust = 1.8', - sprintf('size = %s', base_size/(11/3))) + sprintf('size = %s', base_size/(11/3)), + sprintf('colour = %s', plotcolour)) ) } @@ -395,7 +401,8 @@ make_shape_plot <- function(data, sprintf('ext = %s', ext), sprintf('ratio = %s', ratio), sprintf('base_size = %s', base_size), - sprintf('base_line_size = %s', base_line_size)), + sprintf('base_line_size = %s', base_line_size), + sprintf('colour = %s', plotcolour)), plus = TRUE ) diff --git a/R/plot_like_ckb.R b/R/plot_like_ckb.R index 61d5ddf..f96680b 100644 --- a/R/plot_like_ckb.R +++ b/R/plot_like_ckb.R @@ -4,27 +4,29 @@ #' #' @param base_size base font size, given in pts. #' @param base_line_size base size for line elements +#' @param colour Colour for non-data aspects of the plot. (Default: "black") #' #' @export theme_ckb <- function(base_size = 11, - base_line_size = base_size/22){ + base_line_size = base_size/22, + colour = "black"){ theme_bw(base_size = base_size, base_line_size = base_line_size) %+replace% theme(panel.grid = element_blank(), panel.border = element_blank(), - axis.ticks = element_line(colour = "black"), - axis.text = element_text(colour = "black"), + axis.ticks = element_line(colour = colour), + axis.text = element_text(colour = colour), axis.text.x = element_text(margin = margin(t = base_size/(11/4.4)), vjust = 1), axis.text.x.top = element_text(margin = margin(b = base_size/(11/4.4)), vjust = 0), axis.text.y = element_text(margin = margin(r = base_size/(11/4.4)), hjust = 1), axis.text.y.right = element_text(margin = margin(l = base_size/(11/4.4)), hjust = 0), - axis.title = element_text(face = "bold"), + axis.title = element_text(face = "bold", colour = colour), axis.title.x = element_text(margin = unit(c(1,0,0,0), "lines")), axis.title.y = element_text(margin = unit(c(0,1,0,0), "lines"), angle = 90), plot.margin = unit(c(0,0,0.5,0), "lines"), plot.background = element_blank(), - plot.title = element_text(hjust = 0.5, face = "bold"), + plot.title = element_text(hjust = 0.5, face = "bold", colour = colour), complete = TRUE) } @@ -40,6 +42,7 @@ theme_ckb <- function(base_size = 11, #' @param gap A numeric vector of length two. The gap between plotting area and axis to the left and bottom of the plot, as a proportion of the x-axis length. (Default: c(0.025, 0.025)) #' @param ext A numeric vector of length two. The extensions to add to the right and top of the plot, as a proportion of the x-axis length. (Default: c(0.025, 0.025)) #' @param ratio The ratio (y-axis:x-axis) to use for the plot. (Default: 1.5) +#' @param colour Colour for non-data aspects of the plot. (Default: "black") #' #' @return A ggplot2 plot. #' @@ -54,7 +57,8 @@ plot_like_ckb <- function( ext=c(0.025,0.025), ratio=1.5, base_size = 11, - base_line_size = base_size/22 + base_line_size = base_size/22, + colour = "black" ){ # get plot axis transformations @@ -109,7 +113,7 @@ plot_like_ckb <- function( yend = limits[["yaxis"]][[2]], lwd = base_line_size, lineend = "round", - colour = "black") + + colour = colour) + annotate(geom = "segment", x = limits[["xaxis"]][[1]], xend = limits[["xaxis"]][[2]], @@ -117,6 +121,6 @@ plot_like_ckb <- function( yend = limits[["y"]][[1]], lwd = base_line_size, lineend = "round", - colour = "black") + - theme_ckb(base_size = base_size, base_line_size = base_line_size) + colour = colour) + + theme_ckb(base_size = base_size, base_line_size = base_line_size, colour = colour) } diff --git a/man/make_shape_plot.Rd b/man/make_shape_plot.Rd index 2965587..a0728ff 100644 --- a/man/make_shape_plot.Rd +++ b/man/make_shape_plot.Rd @@ -19,6 +19,7 @@ make_shape_plot( pointsize = 3, col.group = NULL, shape = NULL, + plotcolour = "black", colour = NULL, cicolour = colour, fill = NULL, @@ -80,11 +81,13 @@ estimates. (Default: 3)} \item{shape}{Shape of points. An integer, or name of a column of integers. (Default will use shape 22 - squares with fill.)} -\item{colour}{Colour of points. Name of a colour, or name of a column of colour names. (Default will use black.)} +\item{plotcolour}{Colour for non-data aspects of the plot. (Default: "black")} -\item{cicolour}{Colour of CI lines. Colour of CI lines. Name of a colour, or name of a column of colour names. (Default will use black.)} +\item{colour}{Colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour)} -\item{fill}{Fill colour of points. Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use black.)} +\item{cicolour}{Colour of CI lines. Colour of CI lines. Name of a colour, or name of a column of colour names. (Default will use plotcolour)} + +\item{fill}{Fill colour of points. Fill colour of points. Name of a colour, or name of a column of colour names. (Default will use plotcolour)} \item{ciunder}{Plot CI lines before points. A logical value, or name of a column of logical values. (Default will plot CI lines after points.)} diff --git a/man/plot_like_ckb.Rd b/man/plot_like_ckb.Rd index 64b2a44..3a59319 100644 --- a/man/plot_like_ckb.Rd +++ b/man/plot_like_ckb.Rd @@ -12,7 +12,8 @@ plot_like_ckb( ext = c(0.025, 0.025), ratio = 1.5, base_size = 11, - base_line_size = base_size/22 + base_line_size = base_size/22, + colour = "black" ) } \arguments{ @@ -31,6 +32,8 @@ plot_like_ckb( \item{base_size}{base font size, given in pts.} \item{base_line_size}{base size for line elements} + +\item{colour}{Colour for non-data aspects of the plot. (Default: "black")} } \value{ A ggplot2 plot. diff --git a/man/theme_ckb.Rd b/man/theme_ckb.Rd index 2648d38..c87a45f 100644 --- a/man/theme_ckb.Rd +++ b/man/theme_ckb.Rd @@ -4,12 +4,14 @@ \alias{theme_ckb} \title{CKB ggplot theme} \usage{ -theme_ckb(base_size = 11, base_line_size = base_size/22) +theme_ckb(base_size = 11, base_line_size = base_size/22, colour = "black") } \arguments{ \item{base_size}{base font size, given in pts.} \item{base_line_size}{base size for line elements} + +\item{colour}{Colour for non-data aspects of the plot. (Default: "black")} } \description{ Based on theme_bw diff --git a/vignettes/plot_like_ckb.Rmd b/vignettes/plot_like_ckb.Rmd index 78d7dd7..f7b8a1f 100644 --- a/vignettes/plot_like_ckb.Rmd +++ b/vignettes/plot_like_ckb.Rmd @@ -68,6 +68,11 @@ plot_like_ckb(plot = plot, xlims = c(0, 8), ylims = c(10, 50), ratio = 0.3) ### Modifying the appearance of the plot +The `colour` arguments of `theme_ckb()` and `plot_like_ckb()` can be used to change the colour of the non-data components of the plot. +```{r colour} +plot_like_ckb(plot = plot, xlims = c(0, 8), ylims = c(10, 50), colour = "darkred") +``` + If you wish to override some aspect of the theme applied by `plot_like_ckb`, then this can be done by adding a theme after the function. ```{r plot_like_ckb-example-3} From b66c01e6577f44570854533607c40d6386dcab13 Mon Sep 17 00:00:00 2001 From: neilstats Date: Fri, 3 Jun 2022 22:29:53 +0100 Subject: [PATCH 4/9] use rmarkdown to render plot code to display in viewer --- DESCRIPTION | 7 +++---- R/make_forest_plot.R | 14 ++++++-------- R/utils.R | 39 +++++++++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a2de83e..2612017 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,13 +25,12 @@ Imports: utils, stringi, grid, - ggtext -RoxygenNote: 7.1.2 -Suggests: + ggtext, knitr, rmarkdown, +RoxygenNote: 7.1.2 +Suggests: gridExtra, - highlight, covr, testthat VignetteBuilder: knitr diff --git a/R/make_forest_plot.R b/R/make_forest_plot.R index a8c2861..1a99b28 100644 --- a/R/make_forest_plot.R +++ b/R/make_forest_plot.R @@ -714,7 +714,7 @@ make_forest_plot <- function( ## calculate automatic col.right.pos and col.right.space if (is.null(right.space) | is.null(col.right.pos) | is.null(left.space) | is.null(col.left.pos)){ - codetext$spacing <- "## Automatically calculated horizontal spacing and positioning:" + text_auto_spacing <- "Automatically calculated horizontal spacing and positioning:\n" } ### get maximum width of each columns (incl. heading) colspaces <- gettextwidths(lapply(col.right, function(y) c(sapply(panels, function(x) x[[y]])))) @@ -741,12 +741,12 @@ make_forest_plot <- function( colspaceauto <- round(0.8 * base_size/grid::get.gpar()$fontsize * colspaceauto, 1) if (is.null(right.space)){ right.space <- unit(colspaceauto[length(colspaceauto)], "mm") - codetext$spacing <- c(codetext$spacing, paste0("## right.space = ", printunit(right.space))) + text_auto_spacing <- c(text_auto_spacing, paste0("- right.space = ", printunit(right.space))) } if (length(colspaceauto) > 1){colspaceauto <- colspaceauto[-length(colspaceauto)]} if (is.null(col.right.pos)){ col.right.pos <- unit(colspaceauto, "mm") - codetext$spacing <- c(codetext$spacing, paste0("## col.right.pos = ", printunit(col.right.pos))) + text_auto_spacing <- c(text_auto_spacing, paste0("- col.right.pos = ", printunit(col.right.pos))) } ## calculate automatic col.left.pos and col.left.space @@ -766,12 +766,12 @@ make_forest_plot <- function( colspaceauto <- round(0.8 * base_size/grid::get.gpar()$fontsize * colspaceauto, 1) if (is.null(left.space)){ left.space <- unit(colspaceauto[length(colspaceauto)], "mm") - codetext$spacing <- c(codetext$spacing, paste0("## left.space = ", printunit(left.space))) + text_auto_spacing <- c(text_auto_spacing, paste0("- left.space = ", printunit(left.space))) } if (length(colspaceauto) > 1){colspaceauto <- colspaceauto[-length(colspaceauto)]} if (is.null(col.left.pos)){ col.left.pos <- unit(colspaceauto, "mm") - codetext$spacing <- c(codetext$spacing, paste0("## col.left.pos = ", printunit(col.left.pos))) + text_auto_spacing <- c(text_auto_spacing, paste0("- col.left.pos = ", printunit(col.left.pos))) } @@ -1433,8 +1433,6 @@ make_forest_plot <- function( # Create the plot code plotcode <- c( - codetext$spacing, - '', 'library(ggplot2)', '', codetext$prep.data, @@ -1469,7 +1467,7 @@ make_forest_plot <- function( } # Show code in RStudio viewer. - if (showcode){ displaycode(plotcode) } + if (showcode){ displaycode(plotcode, text_auto_spacing) } # Create plot and print diff --git a/R/utils.R b/R/utils.R index ed7dca6..de39a7f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -85,23 +85,34 @@ make_layer <- function(name = NULL, f, aes = NULL, arg = NULL, plus = TRUE, br = #' @keywords internal #'@noRd #' -displaycode <- function(plotcode){ - writeLines(paste(c('# Generated plot code ------------------', - '', - plotcode), - collapse = "\n"), - file.path(tempdir(), "plotcode.txt")) - codefile <- file.path(tempdir(), "plotcode.txt") - - if (requireNamespace("highlight", quietly = TRUE)) { - highlight::highlight(file.path(tempdir(), "plotcode.txt"), - output = file.path(tempdir(), "highlight-plotcode.html"), - renderer = highlight::renderer_html(document = TRUE)) - codefile <- file.path(tempdir(), "highlight-plotcode.html") +displaycode <- function(plotcode, note = ""){ + + if (!is.null(knitr::opts_knit$get("out.format"))){ + return(NULL) } + writeLines( + c("---", + "title: 'Generated R code'", + "output:", + " html_document:", + " highlight: kate", + "---", + "```{css, echo=FALSE}", + ".no-border {border: 0px;}", + "```", + note, + "```{r plotcode class.source='no-border', eval = FALSE}", + plotcode, + "```"), + file.path(tempdir(), "plotcode.Rmd"), + sep = "\n") + + rmarkdown::render(file.path(tempdir(), "plotcode.Rmd"), + output_file = "plotcode.html", + quiet = TRUE) viewer <- getOption("viewer", default = function(url){}) - viewer(codefile) + viewer(file.path(tempdir(), "plotcode.html")) } From b15a8f2bb2055d011d24fb0136af44c8c2a3b4b2 Mon Sep 17 00:00:00 2001 From: neilstats Date: Fri, 3 Jun 2022 22:57:10 +0100 Subject: [PATCH 5/9] fix adding theme in make_shape_plot --- R/make_shape_plot.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/make_shape_plot.R b/R/make_shape_plot.R index 4c7c09f..68b266f 100644 --- a/R/make_shape_plot.R +++ b/R/make_shape_plot.R @@ -410,7 +410,7 @@ make_shape_plot <- function(data, codetext$theme <- make_layer( '# Add theme', f = "theme", - arg = c(sprintf('legend.position = %s', ds(legend.position))), + arg = c(sprintf('legend.position = %s', deparse(legend.position))), plus = FALSE ) @@ -428,8 +428,7 @@ make_shape_plot <- function(data, codetext$cis.after, codetext$scales, codetext$axes, - codetext$titles, - codetext$theme), + codetext$titles), codetext$plot.like.ckb, indent(2, codetext$theme) ) From 4798755f79c44512cbdfb1ae0040bdd07d8cf38b Mon Sep 17 00:00:00 2001 From: neilstats Date: Fri, 3 Jun 2022 23:35:39 +0100 Subject: [PATCH 6/9] fix --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index de39a7f..d440127 100644 --- a/R/utils.R +++ b/R/utils.R @@ -101,7 +101,7 @@ displaycode <- function(plotcode, note = ""){ ".no-border {border: 0px;}", "```", note, - "```{r plotcode class.source='no-border', eval = FALSE}", + "```{r plotcode, class.source='no-border', eval = FALSE}", plotcode, "```"), file.path(tempdir(), "plotcode.Rmd"), From b2c23779afb1406e2a49ad8491aeb6d76a8f7682 Mon Sep 17 00:00:00 2001 From: neilstats Date: Fri, 3 Jun 2022 23:36:00 +0100 Subject: [PATCH 7/9] add example of labelling groups --- vignettes/make_shape_plot.Rmd | 40 ++++++++++++++++++++++++++++++----- vignettes/plot_like_ckb.Rmd | 2 +- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/vignettes/make_shape_plot.Rmd b/vignettes/make_shape_plot.Rmd index 4e530c1..65aaec2 100644 --- a/vignettes/make_shape_plot.Rmd +++ b/vignettes/make_shape_plot.Rmd @@ -44,7 +44,7 @@ plot <- make_shape_plot(ckbplotr_shape_data[ckbplotr_shape_data$is_female == 0,] ## Using groups -The `col.group` argument can be supplied to plot results for different groups (using shades of grey for the fill colour). (Set `ciunder` to `TRUE` so that the confidence interval lines are hidden behind behind the point estimate squares.) +The `col.group` argument can be supplied to plot results for different groups (using shades of grey for the fill colour). (Set `ciunder` to `TRUE` so that the confidence interval lines are hidden behind behind the point estimate squares.) Use the `legend.position` to set the legend position. ```{r groups, fig.height = 4, fig.width = 4, out.width = "50%"} plot <- make_shape_plot(ckbplotr_shape_data, col.x = "rf", @@ -56,10 +56,40 @@ plot <- make_shape_plot(ckbplotr_shape_data, ylims = c(0.5, 3), scalepoints = TRUE, title = NULL, - ciunder = TRUE) + ciunder = TRUE, + legend.position = "bottom") ``` +### Labelling groups +To label points directly, you could add a `geom_text()` to the plot: +```{r group-labels, fig.height = 4, fig.width = 4, out.width = "50%"} +data_to_plot <- ckbplotr_shape_data %>% + dplyr::mutate(is_female = factor(is_female, + levels = c(0, 1), + labels = c("Men", "Women"))) + +shape_plot <- make_shape_plot(data_to_plot, + col.x = "rf", + col.estimate = "est", + col.stderr = "se", + col.n = "n", + col.group = "is_female", + xlims = c(15,50), + ylims = c(0.5, 3), + scalepoints = TRUE, + title = NULL, + ciunder = TRUE, + legend.position = "none", + printplot = FALSE) + +shape_plot$plot + + geom_text(aes(label = is_female), + hjust = 0, + nudge_x = 2.5, + data = ~ dplyr::group_by(., is_female) %>% dplyr::filter(rf == max(rf))) +``` + ## Adding lines The `lines` argument will add lines (linear fit through estimates on plotted scale, weighted by inverse variance) for each group. ```{r lines, fig.height = 4, fig.width = 4, out.width = "50%"} @@ -96,7 +126,7 @@ The argument/columns, what they control, and the type: ### Using values If the argument doesn't match the name of a column in the data, then the value will be used for all points. -```{r aesthetics with values, fig.height = 4, fig.width = 4, out.width = "50%"} +```{r aesthetics-with-values, fig.height = 4, fig.width = 4, out.width = "50%"} plot <- make_shape_plot(ckbplotr_shape_data, col.x = "rf", col.estimate = "est", @@ -115,7 +145,7 @@ plot <- make_shape_plot(ckbplotr_shape_data, ### Using columns If the argument matches a column name, then the values in the column will be used. -```{r aesthetics with columns, fig.height = 4, fig.width = 4, out.width = "50%"} +```{r aesthetics-with-columns, fig.height = 4, fig.width = 4, out.width = "50%"} ckbplotr_shape_data$fillcol <- "black" ckbplotr_shape_data[ckbplotr_shape_data$is_female == 1,]$fillcol <- "orange" @@ -134,7 +164,7 @@ plot <- make_shape_plot(ckbplotr_shape_data, ``` Specify a column for `cicolour` is useful for changing the colour of confidence interval lines that would otherwise be hidden. -```{r small ci, fig.height = 4, fig.width = 4, out.width = "50%"} +```{r small-ci, fig.height = 4, fig.width = 4, out.width = "50%"} ckbplotr_shape_data$cicol <- "black" ckbplotr_shape_data[2,]$cicol <- "white" diff --git a/vignettes/plot_like_ckb.Rmd b/vignettes/plot_like_ckb.Rmd index f7b8a1f..6126841 100644 --- a/vignettes/plot_like_ckb.Rmd +++ b/vignettes/plot_like_ckb.Rmd @@ -62,7 +62,7 @@ plot_like_ckb(plot = plot, xlims = c(0, 8), ylims = c(10, 50), gap = c(0, 0)) ### Aspect ratio Change the aspect ratio of the plot. -```{r aspect ratio, fig.width = 6} +```{r aspect-ratio, fig.width = 6} plot_like_ckb(plot = plot, xlims = c(0, 8), ylims = c(10, 50), ratio = 0.3) ``` From fbe5b85596c5e22470bc8c3971f23537cc71d479 Mon Sep 17 00:00:00 2001 From: neilstats Date: Mon, 6 Jun 2022 17:33:51 +0100 Subject: [PATCH 8/9] update NEWS.md --- NEWS.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NEWS.md b/NEWS.md index 211cbd2..8e52436 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# 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. + # ckbplotr 0.6.4 * Added legend.name and legend.position arguments to make_shape_plot(). From 1181c0102092953611ecce8695cce83c62771ea5 Mon Sep 17 00:00:00 2001 From: neilstats Date: Mon, 6 Jun 2022 17:34:12 +0100 Subject: [PATCH 9/9] update version number --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2612017..8cd4e15 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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.4.9000 +Version: 0.6.5 Authors@R: person(given = "Neil", family = "Wright",