如何在Plotly(R)中移动次坐标轴标题以避免与标记重叠?
解决Plotly右侧次坐标轴标题与刻度重叠的问题
以下三种方法可快速解决标题与刻度重叠的问题,按需选择即可:
方法1:调整标题与轴的间距
通过设置yaxis2的title.standoff属性,增加标题与坐标轴刻度的像素距离,直接拉开两者间距:
plotly::plot_ly(data = mtcars) %>% plotly::add_trace( x=~mpg, y=~wt, type='scatter', mode='markers', yaxis = "y", showlegend=F ) %>% plotly::add_trace( y=~drat, type='scatter', mode='markers', yaxis = "y2", showlegend=F ) %>% plotly::layout( yaxis = list( title="wt", showgrid=F ), yaxis2 = list( overlaying = "y", side = "right", title = list(text = "long title", standoff = 25) # 新增standoff参数调整间距 ) )
方法2:扩大图表右侧边距
在layout中设置margin参数,给右侧留出足够空间容纳标题:
plotly::plot_ly(data = mtcars) %>% plotly::add_trace( x=~mpg, y=~wt, type='scatter', mode='markers', yaxis = "y", showlegend=F ) %>% plotly::add_trace( y=~drat, type='scatter', mode='markers', yaxis = "y2", showlegend=F ) %>% plotly::layout( yaxis = list( title="wt", showgrid=F ), yaxis2 = list( overlaying = "y", side = "right", title = "long title" ), margin = list(r = 80) # 右侧边距,数值按需调整 )
方法3:旋转标题方向
将标题旋转为垂直方向,避免横向占用空间与刻度重叠:
plotly::plot_ly(data = mtcars) %>% plotly::add_trace( x=~mpg, y=~wt, type='scatter', mode='markers', yaxis = "y", showlegend=F ) %>% plotly::add_trace( y=~drat, type='scatter', mode='markers', yaxis = "y2", showlegend=F ) %>% plotly::layout( yaxis = list( title="wt", showgrid=F ), yaxis2 = list( overlaying = "y", side = "right", title = list(text = "long title", angle = 270) # 旋转标题为垂直方向 ) )
内容的提问来源于stack exchange,提问作者colej1390
相关产品推荐
相关产品推荐

