Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple series in Highcharter R stacked barchart

Tags:

r

highcharts

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: enter image description here

like image 892
New_code Avatar asked Jun 29 '16 07:06

New_code


People also ask

How do I add a data series to a stacked bar chart?

Right-click the chart, and then choose Select Data. The Select Data Source dialog box appears on the worksheet that contains the source data for the chart. Leaving the dialog box open, click in the worksheet, and then click and drag to select all the data you want to use for the chart, including the new data series.

What is the difference between a stacked and clustered bar chart?

If you want to visualize differences of percentage of sub categories value with other sub categories in other categories; 100% Stacked is the best. If you want to compare values for all sub categories, and understand which one is the best or worst, inside a category or overall; Clustered Chart is your asset.


1 Answers

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.

like image 191
jbkunst Avatar answered Sep 20 '22 10:09

jbkunst