{"id":15640560,"url":"https://github.com/bradyajohnston/figpatch","last_synced_at":"2025-04-05T23:11:39.065Z","repository":{"id":56861211,"uuid":"373995169","full_name":"BradyAJohnston/figpatch","owner":"BradyAJohnston","description":"Easily Arrange Images with Patchwork Alongside ggplot2 Figures.","archived":false,"fork":false,"pushed_at":"2025-01-22T03:51:31.000Z","size":67018,"stargazers_count":77,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T22:09:48.407Z","etag":null,"topics":["alignment","ggplot2","patchwork","rmarkdown"],"latest_commit_sha":null,"homepage":"https://bradyajohnston.github.io/figpatch","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BradyAJohnston.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-05T01:27:52.000Z","updated_at":"2025-01-21T05:59:38.000Z","dependencies_parsed_at":"2025-02-12T10:11:35.623Z","dependency_job_id":"600f65df-50fd-4fc8-a4f0-b4d1d89b4cec","html_url":"https://github.com/BradyAJohnston/figpatch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BradyAJohnston%2Ffigpatch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BradyAJohnston%2Ffigpatch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BradyAJohnston%2Ffigpatch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BradyAJohnston%2Ffigpatch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BradyAJohnston","download_url":"https://codeload.github.com/BradyAJohnston/figpatch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411239,"owners_count":20934653,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alignment","ggplot2","patchwork","rmarkdown"],"created_at":"2024-10-03T11:37:23.344Z","updated_at":"2025-04-05T23:11:39.049Z","avatar_url":"https://github.com/BradyAJohnston.png","language":"R","readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"90%\", \n  fig.align = \"center\"\n)\n``` \n\n# figpatch\n\n\u003c!-- badges: start --\u003e\n[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)\n[![CRAN status](https://www.r-pkg.org/badges/version/figpatch)](https://CRAN.R-project.org/package=figpatch)\n[![R-CMD-check](https://github.com/BradyAJohnston/figpatch/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/BradyAJohnston/figpatch/actions/workflows/R-CMD-check.yaml)\n\u003c!-- badges: end --\u003e\n\nThe goal of figpatch is to create an easy way to incorporate external figures\nand images into figures assembled with\n[{patchwork}](https://patchwork.data-imaginist.com/).\n\n## Installation\n \nYou can install the released version of figpatch from [CRAN](https://CRAN.R-project.org) with:\n\n``` r\ninstall.packages(\"figpatch\")\n```\n\nInstall the development version from [GitHub](https://github.com/) with:\n\n``` r\n# install.packages(\"devtools\")\ndevtools::install_github(\"BradyAJohnston/figpatch\")\n```\n## Example\n\n```{r example}\nlibrary(figpatch)\nlibrary(ggplot2)\nlibrary(patchwork)\n```\n\nTo use images inside of a patchwork object, they need to be converted to a\n`{ggplot}` object via `fig()`. Once converted, you can assemble the\n`{patchwork}` as you would otherwise with `+ / - * \u0026` or `wrap_plots()`.\n\n```{r figpatch}\n\nimage_path \u003c- system.file(\n  \"extdata\", \n  \"fig.png\", \n  package = \"figpatch\", \n  mustWork = TRUE)\n\nimg \u003c- fig(image_path)\n\nplt \u003c- ggplot(mtcars) + \n  aes(mpg, cyl) + \n  geom_point()\n\nwrap_plots(img, plt, plt, img)\n```\n\nThe `aspect.ratio` of the figs is set to the dimensions of the image, but the\nplots can still resize as you would expect. For each plot that aligns with a\nfig, it's dimensions will match that of the fig (as above). If however it only\naligns on one axis, then the other is free to resize to fill up the total image\nspace (as below).\n\n```{r}\nwrap_plots(plt, img, plt, img, ncol = 2)\n```\n\nIf for some reason you want your fig to also resize (and thus distort your\nimage) then you can specify a particular `aspect.ratio` or let it _be free!_\n\n```{r}\nfree_fig \u003c- fig(image_path, aspect.ratio = \"free\")\n\nwrap_plots(free_fig, plt, ncol = 1)\n```\n\n_Elegant._\n\n### Tagging\nPatchwork already provides support for easy tagging of sub-plots and sub-figures\nusing `plot_annotation()`.\n```{r}\nwrap_plots(img, plt, plt, img) + \n  plot_annotation(tag_levels = \"A\")\n```\n\nFor a lot of figures that include images, tags should be placed on top of the images themselves.\nTagging in {patchwork} currently utilises the ggplot `tag` option \nfrom `ggplot2::labs(tag = ...)` but which currently [doesn't support tagging inside plot borders.](https://github.com/tidyverse/ggplot2/issues/4297)\n\nLets see how it plays out.\n\n#### The assembled figs\n```{r fig.height=2, fig.width=7}\nknitr::opts_chunk$set(fig.height = 2, fig.width = 7)\n```\n\n\n```{r fig.height=2, fig.width=7}\npatchwork::wrap_plots(img, img, img, nrow = 1)\n```\n\n### Scaling the Figs\n\nIf multiple figs have differing dimensions, but but should be scaled the same, you can use `fig_scale()` to scale them all to the maximum width and height of all of the included figs.\n\n#### Without scaling\n```{r}\nfl \u003c- image_path \u003c- system.file(\"extdata\",\n                                package = \"figpatch\",\n                                mustWork = TRUE) %\u003e%\n  list.files(pattern = \"png\",\n             full.names = TRUE)\n\nfl %\u003e% \n  lapply(fig) %\u003e% \n  fig_wrap(ncol = 3)\n```\n\n#### With Scaling\n```{r}\nfl %\u003e% \n  lapply(fig) %\u003e% \n  fig_scale() %\u003e% \n  fig_wrap(ncol = 3)\n```\n\nThe scaling is based on the number of pixels, so while the two chemical structures are now properly scaled, the fig has been reduced significantly. \n\nWe can scale them independently to keep the third fig bigger.\n\n```{r}\nscaled_strctures \u003c- lapply(fl[1:2], fig) %\u003e% \n  fig_scale()\n\nfig_wrap(c(scaled_strctures, list(fig(fl[3]))))\n```\n\n\n### {patchwork} tagging the figs\n```{r}\npatchwork::wrap_plots(img, img, img, nrow = 1) + \n  plot_annotation(tag_levels = \"A\")\n```\n\n\n### {figpatch} tagging the figs\nTo add internal tags to the figs, use the `fig_tag()` function. Assembling with\n{patchwork} can continue as normal.\n\n```{r}\nimg1 \u003c- fig_tag(img, \"A\")\nimg2 \u003c- fig_tag(img, \"(B)\")\nimg3 \u003c- fig_tag(img, \"misc\")\n\npatchwork::wrap_plots(img1, img2, img3, nrow = 1)\n```\n\nA number of default positions can be supplied to `fig_tag(pos = ...)` or a custom\nvector which will place the text in `npc` coordinates (0 to 1 for both `x` and `y`) and\nautomatically adjust for the aspect ratio of the fig.\n\n```{r}\nimg1 \u003c- fig_tag(img, \"A\", pos = \"topright\")\nimg2 \u003c- fig_tag(img, \"(B)\", pos = \"bottomleft\")\nimg3 \u003c- fig_tag(img, \"misc\", pos = c(0.4, 0.9))\n\nwrap_plots(img1, img2, img3, nrow = 1)\n```\n\n## `fig_wrap()`\nTo quickly label and wrap multiple figures, use `fig_wrap()` \n\nTo add borders around individual figures, use `b_*` options inside of `fig_wrap()` \nor specify them individually with `fig()`.\n\n```{r}\nfig_wrap(\n  list(img, img, img),\n  \"A\",\n  prefix = \"(\",\n  suffix = \")\",\n  b_col = \"black\"\n)\n```\n\n\n\nAssembling lots of figures.\n```{r}\nknitr::opts_chunk$set(fig.height = 5, fig.width = 7)\n```\n\n```{r}\nfigs \u003c- lapply(1:9, function(x) img)\n\nfig_wrap(\n  figs,\n  nrow = 3,\n  tag = 1,\n  suffix = \")\",\n  b_col = \"gray20\",\n  b_size = 2\n)\n```\n\nAdjust the padding around plots with `b_margins` and change the unit used with \n`b_unit`.\n```{r}\nfig_wrap(\n  figs,\n  nrow = 3,\n  tag = 1,\n  suffix = \")\",\n  b_col = \"gray20\",\n  b_size = 2, \n  b_margin = ggplot2::margin(8, 8, 8, 8)\n)\n```\n\n\n## Adding specific sub-plot text\nYou can add labels to the text using the `fig_lab()` function. Some\ncustomisations are available. At the end of the day, a `fig()` is just a\n`ggplot` object, and the labels are just the axis titles (x or y). You can add\nyour own `theme()` elements to customise further.\n\n```{r warning=FALSE}\nimg1 \u003c- fig_lab(img1, \"Above is a fig.\")\n\nimg2 \u003c- fig_lab(img2, \"This is an italic label.\", fontface = \"italic\")\n\nimg3 \u003c- fig_lab(img3, \"Below is a fig.\", pos = \"top\")\n\ndesign \u003c- \"AB\n           CC\"\n\nwrap_plots(img1, img2, img3, design = design)\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbradyajohnston%2Ffigpatch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbradyajohnston%2Ffigpatch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbradyajohnston%2Ffigpatch/lists"}