Reputation: 1122
I want to plot a multiple stacked bar chart, but I don't know how to combine the r code.
Closing Date Non Current Assets Current Assets Non Current Liabilities
2 2013/12 13637344 13078654 9376243
3 2014/12 14075507 12772388 8895126
4 2015/12 14578093 14226181 9715914
5 2016/12 10911628 10205708 9810157
6 2017/12 10680998 10950779 13493110
Current Liabilities
2 5075985
3 4963856
4 5992229
5 8859263
6 4094183
I can plot multiple bar chart by:
highchart() %>%
hc_chart(type = "column") %>%
hc_xAxis(categories = bs.table$`Closing Date`) %>%
hc_add_series(name="Non Current Assets",data = bs.table$`Non Current
Assets`) %>%
hc_add_series(name="Current Assets",data = bs.table$`Current Assets`) %>%
hc_add_series(name="Non Current Liabilities",data = bs.table$`Non Current Liabilities`) %>%
hc_add_series(name="Current Liabilities",data = bs.table$`Current Liabilities`) %>%
hc_add_theme(hc_theme_ft())
and only one stacked bar like this:
highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "MyGraph") %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = FALSE)
) %>%
hc_series(list(name="Current Assets",data=bs.table$`Current Assets`),
list(name="Non Current Assets",data=bs.table$`Non Current Assets`))
my desired output is like this:
I try to combine the code by adding:
%>% hc_series(list(name="Current Liabilities",data=bs.table$`Current Liabilities`),
list(name="Non Current Liabilities",data=bs.table$`Non Current Liabilities`))
on the second code but it doesn't work, it added up the same bar. Please advice.
Upvotes: 3
Views: 10446
Reputation: 2816
You can specify which bars belong in which groups by adding stack
to each series, like this:
library(highcharter)
bs.table = data.frame(
Closing.Date = paste(2013:2017, 12, sep = "/"),
Non.Current.Assets = c(13637344, 14075507, 14578093, 10911628, 10680998),
Current.Assets = c(13078654, 12772388, 14226181, 10205708, 10950779),
Non.Current.Liabilities = c(9376243, 8895126, 9715914, 9810157, 13493110),
Current.Liabilities = c(5075985, 4963856, 5992229, 8859263, 4094183)
)
highchart() %>%
hc_chart(type = "column") %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_xAxis(categories = bs.table$Closing.Date) %>%
hc_add_series(name="Non Current Assets",
data = bs.table$Non.Current.Assets,
stack = "Assets") %>%
hc_add_series(name="Current Assets",
data = bs.table$Current.Assets,
stack = "Assets") %>%
hc_add_series(name="Non Current Liabilities",
data = bs.table$Non.Current.Liabilities,
stack = "Liabilities") %>%
hc_add_series(name="Current Liabilities",
data = bs.table$Current.Liabilities,
stack = "Liabilities") %>%
hc_add_theme(hc_theme_ft())
(To group by current vs. non-current instead of assets vs. liabilities, just rename stack
in each series appropriately.)
Upvotes: 12