Why there are no intended labels on this highcharter plot - R - r

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")

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

How to Change Position of RangeSlider in R Plotly?

I would like to somehow move the range slider toggle down further below the plot image of plot + range slider here, had to only show a portion of the image due to confidentiality. Is it possible to do this? I will paste my code for the plot below:
output$p <- renderPlotly({
plot1 <- plot_ly(x = x(), y = y(), name = as.character(input$select), type = 'scatter', mode = 'markers+lines', color = I("#696969")) %>%
add_lines(y = meaan(), name = paste("Mean =", meaan()[1]), color = I("black")) %>%
add_lines(y = ucl(), name = paste("UCL =", ucl()[1]), color = I("green")) %>%
add_lines(y = lcl(), name = paste("LCL =", lcl()[1]), color = I("green")) %>%
add_lines(y = usl(), name = paste("USL =", usl()[1]), color = I("red")) %>%
add_lines(y = lsl(), name = paste("LSL =", lsl()[1]), color = I("red")) %>%
add_trace(x=r5x(),y=r5y(),mode='markers', color = I("blue"), name = paste("R 5 =", length(r5())))%>%
add_trace(x=r4x(),y=r4y(),mode='markers', color = I("yellow"), name = paste("R 4 =", length(r4()))) %>%
add_trace(x=r1x(),y=r1y(),mode='markers', color = I("red"), name = paste("R 1 =", length(r1()))) %>%
layout(
title = paste("Test", input$select),
yaxis = list( showline = TRUE,
mirror = "ticks",
linecolor = toRGB("grey"),
linewidth = 1.85
),
xaxis = list( showline = TRUE,
mirror = "ticks",
linecolor = toRGB("grey"),
linewidth = 1.85,
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all"))),
rangeslider = list(type = "date", thickness=0.2)))
})

Highcharter-R: x-axis values not showing up, wrong text on y-axis

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)

highcharter hc_axis not working correctly

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)

Second Y-Axis in a R plotly graph

I combined 2 charts and I am trying to add the second y-axis, but every time I add the yaxis = "y2" to my code, I lose have the my bar graphs.
> MediaDate Spend Search_Visits Other_Visits MediaDate2
> 2016-04-01 $39654.36 19970 2899 Apr 2016
> 2016-05-01 $34446.28 14460 2658 May 2016
> 2016-06-01 $27402.36 12419 2608 Jun 2016
my original code is:
p <- plot_ly(x= w$MediaDate2,y=w$Search_Visits,name = "Paid Search",
type = "bar")
p2 <- add_trace(p, x=w$MediaDate2, y=w$Other_Visits,name = "Other Traffic",
type = "bar")
spend_visits <- layout(p2, barmode = "stack")
spendvisits2 <- spend_visits %>% add_trace(data=w, x=MediaDate2, y=round(Spend,0), fill="tonexty", mode="lines",
text=w$MediaDate2, hoverinfo='name+y+text', name="Spend")
When I add the yaxis= "y2", only the area chart remains:
`spendvisits2 <- spend_visits %>% add_trace(data=w, x=MediaDate2, y=round(Spend,0), yxis="y2" fill="tonexty", mode="lines",
text=w$MediaDate2, hoverinfo='name+y+text', name="Spend")`
Any suggestions would be immensely helpful. Thank you
See this quick Plotly tutorial for multiple axes. You need to specify the attributes for the second y axis in layout().
df <- data.frame(MediaDate = as.Date(c("2016-04-01","2016-05-01","2016-06-01"), format = "%Y-%m-%d"),
Spend = c(39654, 34446, 27402),
Visits = c(19970, 14450, 12419))
plot_ly(df, x = ~MediaDate, y = ~Spend, type = "bar", name = "Spend") %>%
add_trace(x = ~MediaDate, y = ~Visits, mode = "lines", yaxis = "y2", name = "Visits") %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))
Try this
plot_ly(df) %>%
add_trace(x = ~MediaDate, y = ~Spend,type = "bar", name = "Spend") %>%
add_trace(x = ~MediaDate, y = ~Visits, mode = "lines", yaxis = "y2", name = "Visits") %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))
Adding type='scatter' to what Vance Lopez commented worked for me:
plot_ly(df, x = ~MediaDate, y = ~Spend, type = "bar", name = "Spend") %>%
add_trace(x = ~MediaDate, y = ~Visits, type = 'scatter', mode = "lines", yaxis = "y2", name = "Visits") %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))

Resources