The “geom_boxplot” Function in R

  • Package: ggplot2

  • Purpose: To create boxplots in ggplot.

  • General Class: Geometric object for plotting boxplots.

  • Required Argument(s): None

  • Notable Optional Arguments:

    • mapping: Aesthetic mappings.

    • data: The data to be displayed in the plot.

    • stat: The statistical transformation to use on the data for this layer.

    • position: Position adjustment.

    • width: Width of the boxes.

    • fill: Box fill color.

    • color: Box border color.

    • outlier.color: Color for outlier points.

    • outlier.shape: Shape of outlier points.

    • outlier.size: Size of outlier points.

    • notch: Logical, indicating whether to draw a notch around the median.

  • Example:

  • # Example usage
    library(ggplot2)

    # Create a data frame
    my_data <- data.frame(
    group = rep(c("A", "B", "C"), each = 50),
    values = rnorm(150)
    )

    # Create a ggplot object with a boxplot using geom_boxplot
    my_plot <- ggplot(data = my_data, aes(x = group, y = values)) +
    geom_boxplot(fill = "lightblue", color = "blue", notch = TRUE) +
    labs(title = "My Boxplot", x = "Groups", y = "Values")

    # Print the plot
    print(my_plot)

  • In this example, the geom_boxplot function from the ggplot2 package is used to create a boxplot. The function allows customization of aesthetics such as fill color, border color, and the presence of a notch around the median. The required aesthetics are specified in the aes function.

Previous
Previous

The “facet_grid” Function in R

Next
Next

The “geom_bar” Function in R