HYljy
HYljy

Reputation: 15

how to make multiple highchart graph in R?

I'm trying to graph multiple dataframe columns in R. (like this-> Graphing multiple variables in R)

      bid   ask       date
1   20.12 20.14 2014-10-31
2   20.09 20.12 2014-11-03
3   20.03 20.06 2014-11-04
4   19.86 19.89 2014-11-05

This is my data.

And I can make one line graph like this.

`data%>% select(bid,ask,date) %>% hchart(type='line', hcaes(x='date', y='bid'))`

I want to add ask line graph in this graph.

Upvotes: 0

Views: 1824

Answers (2)

Edward
Edward

Reputation: 19394

One way is to reshape (gather) the values to plot and then add a group aesthetic to the hchart function:

library(tidyr)
data %>% select(bid,ask,date) %>% 
  gather("key", "value", bid, ask) %>%
  hchart(type='line', hcaes(x='date', y='value', group='key'))

ps. Don't forget to load all the necessary libraries

Upvotes: 2

UseR10085
UseR10085

Reputation: 8198

You can use the following code

library(reshape2)
library(highcharter)
df_m <- melt(df, id="date")
hchart(df_m, "line", hcaes(x = date, y = value, group = variable))

enter image description here

Here is the data

df = structure(list(bid = c(20.12, 20.09, 20.03, 19.86), ask = c(20.14, 
20.12, 20.06, 19.89), date = structure(c(4L, 1L, 2L, 3L), .Label = c("03/11/2014", 
"04/11/2014", "05/11/2014", "31/10/2014"), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L))

Upvotes: 0

Related Questions