r - R plotly中绘图区域上方的位置标题

标签 r plotly

我有一个 plotly 绘图,默认情况下标题放置在绘图区域中。我想将其更改为在绘图区域之外设置绘图标题。

enter image description here

在屏幕截图中,标题位于“绘图区”(浅灰色)中,我希望它位于绘图区上方。

我当前的代码在绘图 layout 中只有 title 参数:

plt <- plt %>% 
layout(
    title = sprintf('Ice-formation Risk <br>%s', 
                    format(Sys.time(),format = '%d %b %Y %H:%M %Z')))

我试过使用 plotly reference 中指示的一些参数没有成功:

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')

plotly title 仍然消失了。我在那里有三个 y 轴,因为我的实际 plotly 需要三个。 margin 值被“调整”以使轴标签正确显示,否则它们会重叠并且不可读。

enter image description here

最佳答案

这真的很奇怪。通常引用是相当准确的。我向 Github 提交了一个问题.特别是因为关于 project website 的文档仍然使用 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)'))
  )

看起来像这样: enter image description here

关于r - R plotly中绘图区域上方的位置标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54131048/

相关文章:

r - 使用 R 将来自两个数据框的数据相乘并平均到一列中

r - 如何在 R 中的 h2o.grid 中调整 hidden_​​dropout_ratios

r - plotly 中的 add_trace,用于我的特定图形的跟踪

r - 交互式 `ggplotly` 图形不是从 R 中 `for` 文件中的 `Rmd` 循环内部绘制的

python - ValueError : Invalid property specified for object of type plotly. graph_objs.Pie: 'xaxis'

R保存ggplot pdf时出错

r - 双引号和 fread 函数的问题

r - 如何在 knit/Rstudio 的 HTML 输出中添加目录

javascript - R plotly : how to observe whether a trace is hidden or shown through legend clicks with multiple plots

python - 在Python中绘制有向图?