Highcharter R堆叠式条形图中的多个系列 [英] multiple series in Highcharter R stacked barchart

查看:267
本文介绍了Highcharter R堆叠式条形图中的多个系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在经历了highcharter包文档,访问JBKunst他的网站并查看list.parse2()之后,我仍然无法解决问题。问题如下:从一个data.frame到多个条形图的图表,系列可以是10-30系列的任何地方。现在系列已经被定义如下,但显然必须有一个更简单的方法,例如将一个列表或融化的data.frame传递给函数hc_series,类似于ggplot2可以完成的功能。



低于带有虚拟数据的代码

  mydata < - 数据.frame(A = runif(1:10),
B = runif(1:10),
C = runif(1:10))

highchart()% >%
hc_chart(type =column)%>%
hc_title(text =MyGraph)%>%
hc_yAxis(title = list(text =Weights ))%>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking =normal,
enableMouseTracking = FALSE)
)%>%
hc_series(list(name =A,data = mydata $ A),
list(name =B,data = mydata $ B),
list(name =C,data = mydata $ C))

b $ b

解决方案

使用 hc_add_series_list (你可以使用for循环),它需要一系列的列表(例如,一系列列表,例如 list(name =A,data = mydata $ A)

正如你所说,你需要对数据进行融合/收集,你可以使用 tidyr 包:

  mynewdata < -  gather(mydata)

然后,您需要按 key 参数对数据进行分组,以便为​​每个键创建数据 /系列。在这里,您可以使用 dplyr 包:

  mynewdata2 < -  mynewdata% >%
#我们更改密钥名称以在图例中包含标签
group_by(name = key)%>%
#这种情况下的数据很简单,只是。$ value列
do(data =。$ value)

这个数据框将包含两列和第二列将包含每行的十个值。



现在你需要列表中的这些信息。因此,我们需要使用 list.parse3 instad list.parse2 来解析,使其保留名称如 data

 系列< ;  -  list.parse3(mynewdata2)

所以最后改变:

  hc_series(list(name =A,data = mydata $ A),
list(name =B,data = mydata $ B) ,
list(name =C,data = mydata $ C))



  hc_add_series_list(系列)

希望这是明确的。


After going through the highcharter package documentation, visiting JBKunst his website, and looking into list.parse2(), I still can not solve the problem. Problem is as follows: Looking to chart multiple series from a data.frame into a stacked barchart, series can be anywhere from 10 - 30 series. For now the series have been defined as below, but clearly there has to be an easier way, for example passing a list or melted data.frame to the function hc_series similar as what can be done with ggplot2.

Below the code with dummy data

mydata <- data.frame(A=runif(1:10),
               B=runif(1:10),
               C=runif(1:10))

highchart() %>% 
hc_chart(type = "column") %>% 
hc_title(text = "MyGraph") %>% 
hc_yAxis(title = list(text = "Weights")) %>% 
hc_plotOptions(column = list(
   dataLabels = list(enabled = FALSE),
   stacking = "normal",
   enableMouseTracking = FALSE)
   ) %>% 
hc_series(list(name="A",data=mydata$A),
        list(name="B",data=mydata$B),
        list(name="C",data=mydata$C))

Which produces this chart:

解决方案

a good approach to add multiples series in my opinion is use hc_add_series_list (oc you can use a for loop) which need a list of series (a series is for example list(name="A",data=mydata$A).

As you said, you need to melt/gather the data, you can use tidyr package:

mynewdata <- gather(mydata)

Then you'll need to group the data by key argument to create the data for each key/series. Here you can use dplyr package:

mynewdata2 <- mynewdata %>%
  # we change the key to name to have the label in the legend
  group_by(name = key) %>%  
  # the data in this case is simple, is just .$value column
  do(data = .$value) 

This data frame will contain two columns and the 2nd colum will contain the ten values for each row.

Now you need this information in a list. So we need to parse using list.parse3 instad of list.parse2 beacuse preserve names like name or data.

series <- list.parse3(mynewdata2)

So finally change:

hc_series(list(name="A",data=mydata$A),
    list(name="B",data=mydata$B),
    list(name="C",data=mydata$C))

by:

hc_add_series_list(series)

Hope this is clear.

这篇关于Highcharter R堆叠式条形图中的多个系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆