0

I am looking at this stackpost over here : How can I plotly a ggplot treemap?

I am able to make a mosaic plot/tree map with the following code using the plotly library:

library(plotly)

dtd7 <- structure(
  list(
    topic = structure(
      c(9L, 8L, 4L, 7L, 2L, 6L, 1L, 3L,
        5L, 10L, 13L, 11L, 12L),
      .Label = c("Apple", "Avocado", "Banana", "Carrot", "Mango","Mushroom", "Onion", "Orange", "Pineapple", "Strawberry", "Sweet-lemon", "Watermelon", "Wildberry"),
      class = "factor"
    ),
    n = structure(
      c(4L, 3L, 9L, 11L, 12L, 2L, 1L, 6L, 10L, 5L,
        7L, 8L, 1L),
      .Label = c("23", "24", "36", "42", "43", "46", "48", "52", "56", "61", "82", "94"),
      class = "factor"
    )
  ),
  class = "data.frame",
  row.names = c(NA,-13L)
)

p <- plot_ly(
  dtd7,
  labels = ~ topic,
  parents = NA,
  values = ~ n,
  type = 'treemap',
  hovertemplate = "Ingredient: %{label}<br>Count: %{value}<extra></extra>"
)

p

The computer I am using for work does not have a USB port or an internet connection. It only has R with a few libraries (plotly is not included).

Is it possible to replicate this plot only using ggplot2?

Thanks

  • 1
    Hi Noob. For sure is this possible but not out of the box. This could be achieved via e.g. http://wilkox.org/treemapify/ or https://rawgit.com/econandrew/ggtreemap/master/poverty-treemap.html. If you don't want or can't use any of these packages then you have do all the computations yourself to finally make a treemap via e.g. `geom_rect`. – stefan Nov 29 '20 at 08:35
  • thank you! i tried the second link (rawgit) and tried to run everything without downloading ggtreemap. I got the following error : Error: Can't find `stat` called 'treemap' ... do you know what I am doing wrong? Thanks –  Nov 29 '20 at 08:40

0 Answers0