【问题标题】:How to wrap text on dumbbell chart如何在哑铃图上换行
【发布时间】:2021-05-22 04:58:05
【问题描述】:

我需要在我的哑铃图上换行。数据集在这里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

【问题讨论】:

    标签: r plotly r-plotly


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2021-12-26
      • 2019-10-17
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多