繁体   English   中英

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

[英]Position title above plot area in R plotly

我有一个plotly图,其中标题被默认放置在绘图区。 我想更改它以使绘图标题位于绘图区域之外。

在此处输入图片说明

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

我当前的代码在绘图layout仅具有title参数:

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

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

在此处输入图片说明

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

看起来像这样: 在此处输入图片说明

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM