[英]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. 我正在尝试创建一个图表,其中包含3个组(宝马,奥迪和大众)的2个度量值(销售和污染),历时10年。 So I need to plot 6 lines. 因此,我需要绘制6条线。 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) 这是数据集: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()
: 而且我还可以在一个图中绘制两个度量,但是由于无法将车分组到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
? 有没有办法在highcharter
实现这一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. 然后添加诸如id
, linkedTo
和yAxis
类的属性,以按品牌关联系列并将系列放入正确的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
The data is ready so you can use hc_add_series_list
to add this multiples series. 数据已准备就绪,因此您可以使用hc_add_series_list
添加此倍数系列。 I recommend you use top
and height
parameter to put the series in differents spaces. 我建议您使用top
和height
参数将系列放置在不同的空间中。
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 实时版本: http : //rpubs.com/jbkunst/questions-41654341
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.