Function plots a ggplot2 based histogram with options for scaling and viewing observation locations. The function offers one of four ways of setting the number of bins.
create_histogram_plot(
df,
aes_x = 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,
bins = 20,
binwidth = NULL,
bin_breaks = NULL,
bin_class = NULL,
bar_fill = NA,
bar_color = "black",
bar_alpha = 1,
bar_lwd = 1,
x_limits = NULL,
x_major_breaks = waiver(),
x_minor_breaks = waiver(),
x_labels = waiver(),
x_decimals = NULL,
x_scientific = NULL,
x_log10 = FALSE,
y_limits = NULL,
y_major_breaks = waiver(),
y_minor_breaks = waiver(),
y_labels = waiver(),
axis_text_size = 11,
do_coord_flip = FALSE,
bar_labels = FALSE,
bar_label_size = 6,
bar_label_color = "black",
plot_obs = FALSE,
plot_obs_len = 0.02,
plot_obs_color = "black",
bold_x = NULL,
bold_x_color = "black",
bold_x_linetype = "dashed",
show_major_grids = TRUE,
show_minor_grids = TRUE,
show_legend = TRUE,
panel_color = "white",
panel_border_color = "black",
legend_pos = "top",
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 continuous variable name from 'df'.
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", "dodge2"(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 plot 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 labels. 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.
An integer that sets the number of bins of the histogram. Is overridden by a non-null value for either 'binwidth', 'bin_breaks' or 'bins_class'. Default is 20.
A numeric that sets the number of bins based on this value. If 'aes_x' is a date variable then 'binwidth' is the number of days and if a time variable then 'binwidth' is the number of seconds.
A numeric vector that sets the number of bins by giving the bin boundaries explicitly.
A character string that sets the number of bins by selecting one of three types of formulas. Acceptable values are "Sturges", "Scott", or "FD".
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 set the alpha component attribute to 'bar_color'.
A numeric that sets the bar's outline line width attribute.
A numeric 2 element vector or function that sets the minimum and maximum for the x axis. Use NA to refer to the existing minimum and maximum.
A numeric vector or function that sets the major tic locations along the x axis.
A numeric vector or function that sets the minor tic locations along the x axis.
A character vector or function giving x axis tic labels. Must be the same length as 'x_major_breaks'.
A numeric that sets the number of decimal places for x-tic labels.
A logical which if TRUE
will put the x-tic labels in scientific notation.
A logical which if TRUE
will use a log10 scale for the x axis.
A numeric 2 element vector or function 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 sets the major tic locations along the y axis.
A numeric vector or function that sets the minor tic locations along the y axis.
A character vector or function giving y axis tic labels. Must be the same length as 'y_major_breaks'.
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 logical which if TRUE
will label each bar with its value.
A numeric that sets the size of the bar labels
A string that sets the color of the bar labels
A logical which if TRUE
plots a line for each observation along the axis margin.
A numeric that sets the length of the 'plot_obs' lines.
A string that sets the color of the 'plot_obs' lines.
A numeric that sets the x-intercept for plotting a bold vertical line.
A string that sets the color of 'bold_x'.
A string that set the linetype of 'bold_x'.
A logical that controls the appearance of major grids.
A logical that controls the appearance of minor grids.
A logical that controls the appearance of the legend.
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 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(rlang)
library(RplotterPkg)
RplotterPkg::create_histogram_plot(
df = RplotterPkg::midwest,
aes_x = "Area",
binwidth = 0.01,
x_limits = c(0.0, 0.11),
x_major_breaks = seq(0.0, 0.11, 0.01),
title = "Distribution of area",
subtitle = "437 counties from midwest dataset",
x_title = "Area",
y_title = "Count",
bar_color = "white",
bar_lwd = 2.0,
bar_fill = "brown",
do_coord_flip = TRUE,
bar_labels = TRUE,
bar_label_size = 4,
bar_label_color = "blue",
rot_y_tic_label = TRUE,
silent_NA_warning = TRUE,
plot_obs = TRUE,
plot_obs_color = "darkorange"
)
Run the code above in your browser using DataLab