Add Count below name in plotly treemap

Viewed 742

Could somebody please tell me how I can make a plotly treemap to display each count number to get something like this image: enter image description here

Here's the code I'm working with:

dtd1 <- structure(list(topic = c("Access control", "Access management",  "Cipher text", "Data encryption", "Data protection", "Data transmission", "Malware", "Malware analysis", "Network security", "Secret encoding", "Secret key", "Storage devices", "Wireless network"), n = c(42L,36L, 56L, 82L, 94L, 24L, 23L, 46L, 61L, 43L, 48L, 52L, 23L)), row.names = c(NA,  -13L), class = "data.frame")

ramp4 <- colorRamp(c("darkblue", "lightblue"))
ramp.list4 <- rgb( ramp4(seq(0, 1, length = 15)), max = 255)

p <- dtd1  %>% 
  plot_ly(labels = ~topic, 
          values = ~n, 
          parents = ~NA, 
          type = 'treemap',
          hovertemplate = "Cluster: %{label}<br>Count: %{value}<extra></extra>") %>%
  config(modeBarButtonsToRemove = list(
    "zoom2d",
    "pan2d",
    "zoomIn2d",
    "zoomOut2d",
    "autoScale2d",
    "resetScale2d",
    "hoverClosestCartesian",
    "hoverCompareCartesian",
    "sendDataToCloud",
    "toggleHover",
    "resetViews",
    "toggleSpikelines",
    "resetViewMapbox"),
    displaylogo = FALSE) %>%
  layout(title = "Patent scape",
         colorway = ramp.list4)
p

There's none of this graphs in plotly library so I'm not completely aware about where to look!

1 Answers

You can simply modify your label as follows:

dtd1 <- structure(list(topic = c("Access control", "Access management",  "Cipher text", "Data encryption", "Data protection", "Data transmission", "Malware", "Malware analysis", "Network security", "Secret encoding", "Secret key", "Storage devices", "Wireless network"), n = c(42L,36L, 56L, 82L, 94L, 24L, 23L, 46L, 61L, 43L, 48L, 52L, 23L)), row.names = c(NA,  -13L), class = "data.frame")

ramp4 <- colorRamp(c("darkblue", "lightblue"))
ramp.list4 <- rgb( ramp4(seq(0, 1, length = 15)), max = 255)

p <- dtd1  %>% 
  plot_ly(labels = ~ paste0(topic, "<br>", n), 
          values = ~n, 
          parents = ~NA, 
          type = 'treemap',
          hovertemplate = "Cluster: %{label}<extra></extra>") %>%
  config(modeBarButtonsToRemove = list(
    "zoom2d",
    "pan2d",
    "zoomIn2d",
    "zoomOut2d",
    "autoScale2d",
    "resetScale2d",
    "hoverClosestCartesian",
    "hoverCompareCartesian",
    "sendDataToCloud",
    "toggleHover",
    "resetViews",
    "toggleSpikelines",
    "resetViewMapbox"),
    displaylogo = FALSE) %>%
  layout(title = "Patent scape",
         colorway = ramp.list4)
p

result

Related