wilkox / treemapify

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

Accessing individual treemap boxes #35

Closed blaisdjo closed 4 years ago

blaisdjo commented 4 years ago

Hi, I am trying to access the individual treemap boxes. In the geom_treemap.R function I would like to associate with each treemap box the data rows of observations that fall into that box. For example, in the diamonds dataset, there are diamonds that satisfy cut: Ideal -> color: E -> clarity: VS1, I would like to store all records associated with those diamonds in the box for those diamonds.

I am trying to plot information on the treemap so I can see more than just the proportions, I would like to be able to draw a plot on top of the treemap boxes that tells me more about that subgroup.

Thank you so much for your time!

wilkox commented 4 years ago

Hi Josiah, I'm not sure I totally understand what you're trying to achieve. Do you want to draw a separate plot (such as a scatterplot or bar plot) within each treemap tile?

From what I understand I think the treemapify() function might be useful for you. It returns a data frame with the coordinates of each tile, which allows you to more easily do things like add a new layer on top of the plot. See for example the solution for this issue where treemapify() was used to set the coordinates for an interactive layer that displayed a tooltip for each tile.

blaisdjo commented 4 years ago

Wow! Thank you for getting back to me so quickly.

Data visualization is actually my research area, I am trying to come up with ways to simultaneously visualize categorical and continuous variables. One idea I have is related to this treemap. I would like to start by drawing a treemap and then selecting two continuous variables. The continuous variables would be used to draw bivariate kernel density functions on top of the treemap. I think what I need is something like:

geom_treemap_subgroup#_bivariate_pdf()

Where # gives the subgroup to draw the pdf onto.

When I look at the function geom_treemap_subgroup_text() I see that you set "geom = GeomSubgroupText". Inside the GeomSubgroupText function you set "draw_key = ggplot2::draw_key_text"

I was thinking that it would be simple, like you could replace draw_key with "draw_key = geom_density2d" but this did not work.

Note: The following code will let you compare bivariate distributions of sepal length and sepal width. I want to be able to put these bivariate pdfs into the boxes of the treemap so I can compare the distributions of different subgroups of the population.

my.theme <- theme_classic()  + theme(aspect.ratio = 1)
ggplot(iris, mapping = aes(area = Species, x = Sepal.Length, y = Sepal.Width)) + 
  geom_density2d() + 
  geom_jitter(alpha=.35) +
  sepal.labels + labs(subtitle = "I. setosa data only") +
  my.theme +
  facet_grid(Species ~.)
wilkox commented 4 years ago

Here's a proof-of-concept for overlaying treemap tiles with inset plots:

library(ggplot2)
library(treemapify)

inset_plot <- ggplot(iris, aes(area = Species, x = Sepal.Length, y = Sepal.Width)) + 
  geom_density2d()

treemap_plot <- ggplot(G20, aes(area = gdp_mil_usd, fill = hdi)) +
  geom_treemap()

treemap_coordinates <- treemapify(G20, "gdp_mil_usd")

add_inset_to_tile <- function(inset, country) {
  target_tile <- treemap_coordinates[which(treemap_coordinates$country == country), ]
  annotation_custom(
    ggplotGrob(inset), 
    xmin = target_tile$xmin, xmax = target_tile$xmax,
    ymin = target_tile$ymin, ymax = target_tile$ymax
  )
}

treemap_plot + 
  add_inset_to_tile(inset_plot, "European Union") +
  add_inset_to_tile(inset_plot, "China") +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

Created on 2019-10-08 by the reprex package (v0.3.0)

Is this what you're after?

josiahblaisdell commented 4 years ago

Yes, this is it, this is very nice, thank you so much!

blaisdjo commented 4 years ago

I don't suppose you know of a way to draw "circle treemaps" https://datavizcatalogue.com/methods/circle_packing.html

These have a constant aspect ratio.

Or better yet something close to this: http://graphics.uni-konstanz.de/publikationen/Goertler2018BubbleTreemapsUncertainty/bubble-treemaps.pdf

wilkox commented 4 years ago

I don't suppose you know of a way to draw "circle treemaps" https://datavizcatalogue.com/methods/circle_packing.html

That's interesting, I've never seen these before. Unfortunately I don't know of any way to draw them in ggplot2.

blaisdjo commented 4 years ago

Circle treemaps are actually really useful because the aspect ratio of a circle is always 1.

Maybe it will have to be my project :-) do you know where you might start looking in your code?

It looks like it will be here: https://github.com/wilkox/treemapify/blob/master/R/geom_treemap.R

Where you got to draw rects, ( grob <- grid::rectGrob( … ) ), I think you can replace that with grid::circle: https://www.rdocumentation.org/packages/grid/versions/3.6.1/topics/grid.circle

wilkox commented 4 years ago

If you want to add it as a feature to treemapify (which would be very much appreciated!), I think the place to start would be to write the layout algorithm. It should work something like treemap_fixed(), taking the plot data as input and returning a data frame of coordinates (x/y/radius) for the bubbles.

Once the layout algorithm is done, it should be reasonably straightforward to update the geom_*() functions to draw the 'bubble coordinates' with a layout = "bubble" argument.