{"id":13711006,"url":"https://github.com/cj-holmes/ggambit","last_synced_at":"2025-05-06T20:31:30.900Z","repository":{"id":55399947,"uuid":"318881061","full_name":"cj-holmes/ggambit","owner":"cj-holmes","description":"Visualise FEN chess positions with ggplot2","archived":false,"fork":false,"pushed_at":"2021-07-17T08:47:58.000Z","size":3085,"stargazers_count":20,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-02-13T10:03:13.775Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cj-holmes.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-05T20:17:51.000Z","updated_at":"2023-01-30T17:37:12.000Z","dependencies_parsed_at":"2022-08-14T23:30:25.009Z","dependency_job_id":null,"html_url":"https://github.com/cj-holmes/ggambit","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/cj-holmes%2Fggambit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cj-holmes%2Fggambit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cj-holmes%2Fggambit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cj-holmes%2Fggambit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cj-holmes","download_url":"https://codeload.github.com/cj-holmes/ggambit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224528321,"owners_count":17326339,"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:03.328Z","updated_at":"2024-11-13T21:31:22.718Z","avatar_url":"https://github.com/cj-holmes.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 = \"60%\",\n  dpi = 300)\n```\n\n# ggambit \u003cimg src=\"data-raw/hex-logo/ggambit-hex-logo.png\" align=\"right\" height=\"139\"/\u003e\n\n\u003c!-- badges: start --\u003e\n[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)\n\u003c!-- badges: end --\u003e\n\n**THIS IS A DEVELOPMENTAL WORK IN PROGRESS**\n\n## Introduction\nThe aim of `ggambit` is to visualise Forsyth–Edwards Notation (FEN) chess positions in R using `ggplot2`. Simple annotations (such as arrows and circles) can also be added to the image by specifying standard chess square notation (e4, c5, etc...). \n\nAs the plotting engine is `ggplot2`, any additional `ggplot2` layers may be added to the image, giving the user full creative control.\n\n### Attribution\nChess piece SVG design file downloaded from https://commons.wikimedia.org/wiki/File:Chess_Pieces_Sprite.svg\n  \n  * jurgenwesterhof (adapted from work of Cburnett), CC BY-SA 3.0 \u003chttps://creativecommons.org/licenses/by-sa/3.0\u003e, via Wikimedia Commons\n\n## Installation\n`ggambit` can be installed from github\n```{r eval=FALSE}\nremotes::install_github('cj-holmes/ggambit')\n```\n\n```{r}\nlibrary(ggambit)\n```\n\n## Plotting FEN positions\nVisualise the FEN starting position using the default settings (from white's perspective)\n```{r}\nplot_fen()\n```\n\nUse `add_arrow()`, `highlight_squares()`, `add_piece()`, `add_box()` and `fade_board()` to include simple annotations specified by chess board squares. These annotations are added to the plot in layers.\n```{r}\nplot_fen() + add_arrow(\"e2\", \"e4\") + highlight_squares(c(\"d5\", \"f5\"))\nplot_fen() + add_box(\"d4\", \"e5\", fill=\"white\", alpha=1/2) + add_arrow(\"c2\", \"c4\")\n\n```\n\n### A more interesting example\n```{r}\nmy_fen \u003c- \"2k4r/pp3pp1/2p1bn1p/4N3/1b2PQ2/4KB2/Pq1r1PPP/R6R b - - 1 18\"\n```\n\n```{r}\nplot_fen(my_fen) + add_arrow(\"b4\", \"c5\") + highlight_squares(\"e3\")\n```\n\nChange perspective with the `perspective` argument\n```{r}\nplot_fen(my_fen, perspective = \"b\") + add_arrow(\"b4\", \"c5\") + highlight_squares(\"e3\")\n```\n\n## Board customisation\nSquare fill colours can be specified with the `palette` argument. An attempt at a newspaper style can be set using `palette = \"news\"`. See `plot_fen()` documentation for more details and options.\n```{r}\nplot_fen(palette = \"green\")\nplot_fen(palette = \"news\")\n```\n\n## Parsing PGN files\nThere is a new **experimental** (and slow!) function for the parsing of PGN files to a long-format dataframe `pgn_to_df()`. Currently this function only works with raw (un-annotated) PGN files and **should be used with extreme caution**.\n\nHere I demonstrate using it to parse my own personal PGNs (from [Lichess](https://lichess.org/)) and combining it with `plot_fen()` to visualise where I move pieces to as a heat map.\n\nParse the PGN file\n```{r message=FALSE}\npgn_df \u003c- pgn_to_df('tests/lichess_holmestorm_2021-01-03.pgn')\n```\n\nVisualise \n```{r message=FALSE, warning=FALSE, out.width=\"100%\", fig.width=10}\nlibrary(ggplot2)\nlibrary(dplyr)\n\nplot_fen(palette = c(\"white\", \"white\"), show_coords = FALSE, border_col = 1)+\n  fade_board(fade = 0.3)+\n  ggnewscale::new_scale_fill()+\n  geom_tile(data =\n              pgn_df %\u003e%\n              filter(player == \"holmestorm\") %\u003e%\n              mutate(piece_moved = factor(piece_moved, levels = c(\"P\", \"B\", \"N\", \"R\", \"Q\", \"K\"))) %\u003e%\n              count(colour, piece_moved, x, y) %\u003e%\n              group_by(colour, piece_moved) %\u003e%\n              mutate(t = sum(n),\n                     p = 100 * (n/t)),\n            aes(x, y, fill = p), alpha = 0.9)+\n  facet_grid(colour ~ piece_moved)+\n  scale_fill_viridis_c(option=\"viridis\")+\n  theme(legend.position = \"bottom\")+\n  labs(title = \"Where do I move my pieces to?\",\n       subtitle = \"Facetted by piece [columns] and side (White/Black) [rows]\",\n       fill = \"%\",\n       caption = \"Data source: Lichess, Username: holmestorm\")\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcj-holmes%2Fggambit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcj-holmes%2Fggambit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcj-holmes%2Fggambit/lists"}