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)
Related
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.
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.
I am trying to create a mixed chart with highcharter R, see below the full code. I can't get rid of the following problems:
how to delete the text "Values" on both y-axes? I'd like only my text to show up ("xxx" and "yyy").
why are the x-axis values not showing up (2019, 2020a, 2020b) and instead below the columns "1", 2" and "3" appear? How could I change it?
Any help would be appreciated, thank you!
df2 <- data.frame(supp=rep(c("Media", "Mediana"), each=3),
Anno=rep(c("2019", "2020a", "2020b"),2),
Reddito=c(40100, 39000, 38000, 34000, 33000, 32000))
df3<-data.frame(supp=rep(c("Numero indice media", "Numero indice mediana"), each=3),
Anno=rep(c("2019", "2020a", "2020b"),2),
Reddito=c(100, 97, 96, 100, 96, 95))
highchart() %>%
hc_yAxis_multiples(
list(title = list(text = "xxx"),opposite=F),
list(title = list(text = "yyy"),opposite=TRUE),
list(lineWidth = 0),
list(showLastLabel = F, opposite = T))%>%
hc_add_series(data = df2,type="column" ,hcaes(x = "Anno", y = 'Reddito', group = 'supp')) %>%
hc_add_series(data = df3, type = "spline", hcaes(x = 'Anno', y = 'Reddito', group = 'supp'),yAxis = 1)
To answer your first question, try this:
library(highcharter)
highchart() %>%
hc_yAxis_multiples(
list(title=list(text="xxx",margin = 20),
lineWidth = 3,showLastLabel = FALSE,opposite = FALSE),
list(
title=list(text="yyy", margin = 20),
lineWidth = 3,showLastLabel = FALSE, opposite = T)
) %>%
hc_add_series(data = df2,type="column" ,hcaes(x = "Anno", y = 'Reddito', group = 'supp')) %>%
hc_add_series(data = df3, type = "spline", hcaes(x = 'Anno', y = 'Reddito', group = 'supp'),yAxis = 1)
Values of xAxis aren't displayed in the way you expect, because you didn't define xAxis type, so they appear as index numbers by default. If you specify the type within hc_xAxis to category, it should work fine. Here's the final code:
library(highcharter)
highchart() %>%
hc_yAxis_multiples(
list(title=list(text="xxx",margin = 20),
lineWidth = 3,showLastLabel = FALSE,opposite = FALSE),
list(
title=list(text="yyy", margin = 20),
lineWidth = 3,showLastLabel = FALSE, opposite = T)
) %>%
# Specify Type
hc_xAxis(type = "category") %>%
hc_add_series(data = df2,type="column" ,hcaes(x = "Anno", y = 'Reddito', group = 'supp')) %>%
hc_add_series(data = df3, type = "spline", hcaes(x = 'Anno', y = 'Reddito', group = 'supp'),yAxis = 1)
I noticed that my bar graphs change in order based on alphabetical order. I'm using a selectinput, thus if a person who is selected with a name beginning in A, they are at the top, but if it is a letter after C, then they move to the bottom. This is not based on the value of the bars, but seems tied to the names. How can I keep the ProviderName at top always?
My hc code is below
hchart(
comparison_prov_df,
type = "bar",
hcaes(x = Metric, y = Value, group = ProviderName),
colorByPoint = F,
showInLegend = T,
dataLabels = list(enabled = T)
) %>%
hc_chart(zoomType = "xy") %>%
hc_tooltip(crosshairs = TRUE, shared = FALSE, borderWidth = 1) %>%
hc_credits(
enabled = TRUE,
text = ""
) %>%
hc_add_theme(hc_theme_elementary()) %>%
hc_legend(enabled = TRUE) %>%
hc_exporting(
enabled = TRUE,
filename = "data"
) %>%
hc_title(
text = "Title",
align = "left"
) %>%
hc_yAxis(
title = list(text = "Y Axis"),
labels = list(
reserveSpace = TRUE,
overflow = "justify"
)
) %>%
hc_xAxis(title = "") %>%
hc_tooltip(pointFormat = "{point.y:.1f}")
I am not sure what the original data is like, but I'll provide a simple example of one way to change the order of items on an axis. You can change the order by simply using hc_xAxis and then listing the category order.
library(highcharter)
library(tidyverse)
df %>%
hchart("bar", hcaes(x = category, y = value, group = group)) %>%
hc_xAxis(
categories = list(
"Pineapples",
"Strawberries",
"Apples",
"Plums",
"Blueberries",
"Oranges"
)
)
Output
Data
set.seed(326)
df <-
data.frame(category = rep(c(
"Apples", "Oranges", "Plums", "Pineapples", "Strawberries", "Blueberries"
), each = 2),
value = round(runif(12, min = 0, max = 100)),
group = rep(c(2020, 2021), 6))
Looking at the documentation, tickmarkPlacement, seems to allow the moving of ticks from 'on' to 'between' but with the hchart function I can't seem to get it to work.
Trying to change from this style to this.
library(highcharter)
# Create sample data frame
sample_df <- data_frame(sku = c(rep("12",40)),
type = c(rep("actuals",20), rep("forecast",20)),
calendar_week = rep(seq(as.Date("2017-09-08"), as.Date("2018-01-23"), by=7),2),
units = round(c(rnorm(11, mean=50, sd=10), rep(0, 9), c(rnorm(20, mean=100, sd=10))),0))
# Create colours vector
custom_colours <- c("#4286f4", "#d66048")
# Chart
hchart(sample_df, "line", hcaes(calendar_week, units, group = type), color = custom_colours) %>%
hc_yAxis(title = list(text = "Units")) %>%
hc_xAxis(title = list(text = "Week"), startOfWeek = 5, type = "datetime", tickInterval = 7* 24 * 3600 * 1000, tickmarkPlacement = "between")
tickmarkPlacement works for categorized axes only. Try this:
hchart(sample_df, "line", hcaes(factor(format(sample_df$calendar_week, "%d-%b-%Y")),
units, group = type), color = custom_colours) %>%
hc_yAxis(title = list(text = "Units")) %>%
hc_xAxis(title = list(text = "Week"), tickmarkPlacement = "between")