examples/basics/ggplot-basics-1.R

# Load the packages: tidyverse includes ggplot2
library(tidyverse)
library(clcharts)

# Set the random seed to make the output reproducible
set.seed(2001)

# Create some random data to plot
df <- tibble(
    time = 1:10,
    value = time + 5 + rnorm(10))

# Use the ggplot function to set up the plot with the data
plot <- ggplot(
        # Specify that df contains the data
        data = df,
        # Use aes to map time to the x axis and value to the y axis
        mapping = aes(x = time, y = value)) +

    # Use the geom_line function to indicate the data should be drawn as a line
    geom_line(color = "#006548", size = 1.1) +

    # Use the labs function to set the title, subtitle, and axis titles
    labs(
        title = "Something has increased",
        subtitle = "The value of something over time",
        x = "Time",
        y = "Value") +

    # Use the scale_x and scale_y functions to control each axis
    scale_x_continuous(breaks = seq(0, 10, 2)) +
    scale_y_continuous(limits = c(0, 20)) +

    # Use the theme_commonslib function to set the plot style
    theme_commonslib()

# Save the plot
save_png(plot, "ggplot-basics-1.png", width = 8, height = 5)
houseofcommonslibrary/clcharts documentation built on June 10, 2025, 9:16 p.m.