wilkox / treemapify

🌳 Draw treemaps in ggplot2
http://wilkox.org/treemapify
213 stars 18 forks source link

is there a way to get a tooltip to work? #28

Closed dockstreet closed 6 years ago

dockstreet commented 6 years ago

I've seen tooltips in ggiraph, but have had no luck to enable html tool tips for a hover over. Is it supported by treemapify or are there other approaches?

https://github.com/davidgohel/ggiraph

wilkox commented 6 years ago

I don't have any plans to make a geom_treemap_interactive, but you can get a pretty decent approximation by drawing a geom_rect_interactive overlay with manually calculated treemap coordinates:

library(treemapify)
library(ggiraph)
library(ggfittext)
library(tidyverse)

# Manually build treemap layout
treemap <- treemapify(G20, area = "gdp_mil_usd")
treemap <- left_join(treemap, G20)

# Build ggplot2 treemap object
plot <- ggplot(treemap, aes(
  area = gdp_mil_usd,
  fill = region,
  xmin = xmin,
  xmax = xmax,
  ymin = ymin,
  ymax = ymax,
  tooltip = country,
  label = country
  )) +
  geom_treemap() +
  geom_treemap_text() +
  geom_rect_interactive(alpha = 0.1) +
  # Remove plot elements added by geom_rect_interactive
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme(axis.text = element_blank(), axis.ticks = element_blank())

# Draw with ggiraph
ggiraph(code = print(plot))