Laura
Laura

Reputation: 553

In highchart in R how to create this line + bar chart

I need to replicate this chart using highchart R package:

https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/stock/stocktools/navigation-annotation-options

I think the answer is in here: series:

[{
            type: 'line',
            id: 'aapl-ohlc',
            name: 'AAPL Stock Price',
            data: ohlc
        }, {
            type: 'column',
            id: 'aapl-volume',
            name: 'AAPL Volume',
            data: volume,
            yAxis: 1
        }]

It looks like I need to use hc_add_series function with 2 series inside, right?

But its not easy for me.

library(highcharter)
library(gapminder)
 
gapminder %>%  filter(country == c("Chile","Argentina")) %>%
    hchart("line",
           hcaes(x = year, y = pop, group = country))

Upvotes: 1

Views: 639

Answers (1)

Kat
Kat

Reputation: 18754

There are many ways you can combine the line and column series. However, to use the range selector, I think that that is limited to chart type "stock". I've put together several examples. The first of which shows you how you can add the range selector. The others show how you can add disparate series.

I modified the data to add a date field. This field is only used in the first example for the range selector. You could just use the field 'year', but it changes the labels on the x-axis to nonsense. That could be changed, as well. I just went with what I thought was a bit easier.

For this graph, note that I have two series calls and two y-axis calls. In the y-axis calls, you'll find relative. relative indicates the size relative to the other sizes you provide. I believe the rest should be somewhat self-explanatory if you have any understanding of highcharter.

library(highcharter)
library(tidyverse)
library(gapminder)

data(gapminder)

gp <- gapminder %>% filter(country %in% c("Chile", "Argentina")) %>% 
  mutate(dt = as.Date(paste0(year, "-01-01"), 
                      format = "%Y-%m-%d", origin = "GMT"))

highchart(type = "stock") %>% hc_rangeSelector(verticalAlign = "bottom") %>% 
  hc_xAxis(type = "datetime") %>% 
  hc_add_series(gp, "line", hcaes(x = dt, y = pop, group = country), yAxis = 0) %>% 
  hc_add_yAxis(title = list(text = "Population"), relative = 2) %>%
  hc_add_series(gp, "column", 
                hcaes(x = dt, y = gdpPercap), yAxis = 1, showInLegend = F) %>% 
  hc_add_yAxis(title = list(text = "GDP per capita"), relative = 1L)

enter image description here

Here are a few other options for arranging the axes.

hchart(gp, "line", hcaes(x = year, y = pop, group = country), yAxis = 0) %>% 
  hc_yAxis(title = list(text = "Population"), relative = 3) %>% 
  hc_add_series(gp, "column", hcaes(x = year, y = gdpPercap),
                yAxis = 1, showInLegend = F) %>% 
  hc_add_yAxis(title = list(text = "GDP"), relative = 1)

enter image description here

highchart() %>% 
  hc_yAxis_multiples(create_axis(2,  height = c(4, 1), turnopposite = T)) %>% 
  hc_add_series(gp, "line", hcaes(x = year, y = pop, group = country), yAxis = 0) %>%
  hc_add_series(gp, "column", hcaes(x = year, y = gdpPercap, 
                                    group = country), yAxis = 1)

enter image description here

Upvotes: 2

Related Questions