I want to add direct labels on both two geom_line graphs, and I tried lots of times by using directlabels packages. However, the error always happened and I do not know what is exactly wrong with my code. The raw data is in csv file, so I will upload example picture of dataframe since I cannot upload excel files on stack overflow.
And here is my code.
living = c("insurance", "housing", "utilities", "food", "transportation", "healthcare", "debt")
discretionary = c("recreation", "personal", "gifts", "vacation")
dat2 = dat %\>%
group_by(month = lubridate::floor_date(date, "month")) %\>%
filter(category %in% living) %\>%
summarise(cost_of_living_spending = sum(amount))
dat3 = dat %\>%
group_by(month = lubridate::floor_date(date, "month")) %\>%
filter(category %in% discretionary) %\>%
summarise(discretionary_spending = sum(amount))
gg = ggplot() +
geom_line(data = dat2, aes(x = month, y = cost_of_living_spending, group = 1, color = "cost_of_living_spending"),
linewidth = 0.8, stat = "identity") +
geom_line(data = dat3, aes(x = month, y = discretionary_spending, group = 1, color = "discretionary_spending"),
linewidth = 0.8, stat = "identity") +
ggtitle("Comparison between living and discretionary spending over time") +
scale_color_manual(values = c("#FFC20A", "#0C7BDC" )) +
scale_x_continuous(name = "date", breaks = seq.Date(as.Date("2018-06-01"), as.Date("2022-12-01"), by = "6 months"), labels = c("2018.06.", "2018.12.", "2019.06.", "2019.12.", "2020.06", "2020.12.", "2021.06.", "2021.12.", "2022.06.", "2022.12.")) +
scale_y_continuous(name = "spending ($)", limits = c(0, 10000), breaks = seq(0, 10000, 2500)) +
theme(plot.background = element_rect(fill = "gray96"),
panel.background = element_rect(fill = "gray96"),
panel.grid.major.x = element_line(color = "gray88"),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(color = "gray88", size = 0.3, linetype = 1),
panel.grid.minor.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(size = 12, color = 'black'),
plot.subtitle = element_text(size = 10),
axis.title.x = element_text(size = 10),
axis.title.y = element_text(size = 10),
axis.text.x = element_text(size = 8),
axis.text.y = element_text(size = 8))
gg
direct.label(gg, aes(color = color))
I want to add direct labels on my two geom_line graph. However, the error, "Continuous value supplied to discrete scale" occurred. Then I tried to fix this error, but another error also occurred, and so on.
In conclusion, I expected to add direct labels in ggplot.