jbkunst / highcharter

R wrapper for highcharts
http://jkunst.com/highcharter/
Other
717 stars 147 forks source link

hc_add_series - Plot x and y as datetime #787

Open ochee opened 1 year ago

ochee commented 1 year ago

Hello,

I tried to plot a highcharter line serie with both x and y values as date, see my sample data below :

library(highcharter)
datasample <- data.frame(month = c("2020-01-01","2020-02-01","2020-03-01"),
                         sales = c("2019-05-08","2022-02-10","2024-12-01"),
                         sales2 = c(10,12,6))

datasample$month <- as.Date(datasample$month)
datasample$sales <- as.Date(datasample$sales)

highchart() %>% 
hc_add_series(datasample, hcaes(x = month, y = sales), name = "Sales") %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%d of %b')) %>% 
hc_yAxis(type = "datetime", dateTimeLabelFormats = list(day = '%d of %b')) 

The render plot is blank, nothing is dipslayed. Is it possible in highcharter package to do this kind of plot ? many thanks, Ochees

ochee commented 1 year ago

with hcaes(..., y = sales2) highcharter renders a dynamic plot with wrong y-axis (as interpreted as a "datimetype" type), see below :

image

ochee commented 1 year ago

by using a datetime_to_timestamp function first

datasample <- data.frame(month = c("2020-01-01","2020-02-01","2020-03-01"),
                         sales = datetime_to_timestamp(as.Date(c("2019-05-08","2022-02-10","2024-12-01"))),
                         sales2 = c(10,12,6))

and hc_tooltip for formatting :

highchart() %>% 
hc_add_series(datasample, hcaes(x =month , y = sales), 
 type = "area", name = "Sales") %>%
#hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%d of %b')) %>% 
hc_xAxis(type = "datetime") %>%  
hc_yAxis(type = "datetime") %>%  
hc_tooltip(crosshairs = TRUE, pointFormat = "y: {point.y:%Y-%m-%d}")

it works !

stale[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Feel free to reopen it if you find it necessary.