My question is somewhat similar to this:
I would like to also add a label that will give the total of each stacked bar:
df_t = df[,c(1,2,4)] # df with TOTALS
df_t[,ncol(df_t)]<- sapply(df_t[,ncol(df_t)] , as.numeric)
df_t = df_t %>% group_by(company, SubSegment) %>%
summarise(value = sum(value)) %>% mutate(Label=value)
df_t[,ncol(df_t)]<- sapply(df_t[,ncol(df_t)] , function(x) scales::percent(x, accuracy = 0.1))
If I plotted the totals without the stacked plot I could get something like this:
Basically, I want to "combine" the two plots, or simply add the total labels to the stacked plot. I have tried a few different approaches but I can't get the totals to appear on the right place (at the end of each bar)
for the non-stacked barplot this is what I used to create the labels:
plot.2<- ggplot(df_t, aes(fill=company, y=value, x=company))
geom_bar(stat="identity", position = 'dodge')
scale_y_continuous(limits = c(0, 0.6))
geom_text(aes(label=Label),position=position_dodge(width=0.9), vjust=-0.25, hjust=-0.2)
facet_wrap(~factor(SubSegment ), ncol=1) coord_flip()
CodePudding user response:
You can use your second dataframe df_t
in a new geom_text
where you could add for example 0.03 to value
by specifing the position of your labels. You can use the following code:
library(ggplot2)
plot.1<- ggplot(df, aes(fill=factor(variable, levels = var_levels), y=value, x=company))
geom_bar(position="stack", stat="identity", alpha=1)
scale_y_continuous(limits = c(0, 0.6))
geom_text(aes(label=Label), position = position_stack(vjust = 0.5), size=2.5)
geom_text(aes(company, value 0.03, label = Label, fill = NULL), data = df_t)
facet_wrap(~factor(SubSegment ), ncol=1)
theme(plot.title = element_text(size = 11), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_blank(),
axis.ticks.x = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
legend.position = c(0.88, 0.89) , legend.text = element_text(size = 9.5),
legend.title = element_text(size = 11),
legend.background = element_rect(fill="gray92", linetype="solid",colour ="gray75"))
scale_fill_manual(values=colors, name="SubSegment")
coord_flip()
plot(plot.1)
Output: