---
title: "Using tooltips in unexpected ways"
description: Rendering images, tables and even charts inside Highcharts tooltips.
date: 2019-02-04
date-modified: 2026-08-13
categories: [data-visualization, highcharter]
image: images/preview.gif
---
```{r setup, include=FALSE}
source(here::here("blog", "_R", "post_setup.R"))
install_missing_packages(c(
"tidyverse", "gapminder", "highcharter"
))
```
In highcharter R package there is `highcharter::tooltip_chart` helper function to put charts in the tooltip. Sadly the function is not that easy to use but is not impossible :).
## Documentation example
The example in the documentation is:
```{r prepare-gapminder-tooltip-data}
library(tidyverse)
library(gapminder)
library(highcharter)
data(gapminder, package = "gapminder")
gp <- gapminder |>
arrange(desc(year)) |>
distinct(country, .keep_all = TRUE)
gp2 <- gapminder |>
select(country, year, pop) |>
group_nest(country) |>
mutate(
data = map(data, mutate, x = year, y = pop, drop = TRUE),
data = map(data, list_parse)
) |>
rename(ttdata = data)
gptot <- left_join(gp, gp2, by = "country")
gptot
```
The data is ready! Now go to chart:
```{r build-tooltip-chart-example}
hchart(
gptot,
"point",
hcaes(lifeExp, gdpPercap, name = country, size = pop, group = continent, name = country)
) |>
hc_yAxis(type = "logarithmic") |>
# here is the magic (inside the function)
hc_tooltip(
useHTML = TRUE,
headerFormat = "<b>{point.key}</b>",
pointFormatter = tooltip_chart(accesor = "ttdata")
)
```
## Donut example
Now we'll use a donut chart and try to place the tooltip inside to give it importance:
```{r prepare-donut-tooltip-data}
donutdata <- gp |>
group_by(continent) |>
summarise(pop = sum(pop/1e6)*1e6)
hchart(donutdata, "pie", hcaes(name = continent, y = pop), innerSize = 300)
```
> Just according to keikaku.
The donut is ready. So now we need a detailed data from each continent to show in the tooltip.
```{r prepare-nested-donut-data}
donutdata2 <- gp |>
select(continent, lifeExp, gdpPercap) |>
group_nest(continent) |>
mutate(
data = map(data, mutate, x = lifeExp, y = gdpPercap, drop = TRUE),
data = map(data, list_parse)
) |>
rename(ttdata = data) |>
left_join(donutdata, by = "continent")
donutdata2
```
And the chart:
```{r build-donut-tooltip-chart}
hc <- hchart(
donutdata2,
"pie",
hcaes(name = continent, y = pop),
innerSize = 375
)
hc |>
hc_tooltip(
useHTML = TRUE,
headerFormat = "<b>{point.key}</b>",
pointFormatter = tooltip_chart(
accesor = "ttdata",
hc_opts = list(
chart = list(type = "scatter"),
credits = list(enabled = FALSE),
plotOptions = list(scatter = list(marker = list(radius = 2)))
),
height = 225
),
positioner = JS(
"function () {
/* one of the most important parts! */
xp = this.chart.chartWidth/2 - this.label.width/2
yp = this.chart.chartHeight/2 - this.label.height/2
return { x: xp, y: yp };
}"),
shadow = FALSE,
borderWidth = 0,
backgroundColor = "transparent",
hideDelay = 1000
)
```
What do you think? Maybe it's a kind of overkill, but hey, it's up to you to (ab)use it or not! Another more subtle alternative can be put text, i.e, the normal tooltip but with a bigger size, like a knob chart.