如何制作在一段时间内每天组合多个条目的折线图?
How do I make a line graph that combines multiple entries per day over a period of time?
不确定我的标题是否有意义,但我正在尝试绘制随时间变化的支出图表。这是我的输出:
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")
当我用 gglines 制作一个简单的折线图时,它是这样的:
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"))
创建这个的:
正如您在此处看到的,它为当天花费的每个单独条目绘制了一个点。在我看来,这看起来有点难看,因为它并没有真正说明每天实际花费了多少。
我更想寻找的是合并每天的所有条目而不合并每个月的支出(因此我使用 x = case 而不是 x = day)。如果有更简单的方法来实现这一点,那将是很棒的。
解决方案:
感谢 Stefan 的回答。我现在在汇总数据后创建了这个可爱的图:
一种选择是汇总每个 day/case 的数据,以折线图的形式显示每天的支出总额:
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
如果您仍想显示单次购买,您可以通过使用原始非汇总数据的额外 geom_point
来实现:
base +
geom_point(data = slack_budget, color = "steelblue")
不确定我的标题是否有意义,但我正在尝试绘制随时间变化的支出图表。这是我的输出:
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")
当我用 gglines 制作一个简单的折线图时,它是这样的:
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"))
创建这个的:
正如您在此处看到的,它为当天花费的每个单独条目绘制了一个点。在我看来,这看起来有点难看,因为它并没有真正说明每天实际花费了多少。
我更想寻找的是合并每天的所有条目而不合并每个月的支出(因此我使用 x = case 而不是 x = day)。如果有更简单的方法来实现这一点,那将是很棒的。
解决方案:
感谢 Stefan 的回答。我现在在汇总数据后创建了这个可爱的图:
一种选择是汇总每个 day/case 的数据,以折线图的形式显示每天的支出总额:
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
如果您仍想显示单次购买,您可以通过使用原始非汇总数据的额外 geom_point
来实现:
base +
geom_point(data = slack_budget, color = "steelblue")