Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hc_add_series - Plot x and y as datetime #787

Open
ochee opened this issue Dec 19, 2022 · 3 comments
Open

hc_add_series - Plot x and y as datetime #787

ochee opened this issue Dec 19, 2022 · 3 comments

Comments

@ochee
Copy link

ochee commented Dec 19, 2022

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
Copy link
Author

ochee commented Dec 19, 2022

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

image

@ochee
Copy link
Author

ochee commented Dec 19, 2022

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 !

Copy link

stale bot commented Mar 17, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant