# Load necessary library
library(ggplot2)
# Define the data points
drives <- c(23, 78, 130, 147, 156, 177, 184, 213)
# Convert to data frame
drives_df <- data.frame(distance = drives)
# Create the histogram
ggplot(drives_df, aes(x = distance)) +
geom_histogram(binwidth = 50, fill = "skyblue", color = "black") +
scale_x_continuous(breaks = seq(0, 250, by = 50)) +
scale_y_continuous(breaks = seq(0, 3, by = 1)) +
labs(
title = "Histogram of Luiza's Driving Distances",
x = "Driving distance (meters)",
y = "Number of drives"
) +
theme_minimal()

This R script is designed to visualize the distribution of driving distances using a histogram, leveraging the ggplot2
library, a powerful and widely-used visualization package in R. The script follows a structured approach to create the visualization, starting with loading the necessary library and preparing the data, then constructing and customizing the histogram.
Initially, the ggplot2
library is loaded to access its functions for creating the plot. This is a prerequisite for using ggplot2
's functionality.
The data points representing driving distances are defined as a vector named drives
. These distances are then converted into a data frame, drives_df
, with a single column named distance
. This transformation is crucial because ggplot2
works with data frames, allowing for more complex data structures and facilitating the use of aesthetic mappings and layers in the plot.
The core of the script is the creation of the histogram. This is done using the ggplot
function, which initializes the plot with the data frame drives_df
and sets the aesthetic mapping for the x-axis to the distance
column. The geom_histogram
function is then added to create the histogram, with a specified binwidth
of 50, which determines the width of each bin in the histogram. The fill
and color
arguments customize the appearance of the bins, making the plot visually appealing.
To enhance readability, the x and y axes are customized using scale_x_continuous
and scale_y_continuous
, respectively. These functions are used to define the breaks on the axes, making the plot easier to interpret by ensuring that the axis ticks are placed at regular intervals.
Finally, the plot is further customized with titles for the plot and axes using the labs
function, and a minimalistic theme is applied using theme_minimal()
. This results in a clean and professional-looking histogram that effectively communicates the distribution of Luiza's driving distances.