'Adding Quarterly totals to Yearly stacked column
I have a very simple data set that has 3 columns: Year, Quarter and Net_Amount. I can create a simple 3 column chart based on year using R, but as soon as I try to add the quarterly data AND add the totals by quarter to the chart, all of the totals, get squashed together on 1 line. This is my code to generate the stacked column chart in R. Here is a small subset of my data
Year Quarter sum_total 1 2020 Q1 0 2 2020 Q2 0 3 2020 Q3 0 4 2020 Q4 1597856189 5 2021 Q1 464188929 6 2021 Q2 499179243 7 2021 Q3 570470331 8 2021 Q4 605997816 9 2022 Q1 654494589 10 2022 Q2 654494579 11 2022 Q3 654494599 12 2022 Q4 654494589
dput(df)
three_yrqtr_df <- df %>%
# change the fiscal_year to character type
mutate(Year = as.character(Fiscal_Year)) %>%
select(Year,Quarter,Net_Amount) %>%
group_by(Year,Quarter) %>%
summarize(sum_total = sum(Net_Amount))
three_yrqtr_df
ggplot(three_yrqtr_df,
aes(x=Year,
y=sum_total,
fill=Quarter,
label = dollar(sum_total)),
vjust = 0.5,
check_overlap = T) +
geom_col(position="stack", color = "black") +
geom_text(aes(x=Year, y = sum_total),vjust = -1) +
scale_y_continuous(labels = label_number_si())
#here is my solution: ggplot(three_yrqtr_df, aes(x=Year, y=sum_total, fill=Quarter, label = dollar(sum_total), group = group), vjust = 0.5, check_overlap = T) + geom_col(position="stack", color = "black") + geom_text(aes(label=dollar(sum_total)), position = position_stack(vjust = 0.9)) + stat_summary(fun = sum, aes(label = dollar(..y..), group = Year, vjust = -1), geom = "text") + scale_y_continuous(labels = label_number_si())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

