plotly / plotly/plotly.R

bug with the font parameter for annotation

未关闭
#780 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug
主要语言
R
星标
2.7k
派生
641
PR 合并指标
30 天内没有已合并 PR

描述

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’

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先运行提供的 R 热力图示例,并检查 plotly_build(pp) 的输出,尤其是 x$layout$annotations。跟踪 add_annotations 如何处理 font 列表;当 color、family 和 size 在一个 annotation 上仍是同一个 font 对象,而不是生成三个 annotation 时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
r
领域
data-visualization
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。