Reaching $15M in Weekly Donations - Tracking Goals with R Time Series Graphs

I decided to donate $20,000 every week and $50,000 once a month (instead of $20,000) starting November 20, 2022, and have been doing so ever since. I want to see a time series graph of when I reach my goal of $15 million.

Today, we'll look at how you can track this giving plan as a time series and visualize when you reach your goal with R code, so you can see how the cumulative amount grows as you give each month and each week in a time series graph.

1. Set a giving plan and goals

시계열 그래프 그림 - 목표

Here's my planned giving goal. Let's calculate when I'll reach my goal based on my giving plan below.

  • Weekly Donate $20 in increments.
  • Once a monthis a $50 donation.
  • Target Amountis 15 million won.

2. load the required packages

First, you need to create a lubridate Packagesfor graphing and ggplot2 Packagesin the file.

# Loading the required packages
library(lubridate)
library(ggplot2)

Description: lubridate package makes date calculations convenient, ggplot2is a package for data visualization.

3. initialize variables

Define the start date, goal amount, weekly donation amount, monthly donation amount, and today's date as variables.

Initialize the # variable
start_date <- as.Date("2022-11-20")
end_goal <- 15000000 # goal of 15 million won
weekly_donation <- 20000 # 20,000 every week
monthly_donation <- 50000 # 50,000 won per month
today <- as.Date("2024-11-10")

Description:

  • start_date: The start date of the donation.
  • end_goal: The target amount (15,000,000 won).
  • weekly_donation: The amount you donate each week (20,000 won).
  • monthly_donation: The amount of additional donations you make each month (50,000 won).
  • today: The date you set to check the donation progress based on the current date.

4. create a data frame to store dates and accumulated amounts

Starting November 20, 2022, you can create a date that becomes a weekly donation day by creating a dates variable, and save it to the data frame donationsin the file.

# Create a data frame to store dates and accumulated amounts
dates <- seq(from = start_date, by = "week", length.out = 520) # through December 31, 2032
donations <- data.frame(Date = dates, Donation = 0)

Description:

  • dates: A list of dates corresponding to each week's donation day. Set the maximum length to 520 weeks to generate dates through the end of 2032.
  • donationsA data frame to record each date and the cumulative donation amount for that date.

5. calculate donation accumulation

The loop accumulates the donation amount each week, adding an additional $5 on the fourth week of each month to accumulate the total donation amount. End the iteration when the goal amount is reached.

# Donation Accumulation Calculation
total_donation <- 0
for (i in 1:nrow(donations)) {
    if (i %% 4 == 0) { # add 50,000 won per month
        total_donation <- total_donation + weekly_donation + monthly_donation
    } else {
        total_donation <- total_donation + weekly_donation
    }
    donations$Donation[i] = end_goal) {
        Calculate goal date for #
        goal_date <- donations$Date[i]
        break
    }
}

Description:

  • for Weekly through the loop weekly_donation Accumulate the amount.
  • if (i %% 4 == 0): every fourth week of every month monthly_donationto the end of the file.
  • donations$Donation[i] <- total_donation: Records the cumulative donation amount for each date.
  • When you reach your goal amount goal_date Save the goal achievement date in a variable and end the iteration.

6. check the current cumulative donation amount

The date you set (today) to see the cumulative donation amount.

# Check the current accumulated amount
current_donation <- donations$Donation[which(donations$Date <= today)]
current_donation <- tail(current_donation, 1)

Description: current_donationis today View the cumulative donation amount based on the date, saving only the most recent value to represent the current cumulative amount.

7. Create an R graph

Visualize cumulative donations to create a time-series graph with goal amounts and goal achievement dates.

Generate a # R graph
p <- ggplot(donations, aes(x = Date, y = Donation)) +
    geom_line(color = "blue") +
    geom_hline(yintercept = end_goal, linetype = "dashed", color = "red") +
    geom_vline(xintercept = as.numeric(goal_date), linetype = "dotted", color = "green") +
    labs(
        title = "Cumulative Donation Time Series Graph",
        subtitle = paste("Estimated Goal Achievement Date:", format(goal_date, "%Y-%m-%d")),
        x = "Date",
        y = "Cumulative Donation (won)"
    ) + +
    annotate("text", x = goal_date, y = end_goal, label = "Goal Achieved", vjust = -1.5, color = "green") +
    theme_minimal()
    scale_x_date(limits = c(start_date, as.Date("2032-12-31")))

print(p)

Description:

  • geom_line(color = "blue"): Visualize the cumulative increase in donation amount as a blue line.
  • geom_hline(yintercept = end_goal, linetype = "dashed", color = "red"): Shows the target amount as a red dotted line.
  • geom_vline(xintercept = as.numeric(goal_date), linetype = "dotted", color = "green"): Shows the goal achievement date with a green dotted line.
  • labs(): Specify a graph title, subtitle, and axis labels. In the subheading, indicate the expected date of goal achievement.
  • annotate("text"): Add a "Goal Achieved" label to the goal achievement date to highlight the milestone.
  • theme_minimal(): Simplifies the graph theme so that the data is emphasized.
  • scale_x_date(limits = c(start_date, as.Date("2032-12-31")))): Adjust the scope of the visualization by limiting the range on the x-axis to between the start date and December 31, 2032.
시계열 그래프 목표-실적

Wrap-up: Visualize a time-series graph of your goal and cumulative donations

In this post, we've plotted our weekly and monthly giving plans in a time series graph to see when we hit our goals. This visualization gives you an intuitive sense of whether or not you're meeting your giving goals, and will help you track your giving goals going forward. We hope you'll try this code to track your own giving plans too!

Similar to time series graphs, you'll also want to visualize how you're performing against your goals at a glance. How do I plot a graph of performance against goals? Check out the post to learn how to do just that!

# Full Code

# Loading the required packages
library(lubridate)
library(ggplot2)

Initialize # variables
start_date <- as.Date("2022-11-20")
end_goal <- 15000000 # goal of 15 million won
weekly_donation <- 20000 # 20,000 won every week
monthly_donation <- 50000 # 50,000 won per month
today <- as.Date("2024-11-10")

Create a data frame to store the # dates and accumulated amounts
dates <- seq(from = start_date, by = "week", length.out = 520) # Until December 31, 2032
donations <- data.frame(Date = dates, Donation = 0)

# Calculate cumulative donations
total_donation <- 0
for (i in 1:nrow(donations)) {
    if (i %% 4 == 0) { # adds 50,000 won every month
        total_donation <- total_donation + weekly_donation + monthly_donation
    } else {
        total_donation <- total_donation + weekly_donation
    }
    donations$Donation[i] = end_goal) {
        Calculate goal date for #
        goal_date <- donations$Date[i]
        break
    }
}

# Check the current accumulated amount
current_donation <- donations$Donation[which(donations$Date <= today)]
current_donation <- tail(current_donation, 1)

Generate the # R graph
p <- ggplot(donations, aes(x = Date, y = Donation)) +
    geom_line(color = "blue") +
    geom_hline(yintercept = end_goal, linetype = "dashed", color = "red") +
    geom_vline(xintercept = as.numeric(goal_date), linetype = "dotted", color = "green") +
    labs(
        title = "Cumulative Donation Time Series Graph",
        subtitle = paste("Estimated Goal Achievement Date:", format(goal_date, "%Y-%m-%d")),
        x = "Date",
        y = "Cumulative Donation (won)"
    ) + +
    annotate("text", x = goal_date, y = end_goal, label = "Goal Achieved", vjust = -1.5, color = "green") +
    theme_minimal()
    scale_x_date(limits = c(start_date, as.Date("2032-12-31")))

print(p)

Similar Posts