[英]How to plot two columns in a stacked barplot
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.
填充是业务的拆分(新的和续订的),Y 轴是新/续订的业务量,X 是月份。 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.
在我的数据中,我有两年的历史记录,所以我希望每个月有两个堆叠的列,一个用于 n-1 年,一个用于 n 年。 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'
.你要的是
position= 'dodge'
和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.
所以让我们做一些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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.