# 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))
지출-액수.csv PIE Graph with exclusions of items in R
# Install and load the necessary packages. Below three lines can be dropped if installed already.
install.packages("RSQLite")
install.packages("DBI")
install.packages("ggplot2") # This package is used for plotting
install.packages("ggrepel") # This package is used to avoid label clustering
library(RSQLite)
library(DBI)
library(ggplot2)
library(ggrepel)
# 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)
# Corrected query to get the total spending by each item for July 2024, excluding 'Rent - Monthly' and 'Grocery'
query <- "
SELECT
Item,
SUM(Pay_Amount) AS Total_Spent
FROM
'지출-액수_2024_08_17'
WHERE
substr(Date_Transaction, 1, 2) = '07'
AND substr(Date_Transaction, 7, 2) = '24'
AND Item NOT IN ('Rent - Monthly', 'Grocery')
GROUP BY
Item
ORDER BY
Total_Spent DESC;
"
# Execute the query and store the result in a data frame
july_spending <- dbGetQuery(conn, query)
# Close the connection to the SQLite database
dbDisconnect(conn)
# Create a pie chart using ggplot2 and ggrepel
ggplot(july_spending, aes(x = "", y = Total_Spent, fill = Item)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y") +
labs(title = "Spending Breakdown by Item - July 2024 (Excluding Rent and Grocery)") +
theme_minimal() +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_blank(),
axis.ticks = element_blank()) +
geom_text_repel(aes(label = paste0(round(Total_Spent / sum(Total_Spent) * 100, 1), "%")),
position = position_stack(vjust = 0.5),
box.padding = 0.5,
direction = "y",
segment.color = "grey50")
지출-액수.csv monthly sum by Item using R
# Install and load the necessary packages. Below two lines can be dropped after installing once.
install.packages("RSQLite")
install.packages("DBI")
library(RSQLite)
library(DBI)
# 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)
# Adjust the query to handle the MM/DD/YY format
query <- "
SELECT
strftime('%Y-%m', '20' || substr(Date_Transaction, 7, 2) || '-' || substr(Date_Transaction, 1, 2) || '-' || substr(Date_Transaction, 4, 2)) AS YearMonth,
SUM(Pay_Amount) AS Total_Spent
FROM
'지출-액수_2024_08_17'
WHERE
Item = 'Grocery'
GROUP BY
YearMonth
ORDER BY
YearMonth;
"
# Execute the query and store the result in a data frame
monthly_sum <- dbGetQuery(conn, query)
# Print the result
print("Monthly Sum of Pay_Amount for Grocery Items by Year and Month:")
print(monthly_sum)
# Close the connection to the SQLite database
dbDisconnect(conn)
지출-액수.csv R Graph
# Install and load the necessary packages. After installing once, below 3 lines can be dropped.
install.packages("RSQLite")
install.packages("DBI")
install.packages("ggplot2") # This package is used for plotting
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)
# Adjust the query to handle the MM/DD/YY format
query <- "
SELECT
strftime('%Y-%m', '20' || substr(Date_Transaction, 7, 2) || '-' || substr(Date_Transaction, 1, 2) || '-' || substr(Date_Transaction, 4, 2)) AS YearMonth,
SUM(Pay_Amount) AS Total_Spent
FROM
'지출-액수_2024_08_17'
WHERE
Item = 'Grocery'
GROUP BY
YearMonth
ORDER BY
YearMonth;
"
# Execute the query and store the result in a data frame
monthly_sum <- dbGetQuery(conn, query)
# Close the connection to the SQLite database
dbDisconnect(conn)
# Create a plot using ggplot2
ggplot(monthly_sum, aes(x = YearMonth, y = Total_Spent)) +
geom_line(group = 1, color = "blue") +
geom_point(color = "red") +
labs(title = "Monthly Grocery Spending",
x = "Year-Month",
y = "Total Spent on Groceries") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))