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
Related
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.
Maybe it's duplicated with mapbubble does not work with highcharter highmaps but there was no answer to the question...
From the example taken at https://jkunst.com/highcharter/articles/maps.html with a map of type "mappoint", I try to achieve the same with "mapbubble". I created for this a column z
library(httr)
library(jsonlite)
library(geojsonio)
library(highcharter)
library(dplyr)
ausgeojson <- GET("https://raw.githubusercontent.com/johan/world.geo.json/master/countries/AUS.geo.json") %>%
content() %>%
fromJSON(simplifyVector = FALSE) %>%
as.json()
ausmap <- highchart(type = "map") %>%
hc_add_series(mapData = ausgeojson, showInLegend = FALSE)
ausmap
airports <- read.csv("https://raw.githubusercontent.com/ajdapretnar/datasets/master/data/global_airports.csv")
airports <- airports %>%
filter(country == "Australia", name != "Roma Street Railway Station")
airports <- airports %>%
mutate(z=runif(n=nrow(airports),min=1,max=20))
airp_geojson <- geojson_json(airports, lat = "latitude", lon = "longitude")
# works with mappoint
ausmap %>%
hc_add_series(
data = airp_geojson,
type = "mappoint",
dataLabels = list(enabled = FALSE),
name = "Airports",
tooltip = list(pointFormat = "{point.name}")
)
# doesn't work with mapbubble
ausmap %>%
hc_add_series(
data = airp_geojson,
type = "mapbubble",
value = "z",
dataLabels = list(enabled = FALSE),
name = "Airports",
tooltip = list(pointFormat = "{point.name}")
)
The problem is probably due to the fact that {highcharter} cannot access the other columns of the geojson file.
Example if I add another column to the tooltip argument in the mappoint case :
ausmap %>%
hc_add_series(
data = airp_geojson,
type = "mappoint",
dataLabels = list(enabled = FALSE),
name = "Airports",
tooltip = list(pointFormat = "{point.name} : {point.altitude}")
)
I don't see altitude :
I have the following data frame:
library(dplyr)
library(highcharter)
hc <- structure(list(date_ref = c("2020-03-02", "2021-02-02", "2021-03-02"
), bracket_pay = c("T1", "T1", "T1"), col1 = c(10010, 12010, 11090), col2 = c(9850,
11300, 10080), ratio = c(98.40, 94.09, 90.89)), row.names = c(NA, -3L), class = c("tbl_df", "tbl",
"data.frame"))
The data is:
# A tibble: 3 x 5
date_ref bracket_pay col1 col2 ratio
<chr> <chr> <dbl> <dbl> <dbl>
1 2020-03-02 T1 10010. 9850. 98.4
2 2021-02-02 T1 12010. 11300. 94.1
3 2021-03-02 T1 11090. 10080. 90.9
I want to create a combined plot (column and line) using highcharter. This is what I tried:
highchart() %>%
hc_yAxis_multiples(
list(title = list(text = "Amount"),
opposite = FALSE),
list(title = list(text = "Percentage"),
opposite = TRUE)) %>%
hc_add_series(hc, hcaes(x = date_ref, y = col2),
yAxis = 0,
name = "Amount",
type = "column",
color = "mediumseagreen") %>%
hc_add_series(hc, hcaes(x = date_ref, y = ratio),
yAxis = 1,
name = "Advanced",
type = "line",
color = "tomato")
And the plot is almost good, however when you see the labels on the X axis, you see 1,2,3 instead of 2020-03-02,2021-02-02,2021-03-03. By the way date_ref is a character vector. Please, any help will be greatly appreciated.
Adding hc_xAxis(type = 'category') seems to fix it.
highchart() %>%
hc_xAxis(type = 'category') %>%
hc_yAxis_multiples(
list(title = list(text = "Amount"),
opposite = FALSE),
list(title = list(text = "Percentage"),
opposite = TRUE)) %>%
hc_add_series(hc, hcaes(x = date_ref, y = col2),
yAxis = 0,
name = "Amount",
type = "column",
color = "mediumseagreen") %>%
hc_add_series(hc, hcaes(x = date_ref, y = ratio),
yAxis = 1,
name = "Advanced",
type = "line",
color = "tomato")
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}")
Follow up on this post I am not able to get the x-axis labels in correct format using the code below.
hchart(visits, "column", x = as.Date(VisitDate), y = freq, group = Clinic) %>%
hc_xAxis(categories = allDates$VisitDate, title = list(text = 'Deadline'), type = 'datetime', dateTimeLabelFormats = list(month = "%b", year = "%y")) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = TRUE)
)
The resulting chart below has labels all messed up.
Also is it possible to specify interval as in 1 month, 15 days etc.
hchart(visits, "column", hcaes(x = VisitDate, y = freq, group = Clinic)) %>%
hc_xAxis(categories = allDates$VisitDate, title = list(text = 'Deadline'), type = 'datetime', dateTimeLabelFormats = list(month = "%b", year = "%y")) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = TRUE)
)
You need to add hcaes inside the hchart function. There are some nice examples here Highcharter page
Also Highchart prefer dates as timestamp to work the way you want, please check this answer plotband.
I hope this help you. Have a nice day
edit1:
With this code you can add the select range to date you want, however I think to this kind of data and chart it does not apply
hchart(visits, "column", hcaes(x = VisitDate, y = freq, group = Clinic))%>%
hc_xAxis(categories = allDates$VisitDate, title = list(text = 'Deadline'),
type = 'datetime', dateTimeLabelFormats = list(month = "%b", year = "%y"))
%>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = TRUE)
) %>%
hc_rangeSelector(enabled = TRUE, buttons = list(
list(type = "all", text = "All"),
list(type = "month", count = 1, text = "1m"),
list(type = "day", count = 15, text = "15d"),
list(type = "day", count = 1, text = "1d")
), selected = 3)