Stripe month by month revenue graph generating R code

# Install and load the necessary packages.  Below three lines can be dropped after installation once.
install.packages("RSQLite")
install.packages("DBI")
install.packages("ggplot2")
library(RSQLite)
library(DBI)
library(ggplot2)

# Define the path to your SQLite database
db_path <- "/home/jbyungrokim/CSV/CSV.db"

# Connect to the SQLite database
conn <- dbConnect(RSQLite::SQLite(), dbname = db_path)

# Query to calculate the monthly total of the 'gross' field, grouped by year and month
query <- "
SELECT 
    strftime('%Y-%m', created) AS YearMonth, 
    SUM(gross) AS Total_Gross
FROM 
    'Itemized_balance_change_from_activity_USD_2022-03-11_to_2024-08-05_America-Anchorage'
WHERE 
    strftime('%Y-%m', created) IS NOT NULL
GROUP BY 
    YearMonth
ORDER BY 
    YearMonth;
"

# Execute the query and store the result in a data frame
monthly_totals <- dbGetQuery(conn, query)

# Close the connection to the SQLite database
dbDisconnect(conn)

# Convert YearMonth to a Date type for better plotting
monthly_totals$YearMonth <- as.Date(paste0(monthly_totals$YearMonth, "-01"), format = "%Y-%m-%d")

# Create a line plot using ggplot2
ggplot(monthly_totals, aes(x = YearMonth, y = Total_Gross)) +
  geom_line(color = "blue", linewidth = 1) +  # Updated to use 'linewidth' instead of 'size'
  geom_point(color = "red", size = 2) +
  labs(title = "Monthly Total Gross Over Time",
       x = "Month-Year",
       y = "Total Gross") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Leave a Reply

Your email address will not be published. Required fields are marked *