concurve graphs functions via
ggplot2, it is quite easy to customize parts of the plot beyond some of the arguments that are provided in the
ggcurve() function. For example, we are able to provide arguments to the function to give custom titles, subtitles, x-axes, y-axes, fills, and colors. However, we could also do this using the standard
ggplot2 grammar. We’ll generate a quick graph to show how.
library(concurve) set.seed(1031) GroupA <- rnorm(500) GroupB <- rnorm(500) RandomData <- data.frame(GroupA, GroupB) intervalsdf <- curve_mean(GroupA, GroupB, data = RandomData, method = "default" ) (function1 <- ggcurve(data = intervalsdf[], type = "c", nullvalue = TRUE))
If we even wanted to provide a custom theme, we could do the following.
library(cowplot) #> #> ******************************************************** #> Note: As of version 1.0.0, cowplot does not change the #> default ggplot2 theme anymore. To recover the previous #> behavior, execute: #> theme_set(theme_cowplot()) #> ******************************************************** logo_file <- "https://res.cloudinary.com/less-likely/image/upload/v1575441662/Site/Logo2.jpg" function1 <- function1 + theme_cowplot() function2 <- ggdraw(function1) + draw_image(logo_file, x = 1, y = 1, hjust = 2, vjust = 1.75, width = 0.13, height = 0.2) function2
I’ve only tried testing this with the
cowplot package, so I cannot say for sure that the functions won’t break when applied with other themes.1
Although the above shows how flexible
ggcurve() is due to the integration with the
ggplot2 framework, we can achieve much more with a newer package called
ggtext.2 If we wanted to fully control how the labels and titles in our graphs looked or insert textboxes with full control, we could easily do that with
Before we used
cowplot, here’s what our plain function looked like (after regenerating it from scratch)
(function1 <- ggcurve(data = intervalsdf[], type = "c", nullvalue = TRUE))
Simple enough, eh?
Okay, but if we wanted to have more fine control over how the title and axes look, we could do that. Here’s how:
We take our existing object function and we specify all the usual options for the titles, axes, etc
(function1 <- ggcurve(data = intervalsdf[], type = "c", nullvalue = TRUE, title = "Something Super Important", xaxis = "Theta"))
But now, instead of doing that above, we’ll use a different style to control the size, form, and color of the text. We’ll load
ggtext and then specify the arguments.
library(ggtext) function1 <- ggcurve(data = intervalsdf[], type = "c", nullvalue = TRUE, title = "Something Super Important", xaxis = "<span style = 'color:#3f8f9b;'>Theta</span> ") function1 + labs( title = "*P*-value Function / Consonance Curve<br><span style = 'font-size:9pt;'> The function below contains <span style = 'color:#3f8f9b;'>nested</span> confidence/compatibility intervals at <span style = 'color:#3f8f9b;'>every possible level</span> (95%, 90%, 75%, 50%) allowing one to see a range of estimates that are <span style = 'color:#3f8f9b;'>consistent with the model and its assumptions</span> that were used to compute the test statistics, *P*-values, and interval estimates. </span>", subtitle = NULL ) + theme( plot.title = element_textbox_simple( size = 11, lineheight = 1.1, linetype = 1, # turn on border box.color = "#748696", # border color fill = "white", # background fill color r = grid::unit(3, "pt"), padding = margin(8, 8, 8, 8), # padding around text inside the box maxwidth = unit(8, "in"), # margin outside the box ), axis.text = element_text(size = 10), axis.title.y = element_text(size = 11), axis.title.x = element_textbox_simple( size = 10, width = NULL, padding = margin(4, 4, 4, 4), margin = margin(4, 0, 0, 0), linetype = 1, r = grid::unit(3, "pt"), box.color = "#748696", # border color fill = "white", # background fill color ) )
As you might have noticed above, we can control the size of the text, the color of it, whether it’s bolded, italicized, etc., and that’s partially because it uses markdown.
The most common way useRs save plots is by going to the plots tab in an IDE like RStudio and clicking “export” and then “save as image” or by using
cowplot has a superior function with far better default options built into it known as
Previously, it was quite difficult to save consonance functions as .svg files or as .pdf files because there was so much information in the graphs that R would often crash. However, this is no longer the case and can easily be done with the
svglite package. Once again, we use the same command from above.
#> R version 4.0.2 (2020-06-22) #> Platform: x86_64-apple-darwin17.0 (64-bit) #> Running under: macOS Catalina 10.15.6 #> #> Matrix products: default #> BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib #> LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib #> #> locale: #>  en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 #> #> attached base packages: #>  stats graphics grDevices utils datasets methods base #> #> other attached packages: #>  gdtools_0.2.2 svglite_22.214.171.124 ggtext_0.1.0 cowplot_1.0.0 #>  ggplot2_3.3.2 concurve_2.7.0 #> #> loaded via a namespace (and not attached): #>  nlme_3.1-148 fs_1.5.0 rprojroot_1.3-2 #>  tools_4.0.2 backports_1.1.8 R6_2.4.1 #>  metafor_2.4-0 colorspace_1.4-1 withr_2.2.0 #>  tidyselect_1.1.0 gridExtra_2.3 curl_4.3 #>  compiler_4.0.2 flextable_0.5.10 xml2_1.3.2 #>  desc_1.2.0 officer_0.3.12 scales_1.1.1 #>  survMisc_0.5.5 pkgdown_1.5.1 systemfonts_0.2.3 #>  stringr_1.4.0 digest_0.6.25 foreign_0.8-80 #>  rmarkdown_2.3 rio_0.5.16 base64enc_0.1-3 #>  pkgconfig_2.0.3 htmltools_0.5.0 rlang_0.4.7 #>  readxl_1.3.1 rstudioapi_0.11 generics_0.0.2 #>  farver_2.0.3 zoo_1.8-8 dplyr_1.0.1 #>  zip_2.0.4 car_3.0-8 magrittr_1.5 #>  Matrix_1.2-18 Rcpp_1.0.5 munsell_0.5.0 #>  abind_1.4-5 lifecycle_0.2.0 stringi_1.4.6 #>  yaml_2.2.1 carData_3.0-4 MASS_7.3-51.6 #>  grid_4.0.2 parallel_4.0.2 forcats_0.5.0 #>  crayon_1.3.4 survminer_0.4.8 lattice_0.20-41 #>  haven_2.3.1 splines_4.0.2 gridtext_0.1.1 #>  hms_0.5.3 magick_2.4.0 knitr_1.29 #>  pillar_1.4.6 ProfileLikelihood_1.1 ggpubr_0.4.0 #>  uuid_0.1-4 boot_1.3-25 markdown_1.1 #>  ggsignif_0.6.0 glue_1.4.1 evaluate_0.14 #>  data.table_1.13.0 vctrs_0.3.2 bcaboot_0.2-1 #>  cellranger_1.1.0 gtable_0.3.0 purrr_0.3.4 #>  tidyr_1.1.1 km.ci_0.5-2 assertthat_0.2.1 #>  xfun_0.16 openxlsx_4.1.5 xtable_1.8-4 #>  broom_0.7.0 rstatix_0.6.0 survival_3.2-3 #>  tibble_3.0.3 pbmcapply_1.5.0 memoise_1.1.0 #>  KMsurv_0.1-5 ellipsis_0.3.1