1

I am looking to get this gradient colors on the map:

ramp <- colorRamp(c("royalblue4", "white"))
ramp.list <- rgb( ramp(seq(0, 1, length = 15)), max = 255)

But also, and more important, I am looking to add plotly charactheristics to the graph (specially hovering text output). This is my data:

structure(list(V1 = 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"), 
    V2 = 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))

And this is what I've tried:

library(ggplot2)
library(plotly)

ramp <- colorRamp(c("royalblue4", "white"))
ramp.list <- rgb( ramp(seq(0, 1, length = 15)), max = 255)
g <- ggplot(dtd7, aes(area = n, fill = topic, label = as.character(topic))) +
  geom_treemap()+
  geom_treemap_text(fontface = "italic", colour = "white", place = "centre")  +
  theme(legend.position = "none") 

ggplotly(p)
MelaniaCB
  • 427
  • 5
  • 16

1 Answers1

5

Using treemap traces you can display hierarchical datasets.

Accordingly the problem with your code snippet from the comments plot_ly(dtd7, ids = ~topic, values = ~n, parents = ~topic, type = 'treemap') is, that you are assigning the same data to ids and parents.

Please check the following:

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

Result

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • Worked out perfectly fine, thanks! Would you happen to know how can I edit the hovering pop up to: Ingredient: ~topic, Count = ~n? Tried using `add_trace` but I can't fully figure how I can do this without knowing any JavaScript, sorry. – MelaniaCB Feb 13 '20 at 16:22
  • 1
    I just updated the answer. You should try running `schema()` and navigate: object ► traces ► treemap ► attributes ► hovertemplate - for further information. – ismirsehregal Feb 14 '20 at 08:04
  • Wonderful, really appreciate it! – MelaniaCB Feb 14 '20 at 16:16
  • Hello again - Could you please advice me on a way to show count below teach group name without the nned of hovering on it? – MelaniaCB Sep 15 '20 at 14:20
  • Sure :) - I just left an answer [here](https://stackoverflow.com/questions/63903111/add-count-below-name-in/) - cheers – ismirsehregal Sep 15 '20 at 14:37