简体   繁体   中英

highcharter graph with 2 groups in r

I'm trying to create a graph with 2 measures(Sales and Pollution) for 3 groups (BMW, AUDI and VW) for 10 years. So I need to plot 6 lines. I would like the lines for each car be the same but the pollution should be dashed. This is the dataset: library(data.table) library(highcharter)

mydata<-data.table(year=rep(2001:2015,3),car=c(rep('BMW',15),rep('VW',15),rep('AUDI',15)),
                   sales=c(50000*rnorm(n = 15,mean = 1,sd = .8),30000*rnorm(n = 15,mean = 1,sd = .6),60000*rnorm(n = 15,mean = 1,sd = .2)),
                   polution=c(1*rnorm(n = 15,mean = 1,sd = .8),5*rnorm(n = 15,mean = 1,sd = .6),2*rnorm(n = 15,mean = 1,sd = .2)))

It's easy to plot the group chart for only 1 measure:

hchart(mydata,'line',x=year,y=sales,group=car)

And I'm also able to plot both measures in one plot, but then I'm not able to group the car as group is not in hc_add_series() :

highchart() %>%
  hc_xAxis(title=list('Exercise'),categories=mydata$year) %>%
  hc_yAxis_multiples(list(
    title=list(text="MM $"),
    align= "right",
    showFirstLabel=FALSE,
    showLastLabel=FALSE,
    opposite=FALSE
  ),
  list(
    title=list(text="Polution"),
    align= "left",
    showFirstLabel=FALSE,
    showLastLabel=FALSE,
    opposite=T,
    labels = list(format = "{value}%")
  )) %>%
  hc_add_series(name="Sales",type='line',
                data=mydata$sales,yAxis=0) %>%
  hc_add_series(name="polution",type='line',
                data=mydata$polution,yAxis=1,dashStyle='longdash')

Is there a way to achieve this in highcharter ? Thanks.

One approach is gather your data, to have a long format table.

library(tidyr)
library(dplyr)

mydata2 <- gather(mydata, key, value, -year, -car)
mydata2 <- tbl_df(mydata2)
mydata2

# A tibble: 90 × 4
    year   car   key      value
   <int> <chr> <chr>      <dbl>
1   2001   BMW sales   3032.183
2   2002   BMW sales  11842.757
3   2003   BMW sales 110296.092
4   2004   BMW sales  46892.036
5   2005   BMW sales 100139.421
6   2006   BMW sales  54703.331
7   2007   BMW sales  -2340.154
8   2008   BMW sales  54917.231
9   2009   BMW sales  53450.376
10  2010   BMW sales  57969.693
# ... with 80 more rows

Then group car, key to get one series by that combination.

myseries <- mydata2 %>%
  group_by(car, key) %>% 
  do(data = list_parse2(data.frame(.$year, .$value))) %>% 
  ungroup()

And then add attributes like id , linkedTo and yAxis , to associate the series by brand and put the series in the correct y Axis.

myseries <- mutate(
  myseries,
  name = car,
  auxvar = rep(c(TRUE, FALSE), 3), # auxiliar var
  id = ifelse(auxvar, tolower(name), NA),
  linkedTo = ifelse(!auxvar, tolower(name), NA),
  yAxis = ifelse(!auxvar, 0, 1)
  )

myseries

# A tibble: 6 × 8
    car      key        data  name auxvar    id linkedTo yAxis
  <chr>    <chr>      <list> <chr>  <lgl> <chr>    <chr> <dbl>
1  AUDI polution <list [15]>  AUDI   TRUE  audi     <NA>     0
2  AUDI    sales <list [15]>  AUDI  FALSE  <NA>     audi     1
3   BMW polution <list [15]>   BMW   TRUE   bmw     <NA>     0
4   BMW    sales <list [15]>   BMW  FALSE  <NA>      bmw     1
5    VW polution <list [15]>    VW   TRUE    vw     <NA>     0
6    VW    sales <list [15]>    VW  FALSE  <NA>       vw     1

The data is ready so you can use hc_add_series_list to add this multiples series. I recommend you use top and height parameter to put the series in differents spaces.

highchart() %>% 
  hc_add_series_list(myseries) %>% 
  hc_yAxis_multiples(list(
    title=list(text="MM $"),
    align= "right",
    top = "0%",
    height = "60%",
    showFirstLabel=FALSE,
    showLastLabel=FALSE,
    opposite=FALSE
  ),
  list(
    title=list(text="Polution"),
    align= "left",
    top = "61%",
    height = "39%",
    showFirstLabel=FALSE,
    showLastLabel=FALSE,
    opposite=T,
    labels = list(format = "{value}%")
    ))

Result:

结果

Live version: http://rpubs.com/jbkunst/questions-41654341

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM