Add hbar_percent()

This commit is contained in:
gnoblet 2022-07-10 11:27:01 -04:00
parent c8fdf41521
commit d349e6a6d3
10 changed files with 319 additions and 14 deletions

View file

@ -11,7 +11,6 @@
#' @param y_title The y scale title. Default to empty string
#' @param group_title The group legend title. Defaut to NULL
#' @param font_family The font family. Default to "Leelawadee"
#' @param scales_percent Should x_labels be scaled to percentages? Default to TRUE.
#' @param position Should the chart be stacked? Default to dodge
#' @param reverse Boolean indicating whether the color palette should be reversed
#' @param title Plot title. Default to empty string
@ -21,7 +20,7 @@
#' @return A horizontal bar chart
#'
#' @export
hbar <- function(.tbl, x, y, group = NULL, initiative = "reach", pal = "primary", width = 0.5, x_title = "", y_title = "", group_title = NULL, font_family = "Leelawadee", scales_percent = TRUE, position = "dodge", reverse = FALSE, title = "", subtitle = "", ...){
hbar <- function(.tbl, x, y, group = NULL, initiative = "reach", pal = "primary", width = 0.5, x_title = "", y_title = "", group_title = NULL, font_family = "Leelawadee", position = "dodge", reverse = FALSE, title = "", subtitle = "", ...){
if (!(initiative %in% c("reach", "agora", "impact"))) rlang::abort(c("Wrong `initiative` arg", "*" = paste0("Arg `initiative` cannot be: ", initiative), "i" = "It must be one of 'reach' or 'agora' or 'impact'"))
@ -62,7 +61,6 @@ hbar <- function(.tbl, x, y, group = NULL, initiative = "reach", pal = "primary"
alpha_fill = 1,
pal = palette,
width = width,
x_labels = ifelse(scales_percent, scales::percent, NULL),
position = position,
stat = "identity",
title = "",
@ -73,3 +71,80 @@ hbar <- function(.tbl, x, y, group = NULL, initiative = "reach", pal = "primary"
return(hbar)
}
#' @title Simple horizontal bar chart which scales x_labels to percentages
#'
#' @param .tbl Some data
#' @param x Some numeric column on the x scale
#' @param y Some column on the y scale
#' @param group Some grouping categorical column, e.g. administrative areas
#' @param initiative Either "reach" or "agora" or "impact" for the color palette
#' @param pal The color palette from the initiative
#' @param width Width.
#' @param x_title The x scale title. Default to empty string
#' @param y_title The y scale title. Default to empty string
#' @param group_title The group legend title. Defaut to NULL
#' @param font_family The font family. Default to "Leelawadee"
#' @param position Should the chart be stacked? Default to dodge
#' @param reverse Boolean indicating whether the color palette should be reversed
#' @param title Plot title. Default to empty string
#' @param subtitle Plot subtitle. Default to empty string
#' @param ... Other arguments to be passed to "ggblanket::gg_col"
#'
#' @return A horizontal bar chart
#'
#' @export
hbar_percent <- function(.tbl, x, y, group = NULL, initiative = "reach", pal = "primary", width = 0.5, x_title = "", y_title = "", group_title = NULL, font_family = "Leelawadee", position = "dodge", reverse = FALSE, title = "", subtitle = "", ...){
if (!(initiative %in% c("reach", "agora", "impact"))) rlang::abort(c("Wrong `initiative` arg", "*" = paste0("Arg `initiative` cannot be: ", initiative), "i" = "It must be one of 'reach' or 'agora' or 'impact'"))
if (initiative == "reach") {
palette <- pal_reach(pal, reverse = reverse)
main_col <- cols_reach("main_grey")
if(is.null(palette)) rlang::warn(
c(paste0("There is no palette '", pal, "' for initiative 'reach'. Fallback to ggblanket's default color palette."),
"i" = paste0("Use `pal_reach(show_palettes = T)` to see the list of availabale palettes.")
)
)
}
if (initiative == "agora") {
palette <- pal_agora(pal, reverse = reverse)
main_col <- cols_agora("main_bordeaux")
if(is.null(palette)) rlang::warn(
c(paste0("There is no palette '", pal, "' for initiative 'agora'. Fallback to ggblanket's default color palette."),
"i" = paste0("Use `pal_agora(show_palettes = T)` to see the list of availabale palettes.")
)
)
}
if (initiative == "impact") rlang::warn("IMPACT colors are under development. Fallback to ggblanket's default.")
hbar <- .tbl |>
ggblanket::gg_col(x = {{ x }},
y = {{ y }},
col = {{ group }},
theme = ggblanket::gg_theme(font = font_family, pal_title = main_col),
x_title = x_title,
y_title = y_title,
col_title = group_title,
alpha_fill = 1,
pal = palette,
width = width,
x_labels = scales::percent,
position = position,
stat = "identity",
title = "",
subtitle = "",
...
)
return(hbar)
}

View file

@ -60,7 +60,7 @@
</li></ul><hr></div>
<div class="section level2">
<h2 class="pkg-version" data-toc-text="0.1.1.9000" id="visualizer-0119000">visualizeR 0.1.1.9000<a class="anchor" aria-label="anchor" href="#visualizer-0119000"></a></h2>
<ul><li>Added two horizontal bar functions: <code><a href="../reference/hbar.html">hbar()</a></code>, <code>hbar_percent()</code> (<a href="https://github.com/gnoblet/visualizeR/issues/3" class="external-link">#3</a>)</li>
<ul><li>Added two horizontal bar functions: <code><a href="../reference/hbar.html">hbar()</a></code>, <code><a href="../reference/hbar_percent.html">hbar_percent()</a></code> (<a href="https://github.com/gnoblet/visualizeR/issues/3" class="external-link">#3</a>)</li>
<li>Added some internals to check for missing columns and bad arguments (<a href="https://github.com/gnoblet/visualizeR/issues/3" class="external-link">#3</a>)</li>
<li>Modified some <code><a href="../reference/theme_reach.html">theme_reach()</a></code> documentation</li>
<li>Add <code><a href="../reference/buffer_bbox.html">buffer_bbox()</a></code> function to produce a buffered bbox, e.g. for use with <code>tmap</code>

View file

@ -2,7 +2,7 @@ pandoc: 2.14.0.3
pkgdown: 2.0.5
pkgdown_sha: ~
articles: {}
last_built: 2022-07-10T15:16Z
last_built: 2022-07-10T15:26Z
urls:
reference: https://gnoblet.github.io/visualizeR/reference
article: https://gnoblet.github.io/visualizeR/articles

View file

@ -63,7 +63,6 @@
<span> y_title <span class="op">=</span> <span class="st">""</span>,</span>
<span> group_title <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span> font_family <span class="op">=</span> <span class="st">"Leelawadee"</span>,</span>
<span> scales_percent <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span> position <span class="op">=</span> <span class="st">"dodge"</span>,</span>
<span> reverse <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> title <span class="op">=</span> <span class="st">""</span>,</span>
@ -118,10 +117,6 @@
<dd><p>The font family. Default to "Leelawadee"</p></dd>
<dt>scales_percent</dt>
<dd><p>Should x_labels be scaled to percentages? Default to TRUE.</p></dd>
<dt>position</dt>
<dd><p>Should the chart be stacked? Default to dodge</p></dd>

View file

@ -0,0 +1,166 @@
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content="Simple horizontal bar chart which scales x_labels to percentages"><title>Simple horizontal bar chart which scales x_labels to percentages — hbar_percent • visualizeR</title><!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png"><link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png"><link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png"><link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png"><link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png"><script src="../deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><link href="../deps/bootstrap-5.1.0/bootstrap.min.css" rel="stylesheet"><script src="../deps/bootstrap-5.1.0/bootstrap.bundle.min.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous"><!-- bootstrap-toc --><script src="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- search --><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- pkgdown --><script src="../pkgdown.js"></script><meta property="og:title" content="Simple horizontal bar chart which scales x_labels to percentages — hbar_percent"><meta property="og:description" content="Simple horizontal bar chart which scales x_labels to percentages"><meta property="og:image" content="https://gnoblet.github.io/visualizeR/logo.png"><!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--></head><body>
<a href="#main" class="visually-hidden-focusable">Skip to contents</a>
<nav class="navbar fixed-top navbar-light navbar-expand-lg bg-light"><div class="container">
<a class="navbar-brand me-2" href="../index.html">visualizeR</a>
<small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">0.1.5.9000</small>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbar" class="collapse navbar-collapse ms-3">
<ul class="navbar-nav me-auto"><li class="active nav-item">
<a class="nav-link" href="../reference/index.html">Reference</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../news/index.html">Changelog</a>
</li>
</ul><form class="form-inline my-2 my-lg-0" role="search">
<input type="search" class="form-control me-sm-2" aria-label="Toggle navigation" name="search-input" data-search-index="../search.json" id="search-input" placeholder="Search for" autocomplete="off"></form>
<ul class="navbar-nav"><li class="nav-item">
<a class="external-link nav-link" href="https://github.com/gnoblet/visualizeR/" aria-label="github">
<span class="fab fa fab fa-github fa-lg"></span>
</a>
</li>
</ul></div>
</div>
</nav><div class="container template-reference-topic">
<div class="row">
<main id="main" class="col-md-9"><div class="page-header">
<img src="../logo.png" class="logo" alt=""><h1>Simple horizontal bar chart which scales x_labels to percentages</h1>
<small class="dont-index">Source: <a href="https://github.com/gnoblet/visualizeR/blob/HEAD/R/hbar.R" class="external-link"><code>R/hbar.R</code></a></small>
<div class="d-none name"><code>hbar_percent.Rd</code></div>
</div>
<div class="ref-description section level2">
<p>Simple horizontal bar chart which scales x_labels to percentages</p>
</div>
<div class="section level2">
<h2 id="ref-usage">Usage<a class="anchor" aria-label="anchor" href="#ref-usage"></a></h2>
<div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">hbar_percent</span><span class="op">(</span></span>
<span> <span class="va">.tbl</span>,</span>
<span> <span class="va">x</span>,</span>
<span> <span class="va">y</span>,</span>
<span> group <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span> initiative <span class="op">=</span> <span class="st">"reach"</span>,</span>
<span> pal <span class="op">=</span> <span class="st">"primary"</span>,</span>
<span> width <span class="op">=</span> <span class="fl">0.5</span>,</span>
<span> x_title <span class="op">=</span> <span class="st">""</span>,</span>
<span> y_title <span class="op">=</span> <span class="st">""</span>,</span>
<span> group_title <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span> font_family <span class="op">=</span> <span class="st">"Leelawadee"</span>,</span>
<span> position <span class="op">=</span> <span class="st">"dodge"</span>,</span>
<span> reverse <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span> title <span class="op">=</span> <span class="st">""</span>,</span>
<span> subtitle <span class="op">=</span> <span class="st">""</span>,</span>
<span> <span class="va">...</span></span>
<span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level2">
<h2 id="arguments">Arguments<a class="anchor" aria-label="anchor" href="#arguments"></a></h2>
<dl><dt>.tbl</dt>
<dd><p>Some data</p></dd>
<dt>x</dt>
<dd><p>Some numeric column on the x scale</p></dd>
<dt>y</dt>
<dd><p>Some column on the y scale</p></dd>
<dt>group</dt>
<dd><p>Some grouping categorical column, e.g. administrative areas</p></dd>
<dt>initiative</dt>
<dd><p>Either "reach" or "agora" or "impact" for the color palette</p></dd>
<dt>pal</dt>
<dd><p>The color palette from the initiative</p></dd>
<dt>width</dt>
<dd><p>Width.</p></dd>
<dt>x_title</dt>
<dd><p>The x scale title. Default to empty string</p></dd>
<dt>y_title</dt>
<dd><p>The y scale title. Default to empty string</p></dd>
<dt>group_title</dt>
<dd><p>The group legend title. Defaut to NULL</p></dd>
<dt>font_family</dt>
<dd><p>The font family. Default to "Leelawadee"</p></dd>
<dt>position</dt>
<dd><p>Should the chart be stacked? Default to dodge</p></dd>
<dt>reverse</dt>
<dd><p>Boolean indicating whether the color palette should be reversed</p></dd>
<dt>title</dt>
<dd><p>Plot title. Default to empty string</p></dd>
<dt>subtitle</dt>
<dd><p>Plot subtitle. Default to empty string</p></dd>
<dt>...</dt>
<dd><p>Other arguments to be passed to "ggblanket::gg_col"</p></dd>
</dl></div>
<div class="section level2">
<h2 id="value">Value<a class="anchor" aria-label="anchor" href="#value"></a></h2>
<p>A horizontal bar chart</p>
</div>
</main><aside class="col-md-3"><nav id="toc"><h2>On this page</h2>
</nav></aside></div>
<footer><div class="pkgdown-footer-left">
<p></p><p>Developed by Noblet Guillaume.</p>
</div>
<div class="pkgdown-footer-right">
<p></p><p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.5.</p>
</div>
</footer></div>
</body></html>

View file

@ -81,6 +81,11 @@
<dd>Simple horizontal bar chart</dd>
</dl><dl><dt>
<code><a href="hbar_percent.html">hbar_percent()</a></code>
</dt>
<dd>Simple horizontal bar chart which scales x_labels to percentages</dd>
</dl><dl><dt>
<code><a href="if_not_in_stop.html">if_not_in_stop()</a></code>
</dt>
<dd>Stop statement "If not in colnames" with colnames</dd>

File diff suppressed because one or more lines are too long

View file

@ -30,6 +30,9 @@
<url>
<loc>https://gnoblet.github.io/visualizeR/reference/hbar.html</loc>
</url>
<url>
<loc>https://gnoblet.github.io/visualizeR/reference/hbar_percent.html</loc>
</url>
<url>
<loc>https://gnoblet.github.io/visualizeR/reference/if_not_in_stop.html</loc>
</url>

View file

@ -16,7 +16,6 @@ hbar(
y_title = "",
group_title = NULL,
font_family = "Leelawadee",
scales_percent = TRUE,
position = "dodge",
reverse = FALSE,
title = "",
@ -47,8 +46,6 @@ hbar(
\item{font_family}{The font family. Default to "Leelawadee"}
\item{scales_percent}{Should x_labels be scaled to percentages? Default to TRUE.}
\item{position}{Should the chart be stacked? Default to dodge}
\item{reverse}{Boolean indicating whether the color palette should be reversed}

64
man/hbar_percent.Rd Normal file
View file

@ -0,0 +1,64 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/hbar.R
\name{hbar_percent}
\alias{hbar_percent}
\title{Simple horizontal bar chart which scales x_labels to percentages}
\usage{
hbar_percent(
.tbl,
x,
y,
group = NULL,
initiative = "reach",
pal = "primary",
width = 0.5,
x_title = "",
y_title = "",
group_title = NULL,
font_family = "Leelawadee",
position = "dodge",
reverse = FALSE,
title = "",
subtitle = "",
...
)
}
\arguments{
\item{.tbl}{Some data}
\item{x}{Some numeric column on the x scale}
\item{y}{Some column on the y scale}
\item{group}{Some grouping categorical column, e.g. administrative areas}
\item{initiative}{Either "reach" or "agora" or "impact" for the color palette}
\item{pal}{The color palette from the initiative}
\item{width}{Width.}
\item{x_title}{The x scale title. Default to empty string}
\item{y_title}{The y scale title. Default to empty string}
\item{group_title}{The group legend title. Defaut to NULL}
\item{font_family}{The font family. Default to "Leelawadee"}
\item{position}{Should the chart be stacked? Default to dodge}
\item{reverse}{Boolean indicating whether the color palette should be reversed}
\item{title}{Plot title. Default to empty string}
\item{subtitle}{Plot subtitle. Default to empty string}
\item{...}{Other arguments to be passed to "ggblanket::gg_col"}
}
\value{
A horizontal bar chart
}
\description{
Simple horizontal bar chart which scales x_labels to percentages
}