在r中有2组的高图图

时间:2017-01-14 19:55:10

标签: r highcharts shiny

我试图为3组(宝马,奥迪和大众)创建一个包含2项措施(销售和污染)的图表,为期10年。所以我需要绘制6条线。我想每辆车的线路是相同的,但污染应该是破灭的。这是数据集: 库(data.table) 库(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)))

只需1个小节就可以轻松绘制组图:

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

而且我也可以在一个情节中绘制两个测量值,但是由于组不在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')

有没有办法在highcharter中实现这一目标? 感谢。

1 个答案:

答案 0 :(得分:2)

一种方法是收集您的数据,以获得长格式表。

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

然后分组车,通过该组合获得一个系列的关键。

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

然后添加idlinkedToyAxis等属性,按品牌关联系列,并将系列放入正确的y轴。

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

数据准备就绪,因此您可以使用hc_add_series_list添加此倍数系列。我建议您使用topheight参数将系列放入不同的空格。

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

实时版:http://rpubs.com/jbkunst/questions-41654341