{"id":13711104,"url":"https://nsgrantham.github.io/ggbraid/","last_synced_at":"2025-05-06T20:31:51.399Z","repository":{"id":40337010,"uuid":"462135820","full_name":"nsgrantham/ggbraid","owner":"nsgrantham","description":"Braided ribbons in ggplot2","archived":false,"fork":false,"pushed_at":"2022-12-31T02:51:43.000Z","size":24134,"stargazers_count":73,"open_issues_count":5,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-03-31T23:34:50.189Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://nsgrantham.github.io/ggbraid/","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/nsgrantham.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-22T04:21:59.000Z","updated_at":"2024-03-10T02:25:37.000Z","dependencies_parsed_at":"2023-01-31T17:46:04.463Z","dependency_job_id":null,"html_url":"https://github.com/nsgrantham/ggbraid","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/nsgrantham%2Fggbraid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsgrantham%2Fggbraid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsgrantham%2Fggbraid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsgrantham%2Fggbraid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nsgrantham","download_url":"https://codeload.github.com/nsgrantham/ggbraid/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224528339,"owners_count":17326345,"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:04.497Z","updated_at":"2024-11-13T21:31:29.439Z","avatar_url":"https://github.com/nsgrantham.png","language":"R","funding_links":[],"categories":["Plot layers"],"sub_categories":[],"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  dpi = 300\n)\n```\n\n# ggbraid \u003cimg src=\"man/figures/logo.png\" style=\"float:right\" width=\"200\"/\u003e\n\n\u003c!-- badges: start --\u003e\n\n\u003c!-- badges: end --\u003e\n\nggbraid provides a new stat, `stat_braid()`, that extends the functionality of `geom_ribbon()` to correctly fill the area between two alternating lines (or steps) with two different colors. ggbraid also provides a geom, `geom_braid()`, that wraps `geom_ribbon()` and uses `stat_braid()` by default.\n\n## Installation\n\nYou can install the development version of ggbraid from GitHub with:\n\n``` r\n# install.packages(\"remotes\")\nremotes::install_github(\"nsgrantham/ggbraid\")\n```\n\n## Usage\n\nTo demonstrate, let's generate a long dataset with two alternating series.\n\n```{r example}\nlibrary(ggplot2)\nlibrary(ggbraid)\nlibrary(tidyr)\n\nset.seed(42)  # for reproducibility\n\nn \u003c- 21\n\ndf_long \u003c- tibble(\n  x = c(1:n, 1:n),\n  y = c(rnorm(n), rnorm(n, mean = 0.5)),\n  z = c(rep(\"a\", n), rep(\"b\", n))\n)\n\ndf_long\n```\n\nAnd let's pivot the dataset wider so we can use it with `geom_ribbon()` and `geom_braid()`.\n\n```{r pivot-wider}\ndf_wide \u003c- pivot_wider(df_long, names_from = z, values_from = y)\n\ndf_wide\n```\n\nNow let's draw the two series as lines and fill the area between them with a single color using `geom_ribbon()`.\n\n```{r geom-ribbon-without-fill}\nggplot() +\n  geom_line(aes(x, y, linetype = z), data = df_long) +\n  geom_ribbon(aes(x, ymin = a, ymax = b), data = df_wide, alpha = 0.2) +\n  guides(linetype = \"none\")\n```\n\nCan we fill the area between the two lines with two different colors? One color when the solid line is *above* the dashed line, and a different color when the solid line is *below* the dashed line?\n\nThat shouldn't be hard. Let's map `a \u003c b` to the `fill` aesthetic in `geom_ribbon()` and...\n\n```{r geom-ribbon-with-fill}\nggplot() +\n  geom_line(aes(x, y, linetype = z), data = df_long) +\n  geom_ribbon(aes(x, ymin = a, ymax = b, fill = a \u003c b), data = df_wide, alpha = 0.6) +\n  guides(linetype = \"none\", fill = \"none\")\n```\n\nChaos.\n\nWhat happened? Is this a bug in `geom_ribbon()`?\n\nNo, it's not a bug. The problem is that we haven't dealt with line intersections properly. I call this the [Unbraided Ribbon Problem](https://nsgrantham.github.io/ggbraid/articles/temps.html#the-unbraided-ribbon-problem).\n\nTo fix it, replace `geom_ribbon()` with `geom_braid()` from ggbraid.\n\n```{r geom-braid-with-fill}\nggplot() +\n  geom_line(aes(x, y, linetype = z), data = df_long) +\n  geom_braid(aes(x, ymin = a, ymax = b, fill = a \u003c b), data = df_wide, alpha = 0.6) +\n  guides(linetype = \"none\", fill = \"none\")\n```\n\n## Articles\n\n-   For an introduction to ggbraid and the \"Unbraided Ribbon Problem\", see [Average Daily Temperatures](https://nsgrantham.github.io/ggbraid/articles/temps.html).\n\n-   To learn how to use `geom_braid()` with `geom_step()`, see [NBA Finals Game](https://nsgrantham.github.io/ggbraid/articles/hoops.html).\n\n-   ggbraid supports flipped aesthetics, see [US Supreme Court](https://nsgrantham.github.io/ggbraid/articles/court.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/nsgrantham.github.io%2Fggbraid%2F","html_url":"https://awesome.ecosyste.ms/projects/nsgrantham.github.io%2Fggbraid%2F","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/nsgrantham.github.io%2Fggbraid%2F/lists"}