## Code

```
# Create a function to create the discounted bar chart
library(ggplot2)
# Helper function to create discounted values
<- function(value, time, discount_rate, start) {
create_discount_data <- seq(start, time, by = 1)
times data.frame(
t = times,
group = as.character(time),
value = value * discount_rate^(time - times)
)
}
# Main function to create the discounted bar chart
<- function(smaller, t_s, larger, t_l, discount_rate, starting_at = 0, y_spacing = 20, x_spacing = 1) {
create_discounted_bar_chart # Create the data
<- data.frame(
data t = c(t_s, t_l),
U_t = c(smaller, larger)
)
# Create the discounted values, starting from 'starting_at'
<- rbind(
discounted_data create_discount_data(smaller, t_s, discount_rate, starting_at),
create_discount_data(larger, t_l, discount_rate, starting_at)
)
# Shift t values based on starting_at
$t_plot <- data$t - starting_at
data$t_plot <- discounted_data$t - starting_at
discounted_data
# Filter out any data points before the starting point
<- data[data$t >= starting_at, ]
data <- discounted_data[discounted_data$t >= starting_at, ]
discounted_data
# Determine x-axis and y-axis limits
<- 0
x_min <- max(max(data$t_plot), max(discounted_data$t_plot))
x_max <- max(max(data$U_t), max(discounted_data$value)) * 1.1 # 10% buffer
y_max
# Create the plot
ggplot() +
# Add the bars
geom_rect(data = data, aes(xmin = ifelse(t_plot == 0, 0, t_plot - 0.15),
xmax = ifelse(t_plot == 0, 0.15, t_plot + 0.15),
ymin = 0, ymax = U_t),
fill = "white", color = "black") +
# Add the discount lines
geom_line(data = discounted_data, aes(x = t_plot, y = value, group = group),
color = "black", linewidth = 1) +
# Customize the plot
scale_x_continuous(breaks = seq(x_min, x_max + 1, by = x_spacing),
limits = c(x_min, x_max + 1),
expand = c(0, 0),
labels = function(x) x + starting_at) +
scale_y_continuous(breaks = seq(0, y_max, by = y_spacing),
limits = c(0, y_max),
expand = c(0, 0)) +
geom_vline(xintercept = 0, linewidth = 0.25) +
geom_hline(yintercept = 0, linewidth = 0.25) +
labs(x = "t",
y = expression(U[t])) +
theme_minimal() +
theme(
axis.title.y = element_text(angle = 0, vjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) }
```