在R plotly中将标题位置放在绘图区域之上

3

我有一个 plotly 的图表,标题默认放在图表区域内。我想把图表标题移到图表区域外。

enter image description here

在截图中,标题位于“绘图区域”(浅灰色),我希望将其放置在绘图区域之上。
我的当前代码仅在绘图的布局中具有标题参数。
plt <- plt %>% 
layout(
    title = sprintf('Ice-formation Risk <br>%s', 
                    format(Sys.time(),format = '%d %b %Y %H:%M %Z')))

我尝试过根据plotly参考中的一些参数进行调整,但没有成功:

plt <- plt %>% 
layout(
  title = list(
    text = sprintf('Ice-formation Risk <br>%s', 
                   format(Sys.time(),format = '%d %b %Y %H:%M %Z')),
    xref = 'paper', yref = 'paper',
    color = 'rgb(17,17,17)'
  )
)

当我将title属性从字符串更改为list时,图表标题消失了。我尝试过删除xrefyref参数,只保留相同结果的text属性。

示例代码

plot1 <- plot_ly(iris) %>% 
  layout(
    title = list(
      text = 'sample plot', xref = 'x', yref = 'y', x = 0.5, y = 1, color = 'rgb(217,83,79)'
    ),
    margin = list( pad = 50, b = 90, l = 130, r = 50 ),
    yaxis = list(
      showline = F, side = 'left', title = 'Axis1', color = 'black', 
      overlaying = 'y3', zeroline = F
    ),
    yaxis2 = list(
      tickfont = list(color = "black"), showline = F, overlaying = "y3", side = "right",
      title = "Axis2", zeroline = F, anchor = 'free', position = 1
    ),
    yaxis3 = list(
      tickfont = list(color = "black"), side = "left", title = "Axis3", 
      zeroline = F, anchor = 'free'
    )
  ) %>% 
  add_trace(x = ~Sepal.Width, y = ~Sepal.Length, name = 'Sepal Length', color = ~Species,
            type = 'scatter', mode = 'markers') %>% 
  add_trace(x = ~Sepal.Width, y = ~Petal.Length, name = 'Petal Length', color = ~Species,
            type = 'scatter', mode = 'markers', yaxis = 'y2') %>% 
  add_trace(x = ~Sepal.Width, y = ~Petal.Width, name = 'Petal Width', color = ~Species,
            type = 'scatter', mode = 'markers', yaxis = 'y3')

情节标题仍然消失了。我需要三个y轴,因为我的实际情节需要三个。 margin值被“调整”以使轴标签正确显示,否则它们会重叠且无法读取。

enter image description here


尝试 xref='x',x=0.5,yref='y',y=1。标题需要是一个列表。如果您需要更多帮助,请创建一个最小的可重现示例或共享您的 plt 对象的代码。 - 5th
@5th 添加了一些示例代码。xrefyref 参数不起作用 - 标题只是消失了。 - Gautam
2个回答

3
那真的很奇怪。通常参考文献相当准确。我向Github提交了一个问题。特别是因为项目网站上的文档仍然使用title="some string"
无论如何,目前我建议您使用annotations选项。这个选项的工作方式就像参考文献中描述的那样;) 不过这有点像黑客行为。如果你把yshift设置得太高,它会溢出。希望它能帮到你:)
plot_ly(iris) %>%
  add_trace(x = ~Sepal.Width, y = ~Sepal.Length, name = 'Sepal Length', color = ~Species,
            type = 'scatter', mode = 'markers') %>% 
  add_trace(x = ~Sepal.Width, y = ~Petal.Length, name = 'Petal Length', color = ~Species,
            type = 'scatter', mode = 'markers', yaxis = 'y2') %>%
  add_trace(x = ~Sepal.Width, y = ~Petal.Width, name = 'Petal Width', color = ~Species,
            type = 'scatter', mode = 'markers', yaxis = 'y3') %>%
  layout(
       margin = list( pad = 50, b = 90, l = 130, r = 50 ),
    yaxis = list(
      showline = F, side = 'left', title = 'Axis1', color = 'black' ,
      overlaying = 'y3', zeroline = F
    ),
    yaxis2 = list(
      tickfont = list(color = "black"), showline = F, overlaying = "y3", side = "right",
      title = "Axis2", zeroline = F, anchor = 'free', position = 1
    ),
    yaxis3 = list(
      tickfont = list(color = "black"), side = "left", title = "Axis3",
      zeroline = F, anchor = 'free'
    ),annotations=list(text="sample text",xref="paper",x=0.5,
                      yref="paper",y=1,yshift=30,showarrow=FALSE, 
                      font=list(size=24,color='rgb(217,83,79)'))
  )

看起来像这样: {{link1:在此输入图片描述}}

1
谢谢!我已经有了这个解决方法,只是它不太优雅。顺便问一下,有没有一个可以解释plotly中使用的参数的参考资料 - 我发现参考资料并不是很详细。 - Gautam

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接