{"id":13712047,"url":"https://github.com/coolbutuseless/ggsvg","last_synced_at":"2025-05-07T09:25:15.049Z","repository":{"id":41412934,"uuid":"441160044","full_name":"coolbutuseless/ggsvg","owner":"coolbutuseless","description":"Use SVG images as ggplot points","archived":false,"fork":false,"pushed_at":"2024-09-11T12:38:03.000Z","size":3267,"stargazers_count":142,"open_issues_count":7,"forks_count":5,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-31T08:38:45.702Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://coolbutuseless.github.io/package/ggsvg/","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/coolbutuseless.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-12-23T11:33:25.000Z","updated_at":"2025-03-22T10:40:52.000Z","dependencies_parsed_at":"2024-10-26T20:28:53.342Z","dependency_job_id":"a91496ce-d4fb-4eeb-b60b-642bf07419ef","html_url":"https://github.com/coolbutuseless/ggsvg","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/coolbutuseless%2Fggsvg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fggsvg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fggsvg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fggsvg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coolbutuseless","download_url":"https://codeload.github.com/coolbutuseless/ggsvg/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252849212,"owners_count":21813786,"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":[],"created_at":"2024-08-02T23:01:14.368Z","updated_at":"2025-05-07T09:25:15.025Z","avatar_url":"https://github.com/coolbutuseless.png","language":"R","funding_links":[],"categories":["Plot layers","ggplot"],"sub_categories":["Miscellaneous"],"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 = \"100%\",\n  fig.width  = 6,\n  fig.height = 4\n)\n\nlibrary(ggplot2)\nlibrary(ggsvg)\n\nset.seed(1)\n\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n# Generate the pkgdown documentation\n#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nif (FALSE) {\n  pkgdown::build_site(override = list(destination = \"../coolbutuseless.github.io/package/ggsvg\"))\n}\n```\n\n# ggsvg - Use SVG as points in ggplot  \u003cimg src=\"man/figures/logo-ggsvg.png\" align=\"right\" width=\"230\"/\u003e\n\n\u003c!-- badges: start --\u003e\n![](https://img.shields.io/badge/cool-useless-green.svg)\n[![R-CMD-check](https://github.com/coolbutuseless/ggsvg/workflows/R-CMD-check/badge.svg)](https://github.com/coolbutuseless/ggsvg/actions)\n\u003c!-- badges: end --\u003e\n\n`ggsvg` is an extension to ggplot to use SVG images for points.\n\nVariables may be aesthetically mapped to features within the SVG using CSS selectors via\nthe `css()` helper function.\n\n\n## What's in the box\n\n* `geom_point_svg()` for plotting points with SVG as the glyph (This is a direct\n   analogue to `geom_point()`)\n* `scale_svg_*()` functions for controlling the aesthetic mapping.\n    * `scale_svg_default()` is a sensible default for most plots.\n    * `scale_svg_*` are a shadow set of `ggplot2::scale_*()` functions with\n      adaptations needed for `css()` selectors as aesthetics.\n    * E.g. `scale_svg_fill_brewer()` is a direct analogue for\n      `ggplot2::scale_fill_brewer()`\n\n## Installation\n\nInstall from [GitHub](https://github.com/coolbutuseless/ggsvg).\n\nThe [`{rsvg}`](https://github.com/ropensci/rsvg) package is used \nto convert SVG into an R raster object.  This requires at least rsvg(\u003e= 2.3.0).\n\n``` r\n# install.package('remotes')\ninstall.packages('rsvg')\nremotes::install_github('coolbutuseless/ggsvg')\n```\n\n\n# Simple plot\n\n```{r fig.height=1, eval=FALSE}\nsvg_url \u003c- 'https://www.svgrepo.com/download/289000/jellyfish.svg'\nsvg_txt \u003c- paste(readLines(svg_url), collapse = \"\\n\")\n```\n\n\n```{r eval=TRUE, echo=FALSE}\n# Local cache\nsvg_txt \u003c- paste(readLines(\"man/figures/test.svg\"), collapse = \"\\n\")\n```\n\n\n\n```{r fig.height=1}\ngrid::grid.draw( svg_to_rasterGrob(svg_txt) )\n```\n\n\n```{r}\ntest_df \u003c- data.frame(\n  x = runif(10), \n  y = runif(10), \n  count = sample(3:5, 10, T),\n  type  = sample(c('a', 'b', 'c'), 10, T))\n\ntest_df\n\nggplot(test_df) + \n  geom_point_svg(aes(x, y), svg = svg_txt) + \n  theme_bw()\n```\n\n\n# Simple plot with mapped `size` aesthetic\n\n\n```{r  warning=FALSE}\nggplot(test_df) + \n  geom_point_svg(aes(x, y, size = type), svg = svg_txt) + \n  theme_bw()\n```\n\n\n\n## Mapping Aesthetics to SVG features with CSS Selectors\n\nAesthetic values are mapped to SVG features with\n[CSS Selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).\n\n#### Snowman SVG\n\nHere is a simple SVG consisting of 2 stacked circles - a big circle on the \nbottom and a small circle resting on top.\n\n```{r fig.height=1}\nsnowman_txt \u003c- '\n  \u003csvg viewBox=\"0 0 100 100 \"\u003e\n    \u003ccircle id=\"top\" cx=\"50\" cy=\"20\" r=\"20\" fill=\"brown\" stroke=\"black\" /\u003e\n    \u003ccircle id=\"bot\" cx=\"50\" cy=\"70\" r=\"30\" fill=\"brown\" stroke=\"black\" /\u003e\n  \u003c/svg\u003e\n  '\n\ngrid::grid.draw( svg_to_rasterGrob(snowman_txt, width=800, height=800) )\n```\n\n\n#### `css()` helper function\n\nUse the `css()` helper function to target aesthetics at selected elements within \nand SVG using `css(selector, property = value)`\n\nE.g. \n\n* **`css(\"rect.big\", stroke = x)`**\n    * Targets `\u003crect\u003e` elements with `class = \"big\"`\n    * Map values in `x` in data.frame to the SVG `stroke` property for these targetted elements.\n\n#### Example\nIn the following example, two `css()` selectors are used within the `geom_point_svg()` call:\n\n* **`css(\"circle#top\", fill=type)`**\n    * Targets `\u003ccircle\u003e` elements with `id = \"top\"`\n    * Map values in `type` in data.frame to the SVG `fill` property for these targetted\n      elements.\n      \n* **`css(\"circle#bot\", stroke='brown')`**\n    * Targets `\u003ccircle\u003e` elements with `id = \"bot\"`\n    * Set a constant value of `brown` for the SVG `stroke` property for these targetted\n      elements.\n      \n* **`css(\"circle\", 'stroke-width'=10)`**\n    * Targets `\u003ccircle\u003e` elements\n    * Set a constant value of `5` for the SVG `stroke-wdith` property for these targetted\n      elements.\n\n\nTo configure how the variable is mapped to the property on the selected target,\nyou can either use:\n\n* `scale_svg_default()` for reasonable defaults\n* `scale_svg_*()` family of functions\n    * Note: the `aesthetic` argument must match exactly the\n      `css(...)` call used in the `geom_point_svg()` call.\n\n\n```{r message = FALSE}\nsnowman_txt \u003c- '\n  \u003csvg viewBox=\"0 0 100 100 \"\u003e\n    \u003ccircle id=\"top\" cx=\"50\" cy=\"20\" r=\"20\" fill=\"brown\" stroke=\"black\" /\u003e\n    \u003ccircle id=\"bot\" cx=\"50\" cy=\"70\" r=\"30\" fill=\"brown\" stroke=\"black\" /\u003e\n  \u003c/svg\u003e\n  '\n\n\nggplot(test_df) + \n  geom_point_svg(\n    aes(x, y, css(\"circle#top\", fill = type)),\n    css(\"circle#bot\", stroke = 'brown'),\n    css(\"circle\", 'stroke-width'=10),\n    svg = snowman_txt\n  ) +\n  theme_bw() + \n  scale_svg_fill_brewer(aesthetics = css(\"circle#top\", fill = type), palette = 'Dark2')\n```\n\n\n\n\n\n\n\n\n## Acknowledgements\n\n* R Core for developing and maintaining the language.\n* CRAN maintainers, for patiently shepherding packages onto CRAN and maintaining\n  the repository\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolbutuseless%2Fggsvg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoolbutuseless%2Fggsvg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolbutuseless%2Fggsvg/lists"}