Plotly双Y轴图例移至底部时右侧次要坐标轴被截断问题问询
解决方案
核心原因是绘图区域的边距未适配底部图例和右侧Y轴的显示需求,通过调整layout相关参数即可实现需求,修复后可复现代码如下:
library(dplyr) library(plotly) plot_ly() %>% add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>% add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>% layout( title = "Double Y Axis", yaxis = list(title = "first y axis", title.standoff = 10), yaxis2 = list(tickfont = list(color = "red"), overlaying = "y", side = "right", title = "second y axis", title.standoff = 10), xaxis = list(title="x"), legend = list(orientation = 'h', x = 0.5, xanchor = "center", y = -0.15), margin = list(b = 50, r = 50) )
关键修改说明
margin参数:b设置底部边距为50像素,预留图例展示空间;r设置右侧边距为50像素,避免右侧Y轴刻度、标题被截断- 图例参数:将
x设为0.5、xanchor设为center,实现图例自动水平居中,无需手动设置固定偏移值,适配不同宽度的响应式场景 title.standoff参数:给两个Y轴分别配置该参数,设置轴标题与刻度标签的间距,避免二者重叠
内容的提问来源于stack exchange,提问作者Annet
相关产品推荐
相关产品推荐

