--- title: "Customising plots" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Customising plots} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 4, fig.height = 3, fig.align = "center", out.width = '60%', dpi = 120, message = FALSE ) library(ckbplotr) my_results <- data.frame( subgroup = c("men", "women", "35_49", "50_64", "65_79"), est = c( 0.45, 0.58, 0.09, 0.35, 0.6), se = c( 0.07, 0.06, 0.06, 0.05, 0.08) ) my_results_2 <- data.frame( x = c( 17, 20, 23.5, 25, 29), est = c( 0, 0.069, 0.095, 0.182, 0.214), se = c(0.05, 0.048, 0.045, 0.045, 0.081) ) ``` There are several approaches to customising the plots produced by `shape_plot()`, `forest_plot()` and `volcano_plot()`. These are, in order of increasing complexity: 1. [Using arguments](#using-arguments) 2. [Adding aesthetics and arguments](#adding-aesthetics-and-arguments) 3. [Adding to the plot](#adding-to-the-plot) 4. [Replacing or removing parts of the plot](#replacing-or-removing-parts-of-the-plot) 5. [Changing the forest plot data before plotting](#the-data-function-argument) ## Using arguments There are several key arguments available for customising plots. Most of these arguments can accept either a fixed value or the name of a column in the supplied data frame. ### Font and line size - **`base_size`** sets the base font size. - **`base_line_size`** sets the base line size. ### Colours - **`plotcolour`** sets the colour of non-data elements of the plot. - **`colour`** sets the colour of points in `shape_plot()` and the colour of points and lines in `forest_plot()`. - **`cicolour`** sets the colour of confidence interval (CI) lines in `shape_plot()` - **`fill`** sets the fill colour of points. This only has an effect when using a shape that supports filling, such as `"square filled"`. - **`point_colours`** sets the colours of points according to their statistical significance and the sign of their estimates in `volcano_plot()`. ### Shape and other appearance options - **`shape`** sets the shape used for points. - **`col.bold`** in `forest_plot()` specifies whether text is displayed in bold. - **`col.diamond`** in `forest_plot()` specifies whether an estimate and its CI are displayed as a diamond. Note that `col.bold` and `col.diamond` must be specified as column names. Diamonds can also be specified using the `diamond` argument, which accepts a character vector of keys identifying the estimates to display as diamonds. ## Adding aesthetics and arguments The `addaes` arguments can be used to modify aesthetics in the ggplot layers created by the functions, and the `addarg` argument can be used to modify arguments. Both `addaes` and `addarg` must be named lists of character strings, and the names of the elements define where the aesthetics/arguments code is added. The required names are shown in brackets in the comments of the generated plot code e.g. `# Plot the point estimates [points]`. For example, adjusting the position and appearance of panel headings: ```{r, include = FALSE} row_labels <- data.frame( subgroup = c("women", "men", "65_79", "50_64", "35_49"), group = c("Sex", "Sex", "Age (years)", "Age (years)", "Age (years)"), label = c("Women", "Men", "65 - 79", "50 - 64", "35 - 49") ) my_resultsA <- data.frame( subgroup = c("men", "women", "35_49", "50_64", "65_79"), est = c( 0.45, 0.58, 0.09, 0.35, 0.6), se = c( 0.07, 0.06, 0.06, 0.05, 0.08) ) my_resultsB <- data.frame( subgroup = c("men", "women", "35_49", "50_64", "65_79"), est = c(0.48, 0.54, 0.06, 0.3, 0.54), se = c(0.12, 0.11, 0.11, 0.09, 0.15) ) ``` ```{r, fig.width=6.5, out.width='75%', warning=FALSE} forest_plot(list("a) Observational" = my_resultsA, "b) Genetic" = my_resultsB), col.key = "subgroup", row.labels = row_labels, addaes = list(panel.headings = "x = 0.5"), addarg = list(panel.headings = c("size = 4.5", "colour = 'navyblue'", "hjust = 0"))) ``` Changing the appearance of the line at the null value: ```{r, fig.width=6.5, out.width='75%', warning=FALSE} forest_plot(list("a) Observational" = my_resultsA, "b) Genetic" = my_resultsB), col.key = "subgroup", row.labels = row_labels, addarg = list(nullline = c("linetype = 'dashed'", "colour = 'darkorange'"))) ``` ## Adding to the plot You can add additional ggplot objects to the plot with the `add` argument. The argument should be a named list. Use the name `start` to include objects in the ggplot immediately after `ggplot()` (i.e. before anything else is added to the ggplot) and use `end` to add to the end. For example, to add additional geoms to the plot: ```{r} hr_geom <- geom_text(aes(label = auto_estcolumn), size = 3, hjust = 0, nudge_y = 0.25, colour = "red") forest_plot(my_results, estcolumn = FALSE, add = list(start = hr_geom)) ``` Or to use `theme` to customise the plot: ```{r} my_theme <- theme(axis.text.x = element_text(colour = "purple", angle = 45, hjust = 1)) forest_plot(my_results, add = list(end = my_theme)) ``` To add multiple objects, use a list: ```{r} my_parts <- list(hr_geom, my_theme) forest_plot(my_results, estcolumn = FALSE, add = list(end = my_parts)) ``` ## Replacing or removing parts of the plot The `add` argument can also be used to replace parts of the plot by using the names shown in brackets in the comments of the generated plot code. To remove a part of a plot, replace it with an empty list, for example: ```{r} shape_plot(my_results_2, add = list(estimates.text = list())) ``` ## The data.function argument Use the `data.function` argument in `forest_plot()` to change the plot data immediately before plotting. For example, you can tweak the text in the generated estimates column: ```{r} my_func <- function(dfr) { dfr$auto_estcolumn <- sub("\\(", "[", dfr$auto_estcolumn) dfr$auto_estcolumn <- sub("\\)", "]", dfr$auto_estcolumn) return(dfr) } forest_plot(my_results, data.function = "my_func") ```