feat: add percent argument for bar plots

This commit is contained in:
Martin Chan 2021-04-29 10:17:15 +01:00
Родитель 2904ed5afd
Коммит 5f16a097da
4 изменённых файлов: 61 добавлений и 13 удалений

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

@ -29,6 +29,8 @@
#' RGB values via `rgb2hex()`.
#' @param na.rm A logical value indicating whether `NA` should be stripped
#' before the computation proceeds. Defaults to `FALSE`.
#' @param percent Logical value to determine whether to show labels as
#' percentage signs. Defaults to `FALSE`.
#' @param plot_title An option to override plot title.
#' @param plot_subtitle An option to override plot subtitle.
#' @param rank String specifying how to rank the bars. Valid inputs are:
@ -74,6 +76,7 @@ create_bar <- function(data,
return = "plot",
bar_colour = "default",
na.rm = FALSE,
percent = FALSE,
plot_title = us_to_space(metric),
plot_subtitle = paste("Average by", tolower(camel_clean(hrvar))),
rank = "descending",
@ -133,6 +136,7 @@ create_bar <- function(data,
hrvar = hrvar,
mingroup = mingroup,
stack_colours = bar_colour,
percent = percent,
plot_title = plot_title,
plot_subtitle = plot_subtitle,
return = "plot",

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

@ -21,13 +21,15 @@
#' Valid inputs are "plot" and "table".
#' @param stack_colours
#' A character vector to specify the colour codes for the stacked bar charts.
#' @param percent Logical value to determine whether to show labels as
#' percentage signs. Defaults to `FALSE`.
#' @param plot_title An option to override plot title.
#' @param plot_subtitle An option to override plot subtitle.
#' @param rank String specifying how to rank the bars. Valid inputs are:
#' - `"descending"` - ranked highest to lowest from top to bottom (default).
#' - `"ascending"` - ranked lowest to highest from top to bottom.
#' - `NULL` - uses the original levels of the HR attribute.
#' @param xlim An option to set max value in x axis.
#' @param xlim An option to set max value in x axis.
#'
#' @import dplyr
#' @import ggplot2
@ -76,6 +78,7 @@ create_stacked <- function(data,
"#34b1e2",
"#b4d5dd",
"#adc0cb"),
percent = FALSE,
plot_title = "Collaboration Hours",
plot_subtitle = paste("Average by", tolower(camel_clean(hrvar))),
rank = "descending",
@ -144,13 +147,13 @@ create_stacked <- function(data,
location <- max(myTable_legends$Total)
}
else if(is.numeric(xlim)) {
location <- xlim
location <- xlim
}
else {
stop("Invalid return to `xlim`")
}
## Remove max from axis labels
## Remove max from axis labels ------------------------------------------
max_blank <- function(x){
as.character(
c(
@ -159,6 +162,17 @@ create_stacked <- function(data,
)
}
## Remove max from axis labels, but with percentages ---------------------
max_blank_percent <- function(x){
x <- scales::percent(x)
as.character(
c(
x[1:length(x) - 1],
"")
)
}
invert_mean <- function(x){
mean(x) * -1
@ -184,14 +198,30 @@ create_stacked <- function(data,
}
} +
geom_bar(position = "stack", stat = "identity") +
geom_text(aes(label = round(Value, 1)),
position = position_stack(vjust = 0.5),
color = "#FFFFFF",
fontface = "bold") +
scale_y_continuous(expand = c(.01, 0),
limits = c(0, location * 1.25),
labels = max_blank,
position = "right") +
{ if(percent == FALSE){
geom_text(aes(label = round(Value, 1)),
position = position_stack(vjust = 0.5),
color = "#FFFFFF",
fontface = "bold")
} else if(percent == TRUE){
geom_text(aes(label = scales::percent(Value, accuracy = 0.1)),
position = position_stack(vjust = 0.5),
color = "#FFFFFF",
fontface = "bold")
}
} +
{ if(percent == FALSE){
scale_y_continuous(expand = c(.01, 0),
limits = c(0, location * 1.25),
labels = max_blank,
position = "right")
} else if(percent == TRUE){
scale_y_continuous(expand = c(.01, 0),
limits = c(0, location * 1.25),
labels = max_blank_percent,
position = "right")
}
} +
annotate("text",
x = myTable_legends$group,
y = location * 1.15,

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

@ -12,9 +12,11 @@ create_bar(
return = "plot",
bar_colour = "default",
na.rm = FALSE,
percent = FALSE,
plot_title = us_to_space(metric),
plot_subtitle = paste("Average by", tolower(camel_clean(hrvar))),
rank = "descending"
rank = "descending",
xlim = NULL
)
}
\arguments{
@ -47,6 +49,9 @@ RGB values via \code{rgb2hex()}.}
\item{na.rm}{A logical value indicating whether \code{NA} should be stripped
before the computation proceeds. Defaults to \code{FALSE}.}
\item{percent}{Logical value to determine whether to show labels as
percentage signs. Defaults to \code{FALSE}.}
\item{plot_title}{An option to override plot title.}
\item{plot_subtitle}{An option to override plot subtitle.}
@ -57,6 +62,8 @@ before the computation proceeds. Defaults to \code{FALSE}.}
\item \code{"ascending"} - ranked lowest to highest from top to bottom.
\item \code{NULL} - uses the original levels of the HR attribute.
}}
\item{xlim}{An option to set max value in x axis.}
}
\value{
A different output is returned depending on the value passed to the \code{return} argument:

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

@ -11,9 +11,11 @@ create_stacked(
mingroup = 5,
return = "plot",
stack_colours = c("#1d627e", "#34b1e2", "#b4d5dd", "#adc0cb"),
percent = FALSE,
plot_title = "Collaboration Hours",
plot_subtitle = paste("Average by", tolower(camel_clean(hrvar))),
rank = "descending"
rank = "descending",
xlim = NULL
)
}
\arguments{
@ -36,6 +38,9 @@ Valid inputs are "plot" and "table".}
\item{stack_colours}{A character vector to specify the colour codes for the stacked bar charts.}
\item{percent}{Logical value to determine whether to show labels as
percentage signs. Defaults to \code{FALSE}.}
\item{plot_title}{An option to override plot title.}
\item{plot_subtitle}{An option to override plot subtitle.}
@ -46,6 +51,8 @@ Valid inputs are "plot" and "table".}
\item \code{"ascending"} - ranked lowest to highest from top to bottom.
\item \code{NULL} - uses the original levels of the HR attribute.
}}
\item{xlim}{An option to set max value in x axis.}
}
\value{
Returns a 'ggplot' object by default, where 'plot' is passed in \code{return}.