0

I've trying to make a simple line graph using highchart but for some reason, the dates are all in different order.

This is the code I'm using to create the graph:

    hchart(dataframe,"line",hcaes(
      x=MONTH,
      y=VALUE,
      group=NAME
    ))%>%
      hc_yAxis(TITLE="VALUE")%>%
      hc_xAxis(title="")

  })

and this is the output I get:

img

as you can see, this is completely wrong. I´ve tried to order by the month on the dataframe before creating the graph, as well as adding X=sort(MONTH) but nothing seems to work. My dataframe looks like this:

|| Name || Month || Value

So I group by Name and Month, and do a simple sum of the total value. The Month value on the dataframe is of type char and comes from an sql query TO_CHAR(DATE,'YYYY-MM') I´ve tried doing it on dplyr but the output is still the same.

Thanks for the help!

Edit:

Here is a reproductible example for you to try out:

library(highcharter)
new_data <- data.frame(names = c("name1","name2","name3","name4","name5","name6","name7","name8","name9"
           ,"name2","name3","name4","name5","name6","name7","name8","name9"
           ,"name2","name3","name4","name5","name6","name7","name8","name9"
           ,"name2","name3","name4","name5","name6","name7","name8","name9"),
months = c("2019-01","2019-06","2019-07","2019-08","2019-01","2019-02","2019-03","2019-04","2019-05"
            ,"2019-06","2019-07","2019-08","2019-01","2019-02","2019-03","2019-04","2019-05"
            ,"2019-06","2019-07","2019-08","2019-01","2019-02","2019-03","2019-04","2019-05"
            ,"2019-06","2019-07","2019-08","2019-01","2019-02","2019-03","2019-04","2019-05"),
values = c(150,152,1506,1245,5214,2312,2435,241,421
            ,152,1506,1245,5214,2312,2435,241,421
            ,152,1506,1245,5214,2312,2435,241,421
            ,152,1506,1245,5214,2312,2435,241,421))

hchart(new_data,"line",hcaes(
  #x=sort(MONTH),
  x=months,
  y=values,
  group=names
))%>%
  hc_yAxis(TITLE="values")%>%
  hc_xAxis(title="")
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
MFTS
  • 35
  • 1
  • 2
  • 8
  • Could you please give us a reproductible example with your dataset. Thx in adavance. – Rémi Coulaud Sep 19 '19 at 16:42
  • @RémiCoulaud just added one :) – MFTS Sep 19 '19 at 16:53
  • @camille I'm rendering the chart on shiny. CRAN tag was a mistake though – MFTS Sep 19 '19 at 16:57
  • 1
    The question itself isn't actually about Shiny. You've got `months` as a factor, since that's the default of `data.frame`—you probably want a date object, you should be able to find a lot of helpful SO posts on conversion to date – camille Sep 19 '19 at 17:02
  • @camille since I want to group by month, a date object doesn't help my case. Only way might be using last month as the date and group by it, but that gives you a wrong value for a date that isn't end of month. If you can help me with an alternative I will really appreaciate it. – MFTS Sep 19 '19 at 17:23
  • If you want to treat it as a date, you're probably going to need to coerce it to a date object. It's fine if you have it grouped by month; [here](https://stackoverflow.com/q/6242955/5325862)'s a post with several options on that, including adding a dummy day to the year/month information – camille Sep 19 '19 at 17:31
  • @camille you mean having, for example, `x=format(date,'%Y-%m')` in my graph function? In that case I can't graph the desire values since the value column won't be calculated – MFTS Sep 19 '19 at 17:39
  • Not sure what you mean since your calculations aren't here. Also the amount of data here isn't enough to get lines like in your example, so if there's something else going on to solve the issue, I can't see it – camille Sep 19 '19 at 17:55
  • There is something wrong with your data. The simplest way is to define xAxis.categories like hc_xAxis(max = 7, categories = list("2019-01", "2019-02", "2019-03", "2019-04", "2019-05", "2019-06", "2019-07", "2019-08")) and add separate series with values only (or values and x as names). API Reference: https://api.highcharts.com/highcharts/xAxis.categories and https://api.highcharts.com/highcharts/series.line.data – raf18seb Sep 24 '19 at 07:19

0 Answers0