Learn R Programming

daltoolbox (version 1.3.767)

plot_bar: Plot Bar Graph

Description

Draw a simple bar chart from a two-column data frame: first column as categories (x), second as values (y).

Usage

plot_bar(data, label_x = "", label_y = "", colors = NULL, alpha = 1)

Value

A ggplot2::ggplot object.

Arguments

data

Two-column data frame: category in the first column, numeric values in the second.

label_x

Label for the x-axis.

label_y

Label for the y-axis.

colors

Optional fill color.

alpha

Bar transparency between 0 and 1.

Details

If colors is provided, a constant fill is used; otherwise ggplot2's default palette applies. alpha controls bar transparency. The first column is coerced to factor when needed, which makes the function convenient for quick summaries of grouped values.

References

Wickham, H. (2016). ggplot2: Elegant Graphics for Data Analysis. Springer.

Examples

Run this code
# summarizing iris dataset
data <- iris |>
  dplyr::group_by(Species) |>
  dplyr::summarize(Sepal.Length = mean(Sepal.Length))
head(data)

# plotting data
grf <- plot_bar(data, colors = "blue")
plot(grf)

Run the code above in your browser using DataLab