| bcat_plt_bar | R Documentation |
Create a bar plot using ggplot2 graphics. This function is a wrapper to create commonly used styles of bar plots. Additional layers can be added to this plot as needed. More complicated plots can be creating using individual ggplot2 layers.
bcat_plt_bar(
df,
x = NULL,
y = NULL,
fill = NULL,
facet = NULL,
stat = c("freq", "identity", "sum", "mean", "median"),
position = c("stack", "dodge", "fill"),
x_lab = ggplot2::waiver(),
y_lab = ggplot2::waiver(),
title = ggplot2::waiver(),
subtitle = ggplot2::waiver(),
caption = ggplot2::waiver(),
legend_lab = ggplot2::waiver(),
legend_position = "bottom",
legend_hide = FALSE,
x_scale = NULL,
y_scale = NULL,
fill_scale = Rbearcat::scale_fill_UC(),
facet_scale = c("fixed", "free_y", "free_x", "free"),
nrow = NULL,
ncol = NULL,
x_refline = NULL,
y_refline = NULL,
coord_flip = FALSE,
order = FALSE
)
df |
The data to be displayed |
x |
Categorical variable to map to the x-axis |
y |
Variable to map to the y-axis. Only applicable if |
fill |
Variable to map to the fill aesthetic |
facet |
Facetting variable(s). Note: must wrap in |
stat |
Statistic to map to y-axis. Default ("freq") calculates frequencies,
"identity" plots the data as-is. "sum", "mean", and "median" calculate
and display the respective summary stats
Must specify |
position |
For grouped data, either stack bars ( |
x_lab |
Label for x-axis |
y_lab |
Label for y-axis |
title |
Plot title |
subtitle |
Plot subtitle |
caption |
Plot caption |
legend_lab |
Legend title |
legend_position |
legend position. "bottom" or "right" |
legend_hide |
Set to |
x_scale |
|
y_scale |
|
fill_scale |
|
facet_scale |
Shoud facet scales be fixed ("fixed", the default), free ("free"), or free in one dimension ("free_x", "free_y")? |
nrow |
Number of facet rows |
ncol |
Number of facet columns |
x_refline |
Vector of x-values at which to draw vertical reference lines |
y_refline |
Vector of y-values at which to draw horizontal reference lines |
coord_flip |
Set to |
order |
Set to |
A ggplot2 plot object.
Saannidhya Rawat
Other plots:
bcat_plt_area(),
bcat_plt_box(),
bcat_plt_coef(),
bcat_plt_diag(),
bcat_plt_hist(),
bcat_plt_line(),
bcat_plt_point(),
bcat_plt_ts()
library(ggplot2)
library(dplyr)
library(scales)
# basic plot of frequencies
bcat_plt_bar(df = mpg,
x = toupper(class),
order = TRUE,
x_lab = NULL,
y_lab = NULL,
title = "Number of Vehicles by Class and Drive Type",
legend_lab = "Drive Type")
# can plot relative frequencies for each `x` by `fill` using `position = "fill"`
bcat_plt_bar(df = mpg,
x = toupper(class),
fill = drv,
position = "fill",
x_lab = NULL,
y_lab = NULL,
y_scale = scale_y_continuous(labels = percent_format()),
title = "Percent of Vehicle Class for Each Drive Type",
legend_lab = "Drive Type")
# use `stat` to compute and plot other statistics of interest
bcat_plt_bar(mpg,
x = toupper(class),
y = hwy,
fill = factor(year),
stat = "mean",
order = TRUE,
position = "dodge",
y_refline = round(mean(mpg$hwy), 2),
coord_flip = TRUE,
x_lab = NULL,
y_lab = "Highway MPG",
title = "Average Highway MPG by Vehicle Class",
legend_lab = NULL)
# use `stat = "identity"` to plot data directly from data set
mpg %>%
group_by(year, class) %>%
summarise(hwy = mean(hwy)) %>%
bcat_plt_bar(x = toupper(class),
y = hwy,
fill = factor(year),
stat = "identity",
order = TRUE,
position = "dodge",
y_refline = round(mean(mpg$hwy), 2),
coord_flip = TRUE,
x_lab = NULL,
y_lab = "Highway MPG",
title = "Average Highway MPG by Vehicle Class",
legend_lab = NULL)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.