[英]Position title above plot area in R plotly
I have a plotly
plot where the title is placed in the plot area by default. 我有一个
plotly
图,其中标题被默认放置在绘图区。 I would like to change this to have the plot title outside of the plot area. 我想更改它以使绘图标题位于绘图区域之外。
In the screenshot, the title is in the "plot area" (light grey), I would like this to be above the plot area. 在屏幕截图中,标题位于“绘图区域”(浅灰色)中,我希望它位于绘图区域上方。
My current code only has the title
argument in the plot layout
: 我当前的代码在绘图
layout
仅具有title
参数:
plt <- plt %>%
layout(
title = sprintf('Ice-formation Risk <br>%s',
format(Sys.time(),format = '%d %b %Y %H:%M %Z')))
I've tried playing with some of the parameters indicated in plotly
reference without success: 我尝试使用
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)'
)
)
The moment I change the title
attribute from a string to a list
, the plot title vanishes. 当我将
title
属性从字符串更改为list
,绘图标题就消失了。 I've experimented with removing the xref
and yref
parameters and leaving only the text
attribute with the same result. 我尝试用去除
xref
和yref
参数,只留下text
具有相同的结果属性。
Sample Code 样例代码
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')
The plot title
still vanishes. 剧情
title
仍然消失。 I have three y-axes in there because my actual plot needs to have three. 我在那里有三个y轴,因为我的实际绘图需要有三个。 The
margin
values were "tweaked" to get the axis labels to show properly, else they overlap and are unreadable. margin
值已“调整”以使轴标签正确显示,否则它们会重叠且不可读。
That is really odd. 真的很奇怪。 Usually the reference is quite accurate.
通常,参考是相当准确的。 I submitted an issue to Github .
我向Github提交了一个问题。 Especially since the documentation on the project website still uses
title="some string"
. 特别是由于项目网站上的文档仍然使用
title="some string"
。
Anyways for the moment I recommend you use the annotations
option instead. 无论如何,我建议您暂时使用
annotations
选项。 This one works like it is described in the reference ;) It is a bit of a hack though. 该程序的工作方式与参考文献中所述的一样;)虽然有点破绽。 If you put
yshift
too high it escapes. 如果您将
yshift
得太高,它就会逃脱。 I hope it helps anyways: 我希望无论如何都会有帮助:
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)'))
)
You won't be able to use title.text
until the R package upgrades to plotly.js 1.43 or higher 在R包升级到plotly.js 1.43或更高
title.text
之前,您将无法使用title.text
https://github.com/plotly/plotly.js/releases/tag/v1.43.0 https://github.com/plotly/plotly.js/releases/tag/v1.43.0
https://github.com/ropensci/plotly/pull/1450 https://github.com/ropensci/plotly/pull/1450
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.