如何在哑铃图上换行文字

How to wrap text on dumbbell chart

我需要在我的哑铃图上换行。数据集在这里 https://www.file.io/download/ODUcXav56m9c

如何让图表显示完整的报表?我的代码如下:

overallp$statement <- factor(overallp$statement, levels = 
overallp$statement[order(overallp$prior)])
library(plotly)
fig <- plot_ly(overallp, color = I("gray36"))
fig <- fig %>% add_segments(x = ~prior, xend = ~current, y = ~statement,  size = I(4),yend = 
~statement, showlegend = FALSE, size = I(6))
fig <- fig %>% add_markers(x = ~prior, y = ~statement, size = I(50), name = "Prior Year", 
color = I("dodgerblue3"))
fig <- fig %>% add_markers(x = ~current, y = ~statement, size = I(50), name = "Current 
Year", color = I("gold"))
fig <- fig %>% layout(

xaxis = list(title = "Mean", showgrid = TRUE),
yaxis = list(title = "", showgrid = TRUE),
margin = list(l = 100))

fig <- fig %>% layout( xaxis = list(title = 'Mean')) %>% add_text(x = ~current, y = 
~statement,text = ~current, texttemplate="%{text}%", textposition = "top right", color = 
I("gold"), showlegend = FALSE)
fig <- fig %>% add_text(x = ~prior, y = ~statement,text = ~prior, texttemplate="%{text}%", 
textposition = "top right",color = I("dodgerblue3"), showlegend = FALSE)

fig

一种选择是使用 stringr 中的 str_wrap 来换行文本。

overallp$statement <- stringr::str_wrap(overallp$statement, 55)

您可以根据自己的选择使用 width 参数。