Reputation: 31
I'm trying to create a stacked bar chart where the important sub categories are represented by a label, but the smaller sub categories don't appear (as this would really clutter the chart). I've now made a subset to only display labels where the value is over 20 for that sub category, but some of the labels are not centered correctly even though I've used position = position_stack(vjust = 0.5)
EDIT: for example of labels which are centered correctly, see the 'housing' column, where property rates doesn't sit within its stack - its actually on a line between 2 stacks. Another example is in the 'Miscellaneous' column, where insurance is correct, but 'personal care' should be higher up, in the larger stack.
If anyone has a better way of displaying these labels that would also be appreciated as I'm not really happy with how this chart is going to look even if the labels do center properly, but i just cant think of a better way to do it.
here is my code for the graph;
##stacked column chart for expenditure breakdowns
ggplot(expenditureDS, aes(fill = Category, y=AvgExpenditurePerWeek, x=Category)) +
geom_bar(stat="identity",
width = 0.6,
color = "black",
size = 0.5,
alpha = 0.7) +
theme_minimal() +
theme(legend.position = "none", axis.text.x = element_text(angle = 45, hjust = 0.8)) +
labs(x= "Categories", y = "Expenditure Per Week", title = "All Households in NZ - Expenditure Breakdowns",
caption = "2008-2020 data") +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()) +
theme(plot.title = element_text(hjust = 0.5)) +
geom_text(data=subset(expenditureDS, AvgExpenditurePerWeek > 20), aes(label = SubCategory), position = position_stack(vjust = 0.5), size = 2.5) +
scale_y_continuous(labels= dollar_format(prefix="$"))
Upvotes: 1
Views: 1163
Reputation: 66415
Change
geom_text(data=subset(expenditureDS, AvgExpenditurePerWeek > 20),
aes(label = SubCategory),
position = position_stack(vjust = 0.5), size = 2.5) +
to
geom_text(aes(label = if_else(AvgExpenditurePerWeek > 20, SubCategory, "")),
position = position_stack(vjust = 0.5), size = 2.5) +
Otherwise your text layer is only stacking a subset of values, which have lower totals than the full data.
Compare these:
ggplot(mtcars, aes(cyl, wt)) +
geom_col(color = "white", fill = "gray80") +
geom_text(data = mtcars %>% filter(wt > 4), aes(label = wt),
position = position_stack(vjust = 0.5))
ggplot(mtcars, aes(cyl, wt)) +
geom_col(color = "white", fill = "gray80") +
geom_text(aes(label = if_else(wt > 4, as.character(wt), "")),
position = position_stack(vjust = 0.5))
Upvotes: 3