feat: allow rounding digits for create_bar_asis

This commit is contained in:
Martin Chan 2021-02-12 17:44:43 +00:00
Родитель e2e2ca693e
Коммит 0a65352d2c
2 изменённых файлов: 16 добавлений и 6 удалений

Просмотреть файл

@ -26,6 +26,8 @@
#' In-built accepted values include "default" (default), "alert" (red), and
#' "darkblue". Otherwise, hex codes are also accepted. You can also supply
#' RGB values via `rgb2hex()`.
#' @param rounding Numeric value to specify number of digits to show in data
#' labels
#'
#' @return
#' ggplot object. A horizontal bar plot.
@ -43,7 +45,8 @@
#' title = "Total Meeting Hours over period",
#' subtitle = "By Organization",
#' caption = extract_date_range(sq_data, return = "text"),
#' bar_colour = "darkblue")
#' bar_colour = "darkblue",
#' rounding = 0)
#'
#' @import ggplot2
#' @import dplyr
@ -60,7 +63,8 @@ create_bar_asis <- function(data,
ylab = group_var,
xlab = bar_var,
percent = FALSE,
bar_colour = "default"){
bar_colour = "default",
rounding = 1){
## Colour bar override
if(bar_colour == "default"){
@ -84,7 +88,7 @@ create_bar_asis <- function(data,
data %>%
ggplot(aes(x = reorder(!!sym(group_var), !!sym(bar_var)), y = !!sym(bar_var))) +
geom_col(fill = bar_colour) +
geom_text(aes(label = round(!!sym(bar_var), 2)),
geom_text(aes(label = round(!!sym(bar_var), digits = rounding)),
hjust = -0.25,
color = "#000000",
fontface = "bold",
@ -94,7 +98,8 @@ create_bar_asis <- function(data,
data %>%
ggplot(aes(x = reorder(!!sym(group_var), !!sym(bar_var)), y = !!sym(bar_var))) +
geom_col(fill = bar_colour) +
geom_text(aes(label = scales::percent(!!sym(bar_var), accuracy = 1)),
geom_text(aes(label = scales::percent(!!sym(bar_var),
accuracy = 10 ^ -rounding)),
hjust = -0.25,
color = "#000000",
fontface = "bold",

Просмотреть файл

@ -14,7 +14,8 @@ create_bar_asis(
ylab = group_var,
xlab = bar_var,
percent = FALSE,
bar_colour = "default"
bar_colour = "default",
rounding = 1
)
}
\arguments{
@ -42,6 +43,9 @@ percentage signs. Defaults to FALSE.}
In-built accepted values include "default" (default), "alert" (red), and
"darkblue". Otherwise, hex codes are also accepted. You can also supply
RGB values via \code{rgb2hex()}.}
\item{rounding}{Numeric value to specify number of digits to show in data
labels}
}
\value{
ggplot object. A horizontal bar plot.
@ -65,7 +69,8 @@ sq_data \%>\%
title = "Total Meeting Hours over period",
subtitle = "By Organization",
caption = extract_date_range(sq_data, return = "text"),
bar_colour = "darkblue")
bar_colour = "darkblue",
rounding = 0)
}
\seealso{