How do I make a line graph that combines multiple entries per day over a period of time?

0

Issue

Not sure if my title makes sense, but I’m trying to graph out spending over time. Here is my dput:

structure(list(case = c(1L, 1L, 1L, 1L, 1L, 2L), year = c(2021L, 
2021L, 2021L, 2021L, 2021L, 2021L), month = c(11L, 11L, 11L, 
11L, 11L, 11L), day = c(11L, 11L, 11L, 11L, 11L, 12L), type1 = c("food", 
"food", "drink", "misc", "drink", "drink"), type2 = c("restaurant", 
"tang_kee", "coffee", "headphone", "cola", "coffee_grounds"), 
    amount = c(210, 12, 9, 50, 18, 39)), row.names = c(NA, 6L
), class = "data.frame")

When I just make a simple line graph with gglines, it makes this:

ggline(slack_budget,
         x="case",
         y="amount")+
  theme_bw()+
  labs(title = "Progression of Spending November and December",
       subtitle = "Non-Rent / Non-Utility Spending Line Graph",
       caption = "*Data obtained from local matrix.")+
  theme(plot.title = element_text(face = "bold"),
        plot.caption = element_text(face = "italic"))

Which creates this:

enter image description here

As you can see here, it graphs a point for every individual entry for spending on that day. This looks a little ugly in my opinion because it doesn’t really tell a picture of how much is actually being spent day to day in total.

What I’m more looking for is combining all the entries for each day without combining each month’s spending (hence why I have x = case instead of x = day). If there is an easier way of achieving that, it would be great to know.

Solution:

Thanks to Stefan for his answer. I have now created this lovely plot after aggregating the data:

enter image description here

Solution

One option would be to aggregate your data per day/case to show the total amount of spending per day as a line graph:

library(ggpubr)

slack_budget_sum <- aggregate(amount ~ case + year + month + day, data = slack_budget, FUN = sum)

base <- ggline(slack_budget_sum,
       x="case",
       y="amount")+
  theme_bw()+
  labs(title = "Progression of Spending November and December",
       subtitle = "Non-Rent / Non-Utility Spending Line Graph",
       caption = "*Data obtained from local matrix.")+
  theme(plot.title = element_text(face = "bold"),
        plot.caption = element_text(face = "italic"))

base

And if you still want to show the single purchases you could do so via an additional geom_point using the original non-summarized data:

base + 
  geom_point(data = slack_budget, color = "steelblue")

Answered By – stefan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More