0

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.

enter image description here

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.

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 3
    Welcome to SO, Seoyoung Park! Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly. – r2evans Mar 08 '23 at 01:50
  • It sounds like your issue is the errors not anything specifically to do with labelling. What are the other errors you're getting? You will need to fix them before anything will work. Per the earlier comment, consider adding a minimal reproducible example so people can actually help with the problem https://stackoverflow.com/help/minimal-reproducible-example – Scransom Mar 08 '23 at 02:52
  • Just speculating because I have no data to try it, but: You provide the two lines in different colors via separate data and geoms (uncommon, but fine). So there is only one color per geom, yet you add a color scale. I suspect an inconsistency in between the geom's and the manual color scale. Try this: remove `scale_color_manual(...)`, put the `color = "..."` arguments in the `geom_line`s _outside_ of `aes(...)`, and give the color codes instead of the category names. – benimwolfspelz Mar 08 '23 at 10:47

0 Answers0