在R中的绘图区域上方的位置标题

时间:2019-01-10 14:41:59

标签: r plotly

我有一个plotly图,默认情况下标题位于图区域中。我想更改它以使绘图标题位于绘图区域之外。

enter image description here

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

我当前的代码在图title中仅具有layout自变量:

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

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

enter image description here

2 个答案:

答案 0 :(得分:0)

那真的很奇怪。通常,参考是相当准确的。我向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

答案 1 :(得分:0)

在R软件包升级到plotly.js 1.43或更高版本之前,您将无法使用title.text

https://github.com/plotly/plotly.js/releases/tag/v1.43.0

https://github.com/ropensci/plotly/pull/1450