R Highcharter - shared tooltip (and various tooltip arguments) not working - r

I'm trying to use highcharter to plot some time-series data:
volumes <- structure(list(priorDate = structure(c(18125, 18126, 18127, 18128,
18129, 18130), class = "Date"), s_1_1 = c(18481L, 18503L, 18247L,
17994L, 17755L, 17506L), s_1_2 = c(83143L, 83129L, 83160L, 83171L,
83120L, 83107L), s_1_3 = c(74456L, 74487L, 74510L, 74549L, 74614L,
74633L), s_1_4 = c(240095L, 240242L, 240416L, 240604L, 240776L,
241006L), s_2_10 = c(44542L, 44600L, 44605L, 44665L, 44749L,
44827L), s_2_11 = c(183468L, 183565L, 183628L, 183692L, 183706L,
183794L), s_2_5 = c(1400L, 1394L, 1415L, 1406L, 1462L, 1415L),
s_2_6 = c(1063L, 1041L, 1046L, 1050L, 1093L, 1051L), s_2_7 = c(38073L,
38066L, 38021L, 37936L, 37929L, 37875L), s_2_8 = c(19175L,
19156L, 19248L, 19345L, 19420L, 19502L), s_2_9 = c(44103L,
44052L, 44029L, 43939L, 43817L, 43747L)), row.names = 1:6, class = "data.frame")
I'm having a lot of trouble customising the tooltip. My goal is to have the tooltip display the data for all series. I thought this would do the trick:
highchart(type = "stock") %>%
hc_add_series(volumes, type = 'line', hcaes(x = priorDate, y = s_1_1), name = 'Prime Enquirer') %>%
hc_add_series(volumes, type = 'line', hcaes(x = priorDate, y = s_1_2), name = 'Active Enquirer') %>%
hc_xAxis(type = 'date') %>%
hc_tooltip(shared = TRUE)
But this plots in exactly the same fashion as without hc_tooltip(shared = TRUE).
I am also unable to get the chart to display a legend. I have to assume I'm being an idiot and missing something really simple.
I'd appreciate any help; this has been annoying me for a while.

Related

How to configure thousand separator for hovertext in R plotly?

It looks like a simple task, but after hours looking aroung I'm not able to find a suitable solution.
I'm using plotly to plot line and bar charts, and I have trouble formatting hover numbers.
I want the following :
decimal separator ','
thousand separator ' '
I'm able to setup the decimal separator by setting the locale to FR. I'm also able to get space separator for the ticklabeld.
However I'm not able at all to add space as thousand separator.
I would like to find an elegant solution by setting hovertemplate = '%{x} | %{y:.0f}' , but any other solution would fit.
This is the code for my plot :
data <- EMPL1019[,c(1,2,4)]
colnames(data) <- c('Date','Salaries','Equivalent')
ticklabels <- seq(from=0, to=round(max(data$Salaries)), by=5000)
ticktexts <- c(0,paste(ticklabels[-1]/1000, " 000", sep=""))
EMP.g1 <- plot_ly(data = data,
name = 'Salariés',
line = list(color = ispfPalette[1]),
x = ~Date,
y = ~Salaries,
hovertemplate = '%{x} | %{y:.0f}<extra></extra>',
type = 'scatter',
mode = 'line')
EMP.g1 <- EMP.g1 %>% add_trace(y = ~Equivalent,
hovertemplate = '%{x} - %{y:.2f}<extra></extra>',
name = 'Equ. temps plein',
line = list(color = ispfPalette[9]))
EMP.g1 <- EMP.g1 %>% layout(yaxis = list(separatethousands = T,tickformat = '.0f'))
EMP.g1 <- EMP.g1 %>% layout(yaxis=list(tickvals = ticklabels,
ticktext = ticktexts))
EMP.g1 <- EMP.g1 %>% layout(yaxis = yaxis_template,
xaxis = xaxis_template,
legend = list(orientation = 'h'))
EMP.g1 <- EMP.g1 %>% config(locale = 'fr')
And this is a sample of my data :
structure(list(Date = structure(c(1635724800, 1633046400, 1630454400,
1627776000, 1625097600, 1622505600, 1619827200, 1617235200, 1614556800,
1612137600, 1609459200, 1606780800, 1604188800, 1601510400, 1598918400,
1596240000, 1593561600, 1590969600, 1588291200, 1585699200, 1583020800,
1580515200, 1577836800, 1575158400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), `Nombre de salariés` = c(67124L, 66347L,
65629L, 66937L, 66503L, 65780L, 64963L, 64820L, 64320L, 63978L,
64320L, 65139L, 64726L, 64824L, 64349L, 64252L, 63802L, 63302L,
60612L, 58449L, 66069L, 66680L, 66934L, 67530L), `Masse salariale` = structure(c(0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000938992338891789,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000909115204071465,
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000088773284923459,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000896660284134519,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000906967890625601,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000916330649236409,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000889642613151166,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000854022123447163,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000855530315648643,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000848916147880622,
0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008974157736486,
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110317720806686,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000870705472791466,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000859874685909506,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000857318642824282,
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000085576196400845,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000840277121133498,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000843657217346959,
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000082555304014476,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000801471849889434,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000900019006376449,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000904107403400091,
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000979945565570618,
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000116166151318982
), class = "integer64"), `Effectif équivalent temps plein` = c(58126.7732710957,
57206.3988279215, 55259.2352879171, 55479.1570677117, 56688.511680619,
55841.9537494603, 54526.0767610181, 53368.5979797748, 53134.6513287857,
52946.3782282233, 53251.3191244597, 54829.7587531314, 53915.6804815763,
54306.2860265348, 54170.5949118788, 53649.4815305165, 52445.0726773908,
51356.6072581076, 50193.6687158648, 48360.6793136537, 56294.0759669762,
57709.3582162786, 57246.9744898572, 58543.1814151947)), class = c("data.table",
"data.frame"), row.names = c(NA, -24L), .internal.selfref = <pointer: 0x000001d4019765b0>)
I can't trust that there is no easy way to do it. Even tickvals and ticktext solution looks overcomplex and I'm sure there is a better way to do it.
Thanks in advance for your help !
It works, but it's not native Plotly. What I did was add the parameter hovertext, I then used this to format the values you used in y = Salaries and y = Equivalent. Then I replaced y in your hovertemplate with hovertext.
(I've only commented out the palettes because I don't have the data for these objects.)
EMP.g1 <- plot_ly(data = data,
name = 'Salariés',
line = list(color = 'darkred'), #ispfPalette[1]),
x = ~Date,
y = ~Salaries,
hovertext = ~formatC(Salaries, big.mark = " ",
format = "d"), # integer (no decimal)
hovertemplate = '%{x} | %{hovertext}<extra></extra>',
type = 'scatter',
mode = 'line')
EMP.g1 <- EMP.g1 %>%
add_trace(y = ~Equivalent,
hovertext = ~formatC(Equivalent,
big.mark = " ", # space separator
format = "d"), # integer (no decimal)
hovertemplate = '%{x} - %{hovertext}<extra></extra>',
name = 'Equ. temps plein',
line = list(color = 'black')) #ispfPalette[9]))
The rest of your code is unchanged.

Avoid ordering hoverinfo by group when using legendgroup in plotly

I have build a Gantt chart in plotly as per example.
I want to use trace filtering with plotly legend, but the code creates an enormous amount of traces which results in hundreds of traces in legend as well. To avoid that - I first use add_lines() on one row per each group with parameter showlegend = T and by specifying legendgroup. After that, I add the remaining lines to the figure with showlegend = F and by specifying the same legendgroup. This works great for the problem, but it messes the text flow inside hoverinfo. Namely, it groups and orders the text by legendgroup instead of allowing them to be ordered by xaxis value.
Data (an excerpt from Soderberg's Sex, Lies & Videotape :)):
library(plotly)
library(dplyr)
df_plotly = structure(list(tt = 1:9, speaker = c("A", "T", "A", "T", "A",
"T", "A", "T", "A"), min_time = c(42.328, 50.67, 53.297, 79.073,
87.54, 116.569, 120.948, 122.45, 131.959), max_time = c(50.5,
55.67, 81.573, 86.203, 112.938, 121.569, 125.948, 130.413, 136.959
), line = c("</br> Garbage.</br>All I've been thinking about all week is garbage.</br>I mean, I can't stop thinking about it.",
"</br> What kind of thoughts about garbage?", "</br> I just</br>I've gotten real concerned over what's gonna happen with all the garbage.</br>I mean, we've got so much of it.</br>You know? I mean, we have to run out of places to put this stuff eventually.</br>The last time I .</br>I started feelin' this way is when that barge was stranded</br>and, you know, it was going around the island and nobody would claim it.</br>Do you remember that?",
"</br> Yes, I remember.</br>Do you have any idea what may have triggered this concern?",
"</br> Yeah. Yeah.</br>You see, the other night, John was taking out the garbage,</br>and he kept spilling things out of the container,</br>and that made me</br>I started imagining, like,</br>a garbage can that produces garbage, and it doesn't stop.</br>It just keeps producing garbage, and it just keeps overflowing.</br>And, yy-you know, what would you do, you know, to try to stop something like that?",
"</br> Ann, do you see any pattern here?", "</br> What do you mean?",
"</br> Well, last week we were talking about your obsession</br>with the families of airline fatalities.</br>Now we're talking about your concern over the garbage problem.",
"</br> Yeah? So?"), color = structure(c(1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L), .Label = c("#66C2A5", "#FC8D62"), class = "factor")), row.names = c(NA,
-9L), class = c("tbl_df", "tbl", "data.frame"))
Gantt without legend with correct order inside hoverinfo.
fig = plot_ly() %>% layout(hovermode = "x unified")
for(i in 1:(nrow(df_plotly) - 1)){
df =
df_plotly
fig <- add_lines(fig,
x = c(df$min_time[i], df$max_time[i]), # x0, x1
y = c(df$speaker[i], df$speaker[i]), # y0, y1
name = df$speaker[i],
mode = "lines",
line = list(color = df$color[i], width = 20),
hoverinfo = "text",
text = df$line[i],
evaluate = T # needed to avoid lazy loading
)
}
fig
Gantt with legend but with incorrect order inside hoverinfo:
fig = plot_ly() %>% layout(hovermode = "x unified")
for(i in 1:2){
df =
df_plotly %>%
group_by(speaker) %>%
filter(min_time == min(min_time)) %>%
ungroup()
fig <- add_lines(fig,
x = c(df$min_time[i], df$max_time[i]), # x0, x1
y = c(df$speaker[i], df$speaker[i]), # y0, y1
name = df$speaker[i],
mode = "lines",
line = list(color = df$color[i], width = 20),
legendgroup = df$speaker[i],
showlegend = T,
hoverinfo = "text",
text = df$line[i],
evaluate = T # needed to avoid lazy loading
)
}
for(i in 1:(nrow(df_plotly) - 1)){
df =
df_plotly %>%
group_by(speaker) %>%
filter(min_time != min(min_time)) %>%
ungroup()
fig <- add_lines(fig,
x = c(df$min_time[i], df$max_time[i]), # x0, x1
y = c(df$speaker[i], df$speaker[i]), # y0, y1
name = df$speaker[i],
mode = "lines",
line = list(color = df$color[i], width = 20),
legendgroup = df$speaker[i],
showlegend = F,
hoverinfo = "text",
text = df$line[i],
evaluate = T # needed to avoid lazy loading
)
}
fig
Thanks!

Plotly barcharts with POSIXct and daylight-savings-time

I want to plot a barchart timeseries with timestamps on the x-axis. My problem is the daylight-saving in Octobre, as there are 2 timestamps at 02:00, one with CEST timezone and one with CET.
The plotly-barchart superimposes these values, which is hard to spot and looks like an error when you hover over the barchart as you will find that the value does not correspond to the y-axis.
How can I display these 2 bars side by side, without changing the tickmode to "array" and defining tickvals/ticktext?
library(plotly)
df <- structure(list(value = round(runif(46, 20, 26), 3),
timestamp = structure(c(1603576800, 1603584000, 1603587600, 1603591200, 1603594800, 1603598400,
1603602000, 1603605600, 1603609200, 1603612800, 1603616400, 1603620000,
1603623600, 1603627200, 1603630800, 1603634400, 1603638000, 1603641600,
1603645200, 1603648800, 1603652400, 1603656000, 1603659600, 1603576800,
1603584000, 1603587600, 1603591200, 1603594800, 1603598400, 1603602000,
1603605600, 1603609200, 1603612800, 1603616400, 1603620000, 1603623600,
1603627200, 1603630800, 1603634400, 1603638000, 1603641600, 1603645200,
1603648800, 1603652400, 1603656000, 1603659600),
class = c("POSIXct", "POSIXt"), tzone = "Europe/Berlin"),
col = c(rep("#FFBA00", 23), rep("#B9CC2E", 23)),
name = c(rep("Grp1", 23), rep("Grp2", 23))),
row.names = 1:46, class = "data.frame")
plot_ly(data = df, text = "text") %>%
add_trace(x = ~timestamp, y = ~value, type = "bar",
marker = list(color = ~col),
text = ~sprintf("Time: %s<br>Value: %s", timestamp, value),
hoverinfo = "text",
name = ~name) %>%
plotly::layout(xaxis = list(title = 'Time', type = "date"),
barmode = 'group')
Wraping up my comments to an answer for future readers:
plotly currently doesn't support displaying daylight saving time natively:
Changes our internal date linearization to use UTC rather than local
milliseconds. Every day on a Plotly graph will now be 24 hours long,
with no daylight shifts.
source: https://github.com/plotly/plotly.js/pull/1194#issue-95087563
Accordingly, the only possibility I'm aware of, to work around this issue is using the tickvals and ticktext arguments in plotly's layout function (not desired by #SeGa and this is reasonable as we'll have duplicated tick labels. Nevertheless, maybe the following helps others).
library(plotly)
DF <- structure(list(value = round(runif(46, 20, 26), 3),
timestamp = structure(c(1603576800, 1603584000, 1603587600, 1603591200, 1603594800, 1603598400,
1603602000, 1603605600, 1603609200, 1603612800, 1603616400, 1603620000,
1603623600, 1603627200, 1603630800, 1603634400, 1603638000, 1603641600,
1603645200, 1603648800, 1603652400, 1603656000, 1603659600, 1603576800,
1603584000, 1603587600, 1603591200, 1603594800, 1603598400, 1603602000,
1603605600, 1603609200, 1603612800, 1603616400, 1603620000, 1603623600,
1603627200, 1603630800, 1603634400, 1603638000, 1603641600, 1603645200,
1603648800, 1603652400, 1603656000, 1603659600),
class = c("POSIXct", "POSIXt"), tzone = "Europe/Berlin"),
col = c(rep("#FFBA00", 23), rep("#B9CC2E", 23)),
name = c(rep("Grp1", 23), rep("Grp2", 23))),
row.names = 1:46, class = "data.frame")
# DF$timestamp_utc <- DF$timestamp
# attr(DF$timestamp_utc, "tzone") <- "UTC"
plot_ly(data = DF, text = "text") %>%
add_trace(x = ~as.numeric(timestamp), y = ~value, type = "bar",
marker = list(color = ~col),
text = ~sprintf("Time: %s<br>Value: %s", timestamp, value),
hoverinfo = "text",
name = ~name) %>%
plotly::layout(xaxis = list(title = 'Time',
ticktext = ~timestamp,
tickvals = ~as.numeric(timestamp),
tickmode = "array"),
barmode = 'group')
Minor edit: If we want to reduce the amount of ticklabels without a lot of effort we can use pretty in ticktext and tickvals:
ticktext = ~pretty(timestamp),
tickvals = ~as.numeric(pretty(timestamp))
Related docs: Formatting Ticks in R

Chaning labels in choropleth hcmap

I have the following dataset:
structure(list(code = structure(1:6, .Label = c("?elino", "?tip",
"?uto Orizari", "Aerodrom", "Aracinovo", "Berovo", "Bitola",
"Bogdanci", "Bogovinje", "Bosilovo", "Brod", "Brvenica", "Butel",
"Ca?ka", "Cair", "Ce?inovo-Oble?evo", "Centar", "Centar ?upa",
"Cucer Sandevo", "Debar", "Debarca", "Delcevo", "Demir Hisar",
"Demir Kapija", "Dojran", "Dolneni", "Drugovo", "Gazi Baba",
"Gjorce Petrov", "Gostivar", "Gradsko", "Ilinden", "Jegunovce",
"Karbinci", "Karpo?", "Kavadartsi", "Kicevo", "Kisela Voda",
"Kocani", "Konce", "Kratovo", "Kriva Palanka", "Krivoga?tani",
"Kru?evo", "Kumanovo", "Lipkovo", "Lozovo", "Makedonska Kamenica",
"Mavrovo and Rostusa", "Negotino", "Northeastern", "Novatsi",
"Novo Selo", "Ohrid", "Oslomej", "Pelagonia", "Phecevo", "Plasnica",
"Polog", "Prilep", "Probistip", "Radovis", "Rankovce", "Resen",
"Saraj", "Skopje", "Sopiste", "Southeastern", "Struga", "Studenicani",
"Sveti Nikole", "Tearce", "Tetovo", "Valandovo", "Vardar", "Vasilevo",
"Veles", "Vev?ani", "Vinitsa", "Vrane?tica", "Zajas", "Zelenikovo",
"Zrnovci"), class = "factor"), value = c(48L, 1810L, 205L, 1507L,
38L, 66L), OPSTINA_NAZIV = c("ЖЕЛИНО", "ШТИП", "ШУТО ОРИЗАРИ",
"АЕРОДРОМ", "АРАЧИНОВО", "БЕРОВО"), `postal-code` = c("ZE", "ST",
"SO", "AD", "AR", "BR")), .Names = c("code", "value", "OPSTINA_NAZIV",
"postal-code"), row.names = c(NA, 6L), class = "data.frame")
and I'm plotting a choropleth map with the hcmap function below:
hcmap("countries/mk/mk-all.js", data = data_fake,
name = "Manucipalities", value = "value", joinBy = c("name", "code"),
borderColor = "transparent") %>%
hc_colorAxis(dataClasses = color_classes(c(seq(0, 2000, by = 500), 13000))) %>%
hc_legend(layout = "vertical", align = "right",
floating = TRUE, valueDecimals = 0, valueSuffix = "") %>%
hc_mapNavigation(enabled = TRUE)
However, at the moment the labels that appear on the map are from the "code" variable, which contain encoding problems. I want to plot the labels from the "OPSTINA_NAZIV" label.
Any ideas how I can do this?
I tried:
dataLabels = list(enabled = TRUE, format = '{point.OPSTINA_NAZIV}')
But it didn't work out.
You can access to the mapData info using the options accesor. Example {point.options.OPSTINA_NAZIV}:
hcmap("countries/mk/mk-all.js", data = data_fake,
name = "Manucipalities", value = "value", joinBy = c("name", "code"),
borderColor = "transparent" ,
dataLabels = list(enabled = TRUE, format = "{point.options.OPSTINA_NAZIV}"))

highcharter - where are the already downloaded maps for hcmap() stored on a (shiny) server?

My setup: I am using two selectizeInputs togehter with hcmap() in order to display a world map (this one custom/world-palestine-highres) where users can change the institute and the variable they want to look at. Hence, the map gets rendered often!
In this process, I downloaded the map multiple times via testing/rendering the app till I found the options(highcharter.download_map_data = FALSE) while browsing through your repository - the vignette on how to use download_map_data is not that clear tbh. However, now the map gets rendered without downloading itself again --> perfect!
Now to my question: Where does the map get stored on the server when downloaded the first time? Because with my current setup, no user will download the map again (which is what I want), but what if we have to restart the server or move files? I just want to make sure that I dont have to service the app all the time in regards to map downloads.
EDIT#1: Code samples for JBKunst, as the new code doesn't show any data
My hcmap code I used before:
hcmap("custom/world-palestine-highres", data = datapool_credit, joinBy = c("hc-a2", "Country"), value = capital_variable,
name = capital_variable,
dataLabels = list(enabled = TRUE, format = '{point.name}'),
borderColor = "#FAFAFA", borderWidth = 0.1,
tooltip = list(valueDecimals = 0, valuePrefix = "€", valueSuffix = "")) %>%
hc_mapNavigation(enabled = TRUE)
The new code as suggested by JBKunst:
highchart(type = "map") %>%
hc_add_series(
mapData = JS("Highcharts.maps['custom/world-palestine-highres']"),
data = datapool_credit, joinBy = c("hc-key"), joinBy = c("hc-a2", "Country"), value = capital_variable,
name = capital_variable,
dataLabels = list(enabled = TRUE, format = '{point.name}'),
borderColor = "#FAFAFA", borderWidth = 0.1,
tooltip = list(valueDecimals = 0, valuePrefix = "€", valueSuffix = "")) %>%
hc_mapNavigation(enabled = TRUE)
EDIT#2: structure of used data for the map (changed the numbers for data privacy reasons, but not the format)
> dput(head(datapool_credit))
structure(list(Country = c("AE", "AF", "AL", "AM", "AO", "AQ"
), PD = c(0.506010018321176, 64.350505, 8.94600128868667, 14.29401046096271,
25.0439479, 3.5626)), .Names = c("Country", "PD"), class = c("data.table",
"data.frame"), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x244ccb8>)
Best regards,
Martin
you can use something like https://github.com/jbkunst/shiny-apps/tree/master/highcharter-maps-performance
You don't need to store the map. Instead the user will read from https://code.highcharts.com/mapdata/custom/world-palestine-highres.js one time per session just like the highcharts example:
In your ui.R you need to add the map
# ui.R
...
tags$script(src = "https://code.highcharts.com/mapdata/custom/world-palestine-highres.js")
...
Then in the server.R don't use the hcmap, instead use highchart():
# server.R
...
highchart(type = "map") %>%
hc_add_series(
mapData = JS("Highcharts.maps["custom/world-palestine-highres"]"),
data = data, joinBy = c("hc-key")
)

Resources