The “geom_line” Function in R

  • Package: ggplot2

  • Purpose: To add a layer of lines to a plot, connecting points in the order of the data frame.

  • General Class: Plot Layer

  • Required Argument(s):

    • None

  • Notable Optional Arguments:

    • mapping: Aesthetic mappings created by the aes function.

    • data: The data frame providing the data for the plot.

    • position: Position adjustment method for overlapping lines.

    • color, size: Aesthetics for line characteristics.

    • linetype: Type of line to draw.

  • Example:

  • # Example usage
    library(ggplot2)

    # Create a data frame with more observations
    my_data <- data.frame(
    Category = rep(c("A", "B", "C", "D"), each = 2),
    Value = c(10, 15, 20, 25, 15, 20, 25, 30)
    )

    # Create a ggplot object with lines
    my_plot <- ggplot(data = my_data, aes(x = Category, y = Value)) +
    geom_line(color = "red", size = 1.5, linetype = "dashed") +
    labs(title = "Line Plot of Values by Category", x = "Category", y = "Value")

    # Print the plot with lines
    print(my_plot)

  • In this example, the geom_line function from the ggplot2 package is used to create a line plot, and optional arguments customize the appearance of the lines.

Previous
Previous

The “facet_wrap” Function in R

Next
Next

The “geom_point” Function in R