{"id":25973529,"url":"https://github.com/jhelvy/factorlock","last_synced_at":"2025-08-18T08:03:44.976Z","repository":{"id":163544868,"uuid":"639017801","full_name":"jhelvy/factorlock","owner":"jhelvy","description":"Set factors levels based on row order","archived":false,"fork":false,"pushed_at":"2023-05-10T20:36:34.000Z","size":6841,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-18T08:02:09.227Z","etag":null,"topics":["factors","ggplot","ggplot2","r","rstats","rstats-package","tidyverse"],"latest_commit_sha":null,"homepage":"https://jhelvy.github.io/factorlock/","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/jhelvy.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":null,"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":"2023-05-10T15:24:30.000Z","updated_at":"2024-07-28T03:27:24.000Z","dependencies_parsed_at":"2024-08-10T07:30:10.140Z","dependency_job_id":null,"html_url":"https://github.com/jhelvy/factorlock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jhelvy/factorlock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhelvy%2Ffactorlock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhelvy%2Ffactorlock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhelvy%2Ffactorlock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhelvy%2Ffactorlock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jhelvy","download_url":"https://codeload.github.com/jhelvy/factorlock/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhelvy%2Ffactorlock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270962391,"owners_count":24675965,"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","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["factors","ggplot","ggplot2","r","rstats","rstats-package","tidyverse"],"created_at":"2025-03-05T01:29:56.495Z","updated_at":"2025-08-18T08:03:44.968Z","avatar_url":"https://github.com/jhelvy.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  warning    = FALSE,\n  message    = FALSE,\n  comment    = \"#\u003e\",\n  fig.path   = \"man/figures/\",\n  fig.retina = 3\n)\n```\n\n# factorlock \u003ca href='https://jhelvy.github.io/factorlock/'\u003e\u003cimg src='man/figures/logo.png' align=\"right\" style=\"height:139px;\"/\u003e\u003c/a\u003e\n\n\u003c!-- badges: start --\u003e\n[![CRAN\nstatus](https://www.r-pkg.org/badges/version/factorlock)](https://CRAN.R-project.org/package=factorlock)\n[![Travis build status](https://app.travis-ci.com/jhelvy/factorlock.svg?branch=master)](https://app.travis-ci.com/github/jhelvy/factorlock)\n[![](http://cranlogs.r-pkg.org/badges/grand-total/factorlock?color=blue)](https://cran.r-project.org/package=factorlock)\n\u003c!-- badges: end --\u003e\n\n# Installation\n\nThe current version is not yet on CRAN, but you can install it from\nGithub using the {remotes} package:\n\n```{r, eval=FALSE}\n# install.packages(\"remotes\")\nremotes::install_github(\"jhelvy/factorlock\")\n```\n\nLoad the library with:\n\n```{r}\nlibrary(factorlock)\n```\n\n# Usage\n\nThe only thing this package does is provide the `factorlock::lock_factors()` function to make it easier to reorder factors in a data frame according to the row order. \n\nBy default, bar charts made with {ggplot2} follow alphabetical ordering:\n\n```{r}\n#| label: bars1\n#| fig.width: 6\n#| fig.height: 4\n\nlibrary(ggplot2)\nlibrary(dplyr)\n\nmpg |\u003e\n  count(manufacturer) |\u003e\n  ggplot() +\n  geom_col(aes(x = n, y = manufacturer))\n```\n\nIf you wanted to sort the bars based on `n`, many people make the mistake of sorting the _data frame_ and assuming the reordered rows will pass through to the bars, like this:\n\n```{r}\n#| label: bars2\n#| fig.width: 6\n#| fig.height: 4\n\nmpg |\u003e\n  count(manufacturer) |\u003e\n  arrange(n) |\u003e\n  ggplot() +\n  geom_col(aes(x = n, y = manufacturer))\n```\n\n**But this produces the same chart!** 🤦\n\nInstead, to sort the bars based on `n` you have to reorder the **factor levels**:\n\n```{r}\n#| label: bars-reorder\n#| fig.width: 6\n#| fig.height: 4\n\nmpg |\u003e\n  count(manufacturer) |\u003e\n  ggplot() +\n  geom_col(aes(x = n, y = reorder(manufacturer, n)))\n```\n\nI find this rather unintuitive and difficult to remember, let alone confusing because the ordering of the rows in the data frame won't match the factor ordering (which is rather opaque to the user).\n\n{factorlock} provides an alternative approach by allowing you to \"lock\" the factor ordering to that of the row ordering in the data frame:\n\n```{r}\n#| label: bars-locked\n#| fig.width: 6\n#| fig.height: 4\n\nmpg |\u003e\n  count(manufacturer) |\u003e\n  arrange(n) |\u003e\n  factorlock::lock_factors() |\u003e\n  ggplot() +\n  geom_col(aes(x = n, y = manufacturer))\n```\n\nNotice that you also get better axis label names for free here since the `y` variable is still mapped to just `manufacturer` instead of `reorder(manufacturer, n)`.\n\nBy default all character or factor type variables are \"locked\", but you can also specify which variables you want to \"lock\" while leaving the others alone:\n\n```{r}\n#| label: bars-locked-named\n#| fig.width: 6\n#| fig.height: 4\n\nmpg |\u003e\n  count(manufacturer) |\u003e\n  arrange(n) |\u003e\n  factorlock::lock_factors(\"manufacturer\") |\u003e\n  ggplot() +\n  geom_col(aes(x = n, y = manufacturer))\n```\n\nYou can also get a reverse factor ordering with `rev = TRUE`:\n\n```{r}\n#| label: bars-locked-rev\n#| fig.width: 6\n#| fig.height: 4\n\nmpg |\u003e\n  count(manufacturer) |\u003e\n  arrange(n) |\u003e\n  factorlock::lock_factors(rev = TRUE) |\u003e\n  ggplot() +\n  geom_col(aes(x = n, y = manufacturer))\n```\n\n# Author, Version, and License Information\n\n- Author: *John Paul Helveston* https://www.jhelvy.com/\n- Date First Written: *May 10, 2023*\n- License: [MIT](https://github.com/jhelvy/factorlock/blob/master/LICENSE.md)\n\n# Citation Information\n\nIf you use this package for in a publication, I would greatly appreciate it if you cited it - you can get the citation by typing `citation(\"factorlock\")` into R:\n\n```{r}\ncitation(\"factorlock\")\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhelvy%2Ffactorlock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjhelvy%2Ffactorlock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhelvy%2Ffactorlock/lists"}