I want to add an (n=xxx) count for the total number of values in each bar above each bar on my plot. I have calculated the counts in the total_counts
dataframe.
I’ve tried using geom_text
to add the labels, but I cannot get the labels to sit above the bars themselves without causing rescaling of the y axis to include the label values, which compresses the bars down.
I wonder if anyone who is ggplot expert may be able to help please?
The dummy data frame can be downloaded from my google drive (https://shorturl.at/YphZ7)
# Import dummy_data.csv from your own path
# Calculate total counts for each predictor level
total_counts <- aggregate(outcome ~ predictor, data = dummy_data, FUN = length)
colnames(total_counts) <- c("predictor", "count")
# Plot
ggplot(dummy_data, aes(x = predictor, fill = outcome)) +
geom_bar(position = "fill") +
labs(y = "Proportion", x = "predictor ", fill = "Outcome") +
scale_y_continuous(labels = scales::percent_format()) +
theme_minimal() +
ggtitle("Distribution of outcome across predictor Levels")