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")
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.
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 am trying to draw line chart using Highchart . I need data format in Million format . Ex for the First point in screenshot 2423175 should be shown as 2.42 Million .How do i change format = "{point.y}" to show in Millions
highchart() %>%
hc_add_series(data, hcaes(x = data$Month, y = data$Total, color = data$Total), type = "line",dataLabels = list(
enabled = TRUE,
format = "{point.y} " )
) %>%
hc_tooltip(cros[![enter image description here][1]][1]shairs = TRUE, borderWidth = 1.5,headerFormat= "",
pointFormat = paste("Year: <b>{point.x:%b-%y}</b> <br> Population: <b>{point.y}</b>")) %>%
hc_title(text = "Population by year") %>%
hc_subtitle(text = "2016-2020") %>%
hc_xAxis(type = "datetime", title = list(text = "Year")) %>%
hc_yAxis(title = list(text = "count per year")) %>%
hc_legend(enabled = FALSE) %>%
hc_add_theme(custom_theme)
Here is a 2 step way of doing it:
First, you need to format your numbers from looking like 2423175 to 2.42 before you create your plot.
data$Total <- format(round(data$Total / 1e6, 1), trim = TRUE)
Next, in order to add 'Million' after your numbers in Highcharter, change format from format = "{point.y} " to format = paste("{point.y} Million") while creating your plot. Your numbers should now be displayed in the format "X.XX Million".
You can use dataLabels.formatter: https://api.highcharts.com/highcharts/series.line.dataLabels.formatter to format your dataLabels. I know how to do it in JavaScript and inject this code inside JS() function in R:
hc_add_series(data, hcaes(x = data$Month, y = data$Total, color = data$Total), type = "line",dataLabels = list(
enabled = TRUE,
formatter = JS("function() {
return (this.y / 1000000).toFixed(2) + 'M'
}") )
) %>%
JS example: https://jsfiddle.net/BlackLabel/o49zcjLv
Let me know if it worked.
Edit: The whole working code with sample data:
library(highcharter)
data <- data.frame(
y = c(54324232,85325324,10424324,44234324,74324234, 44321413))
highchart() %>%
hc_add_series(data, type = "line", hcaes(y = y), dataLabels = list(
enabled = TRUE,
formatter = JS("function() {
return (this.y / 1000000).toFixed(2) + 'M'
}"
)))
Is there a way to associate dashStyle by group in the highcharter package.
I would like the line with 'forecast' as type to be dashed.
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)) %>%
hc_yAxis(title = list(text = "Units")) %>%
hc_xAxis(title = list(text = "Week"), type = "datetime", tickInterval = 7* 24 * 3600 * 1000) %>%
hc_colors(custom_colours)
Also, is it possible to associate colour with a group, for instance using a mutate with ifelse to create a column with the colours in the dataframe?
You can do:
custom_colours <- c("#4286f4", "#d66048")
custom_dashes <- c("Solid", "ShortDashDotDot")
hchart(sample_df, "line", hcaes(calendar_week, units, group = type),
color = custom_colours, dashStyle = custom_dashes) %>%
hc_yAxis(title = list(text = "Units")) %>%
hc_xAxis(title = list(text = "Week"), type = "datetime", tickInterval = 7* 24 * 3600 * 1000)
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)