堆叠Barplot ggplot2

时间:2019-07-01 17:48:59

标签: r ggplot2 bar-chart

我用以下代码创建了一个堆叠的条形图,是否可以在顶部显示堆叠的条形的总和,以及在每个堆叠中的百分比?

month = c('July', 'August', 'September')
members = c(90493.5, 39853.5, 34087.5)
casual = c(676274, 488700.0, 457538.0)



df = data.frame(month, members, casual)
dfm <- melt(df, id=c("month"))

fill <- c("dodgerblue4", "coral")

ggplot(dfm, aes(x = as.factor(month), y= value, fill = variable)) +
theme_minimal() +
geom_bar(stat="identity", width=.8, colour = 'black', alpha = 0.8) +
geom_text(data=dfm, aes(x = as.factor(month), y = value, label = value), colour="black", size = 3, show.legend = F)+
scale_fill_manual(values=fill) +
theme(legend.position="bottom", legend.direction="horizontal", legend.title = element_blank()) +
theme(legend.key.size = unit(0.6, "cm")) +
theme(legend.text=element_text(size=9), legend.spacing.x = unit(0.2, 'cm') ) +
theme(legend.background = element_rect(fill="white", size=0.1, linetype="solid")) +
labs(title = "Α", y = 'Profit', x = "Month", face = 'bold') + 
theme(plot.title = element_text(size=15, family = 'Times New Roman', face = 'bold')) +
theme(plot.caption = element_blank()) +
theme(plot.subtitle = element_blank()) +
theme(axis.text.y = element_text(size = 11, family = 'Times New Roman', color = "black", face = 'bold')) +
theme(axis.text.x = element_text(size = 13, family = 'Times New Roman', color = "black", face = 'bold')) +
theme(axis.title.y = element_text(size = 15, family = 'Times New Roman', color = "black", face = 'bold')) +
theme(axis.title.x = element_text(size = 15, family = 'Times New Roman', color = "black", face = 'bold')) +
theme(panel.background = element_rect(colour = "gray20", size = 0.6, linetype = "solid") ) +
theme(panel.background = element_rect(colour = "gray20", size = 0.6, linetype = "solid") ) +
theme(panel.grid.major = element_line(color = "grey42", size = 0.15))

0 个答案:

没有答案