library(data.table)
## Warning: package 'data.table' was built under R version 3.3.2
library(highcharter)
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
options(highcharter.theme = hc_theme_smpl())
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)))
mydata
## year car sales polution
## 1: 2001 BMW 6158.228 0.99080690
## 2: 2002 BMW 12785.955 0.55178069
## 3: 2003 BMW 84618.173 0.56290527
## 4: 2004 BMW 115902.191 0.11380538
## 5: 2005 BMW 67539.382 1.45123627
## 6: 2006 BMW 47198.567 0.87147117
## 7: 2007 BMW 118258.742 1.87154049
## 8: 2008 BMW 15827.863 0.38939488
## 9: 2009 BMW 33410.413 0.05559778
## 10: 2010 BMW 25502.225 -0.25264474
## 11: 2011 BMW 35694.680 0.65034222
## 12: 2012 BMW 157798.453 1.09306799
## 13: 2013 BMW 68254.418 3.02610141
## 14: 2014 BMW 24190.833 -0.27710518
## 15: 2015 BMW 23157.703 0.14753835
## 16: 2001 VW 32757.821 4.30869702
## 17: 2002 VW 25415.783 9.31850928
## 18: 2003 VW 37529.149 3.65084517
## 19: 2004 VW 34629.476 6.67487548
## 20: 2005 VW 53381.856 0.53129364
## 21: 2006 VW 59404.546 5.25172855
## 22: 2007 VW 40288.226 1.56683265
## 23: 2008 VW 21946.901 3.59177821
## 24: 2009 VW 54476.231 4.19015729
## 25: 2010 VW 40666.323 8.00908048
## 26: 2011 VW 43402.767 7.75472990
## 27: 2012 VW 50446.127 9.37659942
## 28: 2013 VW 27742.599 4.67959398
## 29: 2014 VW 13328.219 3.83001033
## 30: 2015 VW 55193.637 8.14548844
## 31: 2001 AUDI 60300.270 2.00491193
## 32: 2002 AUDI 61074.980 1.39139487
## 33: 2003 AUDI 58691.066 1.39687113
## 34: 2004 AUDI 52053.324 1.85103860
## 35: 2005 AUDI 57880.533 2.00918699
## 36: 2006 AUDI 64322.809 1.51079210
## 37: 2007 AUDI 74883.300 3.37924938
## 38: 2008 AUDI 48080.410 2.02385340
## 39: 2009 AUDI 65927.438 2.75662874
## 40: 2010 AUDI 60712.875 1.51561582
## 41: 2011 AUDI 39218.491 1.80403711
## 42: 2012 AUDI 75823.136 2.09584983
## 43: 2013 AUDI 42464.486 2.18204937
## 44: 2014 AUDI 85181.116 1.80108177
## 45: 2015 AUDI 45915.028 1.04976032
## year car sales polution
library(tidyr)
## Warning: package 'tidyr' was built under R version 3.3.2
library(dplyr)
## -------------------------------------------------------------------------
## data.table + dplyr code now lives in dtplyr.
## Please library(dtplyr)!
## -------------------------------------------------------------------------
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
##
## between, first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(dtplyr)
## Warning: package 'dtplyr' was built under R version 3.3.2
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 6158.228
## 2 2002 BMW sales 12785.955
## 3 2003 BMW sales 84618.173
## 4 2004 BMW sales 115902.191
## 5 2005 BMW sales 67539.382
## 6 2006 BMW sales 47198.567
## 7 2007 BMW sales 118258.742
## 8 2008 BMW sales 15827.863
## 9 2009 BMW sales 33410.413
## 10 2010 BMW sales 25502.225
## # ... with 80 more rows
myseries <- mydata2 %>%
group_by(car, key) %>%
do(data = list_parse2(data.frame(.$year, .$value))) %>%
ungroup()
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),
color = colorize(name)
)
myseries
## # A tibble: 6 × 9
## car key data name auxvar id linkedTo yAxis color
## <chr> <chr> <list> <chr> <lgl> <chr> <chr> <dbl> <chr>
## 1 AUDI polution <list [15]> AUDI TRUE audi <NA> 1 #440154
## 2 AUDI sales <list [15]> AUDI FALSE <NA> audi 0 #440154
## 3 BMW polution <list [15]> BMW TRUE bmw <NA> 1 #21908C
## 4 BMW sales <list [15]> BMW FALSE <NA> bmw 0 #21908C
## 5 VW polution <list [15]> VW TRUE vw <NA> 1 #FDE725
## 6 VW sales <list [15]> VW FALSE <NA> vw 0 #FDE725
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}%")
))