Function wraps ggplot2 geom_bar and geom_col to creates a bar plot.
create_bar_plot(
df = NULL,
aes_x = NULL,
aes_y = NULL,
aes_color = NULL,
aes_fill = NULL,
position = "stack",
title = NULL,
subtitle = NULL,
caption = NULL,
center_titles = FALSE,
x_title = NULL,
y_title = NULL,
hide_x_tics = FALSE,
hide_y_tics = FALSE,
rot_x_tic_angle = 0,
rot_y_tic_label = FALSE,
bar_fill = NA,
bar_color = "black",
bar_alpha = 1,
bar_lwd = 0.7,
bar_width = NULL,
x_major_breaks = NULL,
y_limits = NULL,
y_major_breaks = waiver(),
y_minor_breaks = waiver(),
y_labels = waiver(),
axis_text_size = 11,
do_coord_flip = FALSE,
order_bars = NULL,
bar_labels = FALSE,
bar_label_sz = 4,
bar_label_color = "black",
bar_label_fontface = "plain",
bar_label_alpha = 1,
show_major_grids = TRUE,
show_minor_grids = TRUE,
panel_color = "white",
panel_border_color = "black",
show_legend = TRUE,
legend_pos = "right",
legend_key_width = 0.7,
legend_key_height = 0.7,
legend_key_backgrd = "white",
silent_NA_warning = FALSE,
png_file_path = NULL,
png_width_height = c(480, 480)
)
A ggplot class object.
The target data frame for the bar chart.
A required string that sets the x axis discrete variable name from 'df'.
Sets the y axis numeric variable name from 'df'. If this variable is NULL
the bar heights will be proportional to the number of cases in the levels of 'aes_x'.
If 'aes_y' is not NULL
, the bar heights represent this parameter's values across the 'aes_x' levels.
Sets the variable name from 'df' for the aesthetic mapping for color.
Sets the variable name from 'df' for the aesthetic mapping for fill.
A string that sets the bar positions. Acceptable values are "dodge"(side by side), "identity"(overlap) or "stack".
A string that sets the overall title.
A string that sets the overall subtitle.
A string that sets the caption.
A logical which if TRUE
centers both the 'title' and 'subtitle'.
A string that sets the x axis title. If NULL
(the default) then the x axis title does not appear.
A string that sets the y axis title. If NULL
(the default) then the y axis title does not appear.
A logical that controls the appearance of the x axis tics.
A logical that controls the appearance of the y axis tics.
A numeric that sets the angle of rotation for the x tic label. When x tic labels are long, a value of 40 for this argument usually works well.
A logical which if TRUE rotates the y tic labels 90 degrees for enhanced readability.
A string that sets the fill color attribute for the bars.
A string that sets the outline color attribute for the bars.
A numeric that sets the alpha component attribute to 'bar_color'.
A numeric that sets the bar's outline line width attribute.
A numeric that sets the width attribute of the bars.
If 'aes_x' is numeric then this parameter is a numeric vector that defines the major breaks/intervals for the x axis. Interval labels are created and their respective counts are displayed.
A numeric 2 element vector that sets the minimum and maximum for the y axis.
Use NA
to refer to the existing minimum and maximum.
A numeric vector or function that defines the exact major tic locations along the y axis.
A numeric vector or function that defines the exact minor tic locations along the y axis.
A character vector with the same length as 'y_major_breaks', that labels the major tics.
A numeric that sets the font size along the axis'. Default is 11.
A logical which if TRUE
will flip the x and y axis'.
A string which will order the bars in a specific direction. Acceptable values are "asc" or "desc"
A logical which if TRUE
will label each bar with its value.
A numeric that sets the size of the label.
A string that sets the label's color.
A string that sets the label's font face. Acceptable values are "plain", "bold", "italic", "bold.italic".
A numeric that sets the label's alpha value.
A logical that controls the appearance of major grids.
A logical that controls the appearance of minor grids.
A string in hexidecimal or color name that sets the plot panel's color. The default is "white".
A string in hexidecimal or color name that sets the plot panel's border color. The default is "black".
A logical that controls the appearance of the legend.
A string that sets the legend position. Acceptable values are "top", "bottom", "left", "right".
A numeric that sets the legend width in cm.
A numeric that sets the legend height in cm.
A string that sets the legend's background color.
A logical that controls the appearance of a console warning when Na's are removed.
A character string with the directory and file name to produce a png image of the plot.
A numeric vector that sets the width and height of the png image in pixels. The default is c(480,480). There are 37.8 pixels in a centimeter.
library(ggplot2)
library(data.table)
library(RColorBrewer)
library(RplotterPkg)
RplotterPkg::create_bar_plot(
df = RplotterPkg::religion,
aes_x = "happy",
aes_fill = "religion",
position = "dodge",
title = "Happy Religions",
center_titles = TRUE,
rot_y_tic_label = TRUE,
bar_width = 0.8,
order_bars = "desc",
x_title = "Happiness",
y_title = "Count",
axis_text_size = 16
) +
ggplot2::scale_fill_discrete(
type = RColorBrewer::brewer.pal(n = 9, name = "Set1")
)
Run the code above in your browser using DataLab