plotly / plotly/plotly.R

bug with the font parameter for annotation

Open
#780 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

I want to add an annotation to a plotly heatmap. There is a bug with the font parameter. I've followed the example given here.

library(plotly)

### heatmap data ### 
x <- 1:3; y <- 3:5
dd <- transform(expand.grid(x=x, y=y), z=x+y)

### plotly graphic ###
pp <- plot_ly(data=dd, x=~x, y=~y, z=~z, type="heatmap",
              hoverinfo = 'text',
              text = ~paste('<em>x</em>: ', dd$x, 
                            '</br> <em>y</em>: ', dd$y,
                            '</br> <em>z</em>: ', dd$z)) %>%
  layout(xaxis=list(title="<em>xaxis title</em>"))

### add annotation ###
pp <- pp %>% 
  add_annotations(  x = 1,
                    y = 4,
                    text = "<em>MY ANNOTATION</em>",
                    xref = "x",
                    yref = "y",
                    showarrow = TRUE,
                    arrowhead = 7,
                    ax = 10,
                    ay = 30,
                    font = list(color = 'red',
                                family = 'sans serif',
                                size = 24)
  )

That does not yield the expected result:

rplot_annotation

In fact, the problem lies in the fact that plotly_build generates three annotation layouts, one for each parameter of the font list:

> bpp <- plotly_build(pp)
> annotation <- bpp$x$layout$annotations
> length(annotation)
[1] 3
> sapply(annotation, function(x) x$font)
[1] "red"        "sans serif" "24"  

Thus, here is a way to get the expected result:

myannotation <- annotation[[1]]
myannotation$font <-  list(color = 'red',
                           family = 'sans serif',
                           size = 24)
bpp$x$layout$annotations <- myannotation
bpp

rplot_annotation_good

> packageVersion("plotly")
[1] ‘4.5.2’

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided R heatmap example and inspect the output of plotly_build(pp), especially x$layout$annotations. Trace how add_annotations handles the font list; done means color, family, and size remain one font object on one annotation instead of producing three annotations.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.