R highcharts多个堆积的条形图 [英] R highcharts multiple stacked bar chart
问题描述
我想绘制多个堆叠的条形图,但我不知道如何组合r代码.
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.
推荐答案
您可以通过向每个系列中添加stack
来指定哪些条属于哪些组,如下所示:
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())
(要按流动资产与非流动资产而不是资产与负债进行分组,只需在每个系列中适当地重命名stack
.)
(To group by current vs. non-current instead of assets vs. liabilities, just rename stack
in each series appropriately.)
这篇关于R highcharts多个堆积的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!