I have the following dataset:
xdata <- seq(as.Date("2020-01-01"),as.Date("2020-12-31"), "days")
ydata <- c(1:366)
datamipo <- data.frame(xdata,ydata)
And I want to plot the data by month and plot using highcharter:
datamipo %>%
mutate(month = format(xdata,"%b")) %>%
group_by(month) %>%
summarise(total = sum(ydata)) %>%
hchart(type = "line",
hcaes(x=month, y=total))
But the x axis doesn't recognize the data as dates and put them in alphabetical order. Please, do you know how can I group the date to show the totals by month? Thank you.
I would think about saving this data differently and operate on the actual values. Here you can see an example of such a setting:
df <- data_frame(
time = seq(as.Date("2020-01-01"), as.Date("2020-02-01"), by = 1),
value = sample(1000:2000, size = 32),
dt = datetime_to_timestamp(time)
)
hchart(df, "line", hcaes(dt, value)) %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%d dayview', week = '%d weekview'))
hchart(df, "line", hcaes(dt, value)) %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%d of %b', week = '%d of %b'))
Related
I would like to draw plotly graphs with rangeslider (including start/end range), yet printing quarter dates on the X-axis, instead of the usual ymd format, as shown in the example below ; i.e. printing "Q1 2019" instead of "Jan 2019" :
library(plotly)
library(zoo)
d <- tibble::tibble(
time = as.yearqtr(seq(as.Date("2016-01-01"), as.Date("2020-08-31"), by = "quarters")),
y = rnorm(seq_along(time))
)
d$time <- as.Date(d$time, format = "%Y-%m-%d")
plot_ly(d, x = ~time, y = ~y) %>%
add_lines() %>%
rangeslider(d$time[10], d$time[19])
You can do this with layout using ticktext and tickvals. This is the code that will ensure you see years and quarters on the X-axis.
plot_ly(d, x = ~time, y = ~y) %>%
add_lines() %>%
rangeslider(d$time[10], d$time[19]) %>%
layout(xaxis = list(ticktext = paste0(c(rep(2018, 3),
rep(2019, 4),
rep(2020, 2)),
" Q",
c(2:4, 1)),
tickvals = d$time[10:18]
))
Using set.seed() makes the code repeatable, (since you used rnorm). I used set.seed(3958) if you wanted to see the same output.
Another way that you can customize the text is through the zoom level. Plotly references this site for the formatting here. There are a few examples on Plotly's website, as well. Check it out.
plot_ly(d, x = ~time, y = ~y) %>%
add_lines() %>%
rangeslider(d$time[10], d$time[19]) %>%
layout(xaxis = list(
tickformatstops = list(
list(dtickrange = list(NULL, "M1"),
value = "%Y %b"),
list(dtickrange = list("M1", "M6"),
value = "%Y Q%q"),
list(dtickrange = list("M6", NULL),
value = "%Y Y")
)))
I'm having some trouble getting the correct format for my x-axis in the drill-down chart. I'm hoping to have the same x-axis formatting as the 'drilled-up' chart (e.g. "mmm-yy"). Below is a minimal example.
At the moment, it looks like there is no data being used for the drilled-down x-axis.
Any help would be greatly appreciated.
library(highcharter); library(dplyr)
myData <- tibble(name = c("Cat", "Cat", "Dog", "Dog"),
value = c(2,3,2,5),
date = as.Date(c("2021-03-01", "2021-06-01","2021-03-01", "2021-06-01")),
drilldown = tolower(name) )
hc <- hchart(myData, "column", hcaes( y = value, group=name, x = date)) %>%
hc_plotOptions(series = list(stacking = "normal"))
myDataDrilldown <- tibble(date = as.Date(c("2021-03-01", "2021-06-01")),
value = c(4, 3)) %>%
list_parse2()
hc %>% hc_drilldown(
allowDrilldown = TRUE,
series = list(
list(id = "cat",
data = myDataDrilldown)
)
)
Thanks!
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 linechart with rbokeh where the x-axis is supposed to show a datetime variable.
Consider the following example:
data <- tibble(
time = as.POSIXct(c("2019-08-27 08:15:00",
"2019-08-27 10:30:00",
"2019-08-27 12:45:00")),
value = c(0.3, 0.6, 0.2)
)
figure(data = data) %>%
ly_lines(x = time,
y = value) %>%
ly_points(x = time,
y = value,
hover = value) %>%
x_axis(label = "Date"
# number_formatter = "numeral",
# format = list(hours = "%d %B %Y")
) %>%
y_axis(label = "Values",
number_formatter = "numeral",
format = list(percent = "0 %")) %>%
y_range(dat = c(0, 1))
This produces the following plot:
Not only does this show the wrong values on the x-axis, they are also formatted in a very inconvenient way.
I have tried changing the format to a more appropriate way by using the format argument (that has been commented out in my example), but this only causes the plot to not even be created anymore.
What is going on here?
Your code worked for me with the format argument. If you want the time included, I would use this:
figure(data = data) %>%
ly_lines(x = time,
y = value) %>%
ly_points(x = time,
y = value,
hover = value) %>%
x_axis(label = "Date",
number_formatter = "numeral",
format = list(hours = "%Y-%m-%d %H:%M:%S")
) %>%
y_axis(label = "Values",
number_formatter = "numeral",
format = list(percent = "0 %")) %>%
y_range(dat = c(0, 1)) %>%
theme_axis("x", major_label_orientation = 45)
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}")