I would appreciate your help in the following: I'm trying to build a stacked barplot. The fill is the split of business (new and renewed), the Y axis is volume of new/renewed business and the X would be the month. In my data I have two years of history, so I would like to have two stacked columns per month, one for n-1 year and one for n year. However, I don't know how to do this last step...
To clarify, please find below a picture of the data, the plot I have so far and a drawing of my goal.
And here is the code:
ggplot(BUSINESS, aes(fill=Business, y=GWP_mio, x=Date)) +
geom_bar(position="stack", stat="identity") +
scale_fill_manual(values = c("#009E73","Darkblue")) +
scale_y_continuous(breaks=seq(0,18000000,1000000), labels = scales::comma_format(scale = 1/1000000,
accuracy = .1), limits = c(0,18000000)) +
theme_ipsum() +
ggtitle('GWP development') +
theme(plot.title = element_text(hjust=0.5, size=14, family="Calibri", face="bold"),
legend.title = element_text(size=11, family="Calibri", face="bold"),
axis.title.x = element_text(hjust=1, size=11, family="Calibri", face="bold"),
axis.text.x = element_text(angle=90, hjust=1, size=11, family="Calibri"),
axis.title.y = element_text(angle=0, hjust=1, size=10, family="Calibri", face="bold"))
Any help would be highly appreciated.
What you're asking for is both position= 'dodge'
and position= 'stack'
. I'd actually suggest using faceting:
library(data.table)
library(ggplot2)
N <- 24
dat <- data.table(
date= rep(seq.Date(as.Date('2017-01-01'), as.Date('2018-12-01'), '1 month'), each= 2)
, new= rpois(n= 2 * N, lambda= 5)
, renew= rpois(n= 2 * N, lambda= 4)
)
dat[,year := data.table::year(date)]
dat[,month:= data.table::month(date)]
dat <- melt(dat, id.vars= c("date", "year", "month"), variable.name= 'business_type', value.name= 'units')
This is going to be much easier for the viewer.
ggplot(dat, aes(x= month, y= units, fill= factor(year))) +
geom_bar(position= 'dodge', stat='identity') + facet_grid(business_type ~ .) +
theme(axis.text.x= element_text(angle= 90))
But that's not what you asked for. So let's do something hacky. You'll have to mess around with the colours / fill to get exactly what you want. But here we add a first layer for the total, then a second layer for the new
N <- 24
dat <- data.table(
date= rep(seq.Date(as.Date('2017-01-01'), as.Date('2018-12-01'), '1 month'), each= 2)
, new= rpois(n= 2 * N, lambda= 5)
, renew= rpois(n= 2 * N, lambda= 4)
)
dat[,year := data.table::year(date)]
dat[,month:= data.table::month(date)]
dat[, total := new + renew]
ggplot(dat, aes(x= month, y= total, fill= factor(year))) +
geom_bar(stat= 'identity', position= 'dodge', ) +
geom_bar(data= dat, aes(x= month, y= new, fill= "black", colour= factor(year)), stat= 'identity', position= 'dodge') +
scale_color_brewer(palette = "Dark2")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.