Formatting datetime in Highcharter tooltip - r

Looking to change the format of the datetime in the tooltip. I successfully did it for the x-axis but have not for the tooltip. I've read the docs and cannot find another R-specific topic on this.
Dates are in the required Highchart timestamp from the datetime_to_timestamp function.
library(highcharter)
library(tidyverse)
df <- data.frame(dateTime = c(1557705900000,1557705960000,1557706020000,1557706860000,1557706920000),
points = c(5,7,3,2,9))
highchart() %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%H:%M')) %>%
hc_add_series(df, type = "scatter",
hcaes(x = dateTime, y = points)) %>%
hc_tooltip(crosshairs = TRUE, dateTimeLabelFormats = list(day = '%H:%M'))
# highchart() %>%
# hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%H:%M')) %>%
# hc_add_series(df, type = "scatter",
# hcaes(x = dateTime, y = points)) %>%
# hc_tooltip(crosshairs = TRUE, dateTimeLabelFormats = '%H:%M')
The tooltip format should look like the x-axis format.
Any thoughts?

Try pointFormat.
highchart() %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%H:%M')) %>%
hc_add_series(df, type = "scatter",
hcaes(x = dateTime, y = points)) %>%
hc_tooltip(crosshairs = TRUE, pointFormat = "x: {point.x:%H:%M} <br> y: {point.y}")

Related

Change xAxis format in highcharts using R

I´m trying to change the xAxis format. I have two data tables with values per month.
These are my tables:
data <- structure(list(DATA = c("2022/09", "2022/10", "2022/11"), AUM = c(31057073.67,
32045391.81, 32690375.86)), row.names = c(NA, -3L), class = c("data.table",
"data.frame"))
data2 <- structure(list(DATA = c("2022/09", "2022/10", "2022/11"), CUM_SUM = c(1047515038.76,
978350213.95, 879488195.72)), row.names = c(NA, -3L), class = c("data.table",
"data.frame"))
This is my graph:
highchart() %>%
hc_add_series(name = "AUM", id = "aum_line", data = data, hcaes(x = DATA, y = AUM), type = 'line') %>%
hc_add_series(name = 'PL_CUM', id = 'pl_cum_line', data = data2, hcaes(x = DATA, y = CUM_SUM), type = 'line') %>%
hc_plotOptions(column = list(dataLabels = list(enabled = F),
enableMouseTracking = T)) %>%
hc_chart(zoomType = 'xy') %>%
hc_exporting(enabled = TRUE)
My xAxis should be year and month.
I think this is what you're looking for? Your date data is still a string so I converted to date before adding an additional argument to handle the formatting
data$DATA <- as.Date(paste(data$DATA, "/01", sep=""))
data2$DATA <- as.Date(paste(data2$DATA, "/01", sep=""))
Gives:
> data$DATA
[1] "2022-09-01" "2022-10-01" "2022-11-01"
> data2$DATA
[1] "2022-09-01" "2022-10-01" "2022-11-01"
Then adding hc_xAxis(dateTimeLabelFormats = list(day = '%m %Y'), type = "datetime")
highchart() %>%
hc_add_series(
name = "AUM",
id = "aum_line",
data = data,
hcaes(x = DATA, y = AUM),
type = 'line'
) %>%
hc_add_series(
name = 'PL_CUM',
id = 'pl_cum_line',
data = data2,
hcaes(x = DATA, y = CUM_SUM),
type = 'line'
) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = F),
enableMouseTracking = T
)) %>%
hc_chart(zoomType = 'xy') %>%
hc_exporting(enabled = TRUE) %>%
hc_xAxis(dateTimeLabelFormats = list(day = '%m %Y'), type = "datetime")
Plot:
If you want to add your dates as a string, you should pass it as data.names and change xAxis.type to category.
Demo:
https://jsfiddle.net/BlackLabel/x8fm4njt/
In the case of dates, the more relevant xAxis type is datetime. In that case, x should be given as a timestamp.
Demo:
https://jsfiddle.net/BlackLabel/bts82m6u/
API Reference:
https://api.highcharts.com/highcharts/series.line.data
https://api.highcharts.com/highcharts/xAxis.type

Highcharter format number in tooltip

I'm using Highcharts in R, and I want to format the number in the tooltip as a currency without decimal spaces. Right now, numbers appear like this: 34 537 987.21. I want that number to look like this: $34,537,987. Or, better yet, like this: $34M.
Here's a sample of my code:
highchart() %>%
hc_add_series(df,
type = 'spline',
hcaes(x = year, y = data)
) %>%
hc_add_series(df,
type = 'spline',
hcaes(x = year, y = other_data)
) %>%
hc_plotOptions(series = list(marker = list(enabled = TRUE,
hover = TRUE,
symbol = 'circle'))
) %>%
hc_tooltip(
shared = TRUE,
crosshairs = TRUE
)
Use the tooltip parameter inside hc_ad_series() and define as shown below.
highchart() %>%
hc_add_series(df,
type = 'spline',
hcaes(x = year, y = data),
tooltip = list(pointFormat = "$ {point.data}")
) %>%
hc_add_series(df,
type = 'spline',
hcaes(x = year, y = other_data),
tooltip = list(pointFormat = "$ {point.other_data}")
) %>%
hc_plotOptions(series = list(marker = list(enabled = TRUE,
hover = TRUE,
symbol = 'circle'))
)
Hope it helps.

Highcharts in R: sort xAxis in Line Chart

I have a simple line chart, but some categories do not have all periods (xAxis), so the xAxis is not ordered at the end.
That is an example data:
The chart looks like that (with "2019-01" and "2019-03" exchanged)
Example Code
df <- data.frame(PERIODO = c("2017-01","2017-03","2018-01","2018-03",
"2018-03","2019-01",rep("2019-03",2),
"2020-01"),
CATEGORIA = c(rep("A",4),rep("B",2),"A","B","B"),
FRECUENCIA = c(2,3,3,1,2,4,1,1,2))
highchart() %>%
hc_xAxis(type = "category") %>%
hc_add_series(df, "line",
hcaes(x = PERIODO, y = FRECUENCIA,
group = CATEGORIA),
dataLabels = list(enabled = TRUE,
style = list(fontSize = '13px'))
) %>%
hc_legend(enabled = TRUE, align = "right",layout = 'vertical',verticalAlign= "middle") %>%
hc_tooltip(shared = TRUE, crosshairs = TRUE
,style = list(fontSize = "18px")
Someone knows about how to keep the xAxis order by PERIODO: 2017-01,2017-03,2018-01,2018-03,2019-01,2019-03,2020-01
Perhaps you can put your dates PERIODO in Date format (use first day of the month if you only have months).
df$PERIODO <- as.Date(paste0(df$PERIODO, "-01"))
Then, use type = "datetime" instead of category for your hc_xAxis argument. You can indicate what you want as labels on your x-axis using dateTimeLabelFormats.
highchart() %>%
hc_xAxis(type = "datetime",
dateTimeLabelFormats = list(month = '%b %Y')) %>%
...
Default labels and more information can be found here.

Plotly subplots with shared legend in R

I have made twoplots using plotly, which are working fine individually, but when combined using subplot I can't seem to figure out how to combine the legends. I have tried to use showlegend = F in plot_ly in one of the plots, but this just removes it completely - what I want is to control both subplots with the same legend.
My code is as follows:
coronavirus_not_china <- coronavirus %>%
filter(!(country == "China"))
cases_not_china_plot <- coronavirus_not_china %>%
group_by(type, date) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type, values_from = total_cases) %>%
arrange(date) %>%
mutate(active = confirmed - death - recovered) %>%
mutate(active_total = cumsum(active),
recovered_total = cumsum(recovered),
death_total = cumsum(death)) %>%
plot_ly(x = ~ date,
y = ~ active_total,
name = 'Active',
fillcolor = '#1f77b4',
type = 'scatter',
mode = 'none',
stackgroup = 'one',
showlegend = F) %>%
add_trace(y = ~ death_total,
name = "Death",
fillcolor = '#E41317') %>%
add_trace(y = ~recovered_total,
name = 'Recovered',
fillcolor = 'forestgreen') %>%
layout(title = "Distribution of Covid19 Cases outside China",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of Cases", showgrid = T))
coronavirus_china <- coronavirus %>%
filter((country == "China"))
cases_china_plot <- coronavirus_china %>%
group_by(type, date) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type, values_from = total_cases) %>%
arrange(date) %>%
mutate(active = confirmed - death - recovered) %>%
mutate(active_total = cumsum(active),
recovered_total = cumsum(recovered),
death_total = cumsum(death)) %>%
plot_ly(x = ~ date,
y = ~ active_total,
name = 'Active',
fillcolor = '#1f77b4',
type = 'scatter',
mode = 'none',
stackgroup = 'one',
showlegend = T) %>%
add_trace(y = ~ death_total,
name = "Death",
fillcolor = '#E41317') %>%
add_trace(y = ~recovered_total,
name = 'Recovered',
fillcolor = 'forestgreen') %>%
layout(title = "Distribution of Covid19 Cases inside China",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of Cases", showgrid = F))
And I create the subplots as:
subplot(cases_not_china_plot, cases_china_plot, nrows = 2, margin = 0.05, shareX = T) %>%
layout(title="Coronavirus cases outside China and in China", ylab("Number of cases"))
I am quite new to R, so if there is a smarter way to do what I desire, please let me know.
With the above code, my output is:

Bar with negative stack and positive labels

I'm trying to make in R (Shiny) a reversed stacked bar highchart. I already found how to make the graph, but I can't find out how to make the labels on the x-axis positive, like here: https://www.highcharts.com/demo/bar-negative-stack
I've tried to apply the abs() function, but it didn't work so far. Does anyone have a solution?
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Example") %>%
hc_yAxis(title = list(text = ""), labels = list(format = "{value}")) %>%
hc_plotOptions(series=list(stacking='normal'),
column = list( dataLabels = list(enabled = FALSE),
enableMouseTracking = TRUE)) %>%
hc_legend(enabled = FALSE) %>%
hc_xAxis(reversed=FALSE, opposite=TRUE, reversed=FALSE) %>%
hc_add_series(name="neutral", id='neutral', color=c("#766A62"), data=list(2, 8)) %>%
hc_add_series(name="Neutral",linkedTo='neutral',color=c("#ffeeff"),data=list(-5, -3))
I want the values of the bars and the labels on the x-axis all to be positive. Any ideas welcome.
Using your code:
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Example") %>%
hc_yAxis(title = list(text = ""),labels = list(format = "{value}")) %>%
hc_plotOptions(series=list(stacking='normal'),column = list( dataLabels = list(enabled = FALSE),
enableMouseTracking = TRUE)) %>%
hc_legend(enabled = FALSE) %>%
hc_xAxis(list(categories = c("0-4", "5-9"),
reversed=FALSE ),
list(reversed=FALSE,opposite=TRUE,
reversed=FALSE,
categories =c("0-4", "5-9"),
linkedTo = 0)) %>%
hc_yAxis(
labels = list(
formatter = JS("function(){ return Math.abs(this.value) + '%'; }"))) %>%
hc_add_series(name="neutral",id='neutral',color=c("#766A62"),data=list(2, 8)) %>%
hc_add_series(name="Neutral",linkedTo='neutral',color=c("#ffeeff"),data=list(-5, -3))
Following lines are modified:
hc_xAxis(list(categories = c("0-4", "5-9"),
reversed=FALSE ),
list(reversed=FALSE,opposite=TRUE,
reversed=FALSE,
categories =c("0-4", "5-9"),
linkedTo = 0)) %>%
hc_yAxis(
labels = list(
formatter = JS("function(){ return Math.abs(this.value) + '%'; }")))
Result:

Resources