{"id":4253,"date":"2024-08-17T18:12:06","date_gmt":"2024-08-18T02:12:06","guid":{"rendered":"https:\/\/crm270s.abramjmeister.com\/?p=4253"},"modified":"2024-08-17T18:12:06","modified_gmt":"2024-08-18T02:12:06","slug":"%ec%a7%80%ec%b6%9c-%ec%95%a1%ec%88%98-csv-pie-graph-with-exclusions-of-items-in-r","status":"publish","type":"post","link":"https:\/\/crm270s.mycpamytax.com\/?p=4253","title":{"rendered":"\uc9c0\ucd9c-\uc561\uc218.csv PIE Graph with exclusions of items in R"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># Install and load the necessary packages. Below three lines can be dropped if installed already.\ninstall.packages(\"RSQLite\")\ninstall.packages(\"DBI\")\ninstall.packages(\"ggplot2\")  # This package is used for plotting\ninstall.packages(\"ggrepel\")  # This package is used to avoid label clustering\nlibrary(RSQLite)\nlibrary(DBI)\nlibrary(ggplot2)\nlibrary(ggrepel)\n\n# Define the path to your SQLite database\ndb_path &lt;- \"\/home\/jbyungrokim\/CSV\/CSV.db\"\n\n# Connect to the SQLite database\nconn &lt;- dbConnect(RSQLite::SQLite(), dbname = db_path)\n\n# Corrected query to get the total spending by each item for July 2024, excluding 'Rent - Monthly' and 'Grocery'\nquery &lt;- \"\nSELECT \n    Item, \n    SUM(Pay_Amount) AS Total_Spent\nFROM \n    '\uc9c0\ucd9c-\uc561\uc218_2024_08_17'\nWHERE \n    substr(Date_Transaction, 1, 2) = '07' \n    AND substr(Date_Transaction, 7, 2) = '24'\n    AND Item NOT IN ('Rent - Monthly', 'Grocery')\nGROUP BY \n    Item\nORDER BY \n    Total_Spent DESC;\n\"\n\n# Execute the query and store the result in a data frame\njuly_spending &lt;- dbGetQuery(conn, query)\n\n# Close the connection to the SQLite database\ndbDisconnect(conn)\n\n# Create a pie chart using ggplot2 and ggrepel\nggplot(july_spending, aes(x = \"\", y = Total_Spent, fill = Item)) +\n  geom_bar(width = 1, stat = \"identity\") +\n  coord_polar(\"y\") +\n  labs(title = \"Spending Breakdown by Item - July 2024 (Excluding Rent and Grocery)\") +\n  theme_minimal() +\n  theme(axis.title.x = element_blank(),\n        axis.title.y = element_blank(),\n        panel.grid = element_blank(),\n        axis.text.x = element_blank(),\n        axis.ticks = element_blank()) +\n  geom_text_repel(aes(label = paste0(round(Total_Spent \/ sum(Total_Spent) * 100, 1), \"%\")),\n                  position = position_stack(vjust = 0.5),\n                  box.padding = 0.5,\n                  direction = \"y\",\n                  segment.color = \"grey50\")\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[26],"tags":[],"class_list":["post-4253","post","type-post","status-publish","format-standard","hentry","category-codes"],"_links":{"self":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/4253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4253"}],"version-history":[{"count":0,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/4253\/revisions"}],"wp:attachment":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}