The issue is that you did not aggregate your data by month in geom_text
as you did for your bars, i.e. you have to use a stat_summary
with geom="text"
for your labels. Otherwise you end up with multiple labels per month.
However, IMHO an easier approach would be to aggregate your dataframe outside of ggplot()
.
Using some fake random example data:
library(ggplot2)
library(dplyr, warn=FALSE)
set.seed(123)
prism2018 <- data.frame(
Month = rep(
seq.Date(as.Date("2018-01-01"), as.Date("2018-12-01"), by = "month"),
10
),
`ppt (inches)` = runif(120),
check.names = FALSE
)
prism2018 |>
group_by(Month) |>
summarise(`ppt (inches)` = sum(`ppt (inches)`, na.rm = TRUE)) |>
ggplot(aes(Month, `ppt (inches)`)) +
geom_col() +
geom_text(
aes(label = prettyNum(`ppt (inches)`, digits = 3)),
vjust = 0, position = position_nudge(y = .05)
)
