gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:02:49 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com