2

I want to add data labels for a treemap I have created. I am using this treemap for an image so having the pts and fgpct for each box would be helpful. I want what's listed in the tooltip and the legend to appear in each box.

My code:

library(highcharter)

gamelogs %>%
  filter(slugTeam == "MEM") %>%
  group_by(namePlayer) %>%
  summarise(pts = sum(pts), fgpct = sum(fgm) / sum(fga)) %>%
  hchart("treemap", hcaes(name = namePlayer, value = pts, color = fgpct)) %>%
  hc_title(text = "Grizzlies Scoring") %>%
  hc_subtitle(text = "Shaded by Field Goal %") %>%
  hc_chart(
    backgroundColor = '#FFFFFF' # Chart Background Color
  ) %>%
  hc_exporting(enabled = TRUE,
               filename = "Grizzlies Scoring")

My Output: Grizzlies Team Points Breakdown

The output I would like: enter image description here

This output would have the points 1,041 in the box and also the fgpct of 49% that is shown in the legend. Anyway to add the data labels using highcharter treemap?

Nick
  • 207
  • 1
  • 2
  • 11
  • Basically, you'd like to display this additional information above each dataLabel? I wonder if you need to edit a tooltip for this (it reacts dynamically to the user's interaction). Instead, wouldn't it be better to just adapt the appropriate format for dataLabelka to contain this information? – madepiet Jul 29 '20 at 09:40
  • Hey, I think I might not have explained it right. I want what is listed in the tooltip and the legend to be in every box. So if I took a screenshot of the treemap, all of the information will be there. – Nick Jul 29 '20 at 14:48

1 Answers1

3

Try this

gamelogs %>%
  filter(slugTeam == "MEM") %>%
  group_by(namePlayer) %>%
  summarise(pts = sum(pts), fgpct = round(sum(fgm) / sum(fga),digits=2)) %>% 
  hchart("treemap", hcaes(name = namePlayer, value = pts, color = fgpct),
         dataLabels = list(enabled = TRUE, format='{point.namePlayer}<br/>{point.pts} pts<br/>{point.fgpct} fgpct'),
         tooltip = list(pointFormat = "{point.namePlayer}: {point.pts}, {point.fgpct}%")) %>%
  hc_title(text = "Grizzlies Scoring") %>%
  hc_subtitle(text = "Shaded by Field Goal %") %>%
  hc_chart(
    backgroundColor = '#FFFFFF' # Chart Background Color
  ) %>%
  hc_exporting(enabled = TRUE,
               filename = "Grizzlies Scoring") %>% 
  hc_tooltip(crosshairs = TRUE)

you will get this output

output

YBS
  • 19,324
  • 2
  • 9
  • 27
  • This code doesn't change anything. I want what's listed in the tooltip and the legend to appear in each box. – Nick Jul 29 '20 at 04:47
  • Hey, I think I might not have explained it right. I want what is listed in the tooltip and the legend to be in every box. So if I took a screenshot of the treemap, all of the information will be there. For example, the 3 boxes you have shown do not show the information for points and field goal percentage in every box. – Nick Jul 29 '20 at 14:41
  • This is great! Is there a way to change the fgpct to an actual %? For example, Marc Gasol would have a 42% field goal percentage instead of .42 (which is the same thing). Also making the size of the pts and fgpct smaller would be helpful too. – Nick Jul 29 '20 at 16:14
  • 1
    Two changes should do it: one: `fgpct = 100*round(sum(fgm) / sum(fga),digits=2)`, and two: `dataLabels = list(enabled = TRUE,...
    {point.fgpct}%')`
    – YBS Jul 29 '20 at 16:31
  • Thanks again for your help. I am trying to use `dataLabels = list(enabled = TRUE, fontSize = '8pt'` to make the pts and fgpct smaller in each box. – Nick Jul 29 '20 at 16:44