简体   繁体   English

如何在哑铃图上换行

[英]How to wrap text on dumbbell chart

I need to wrap text on my dumbbell chart.我需要在我的哑铃图上换行。 Dataset is here https://www.file.io/download/ODUcXav56m9c数据集在这里https://www.file.io/download/ODUcXav56m9c

在此处输入图像描述

How do I make the graph show the entire statements?如何使图表显示整个语句? My code is below:我的代码如下:

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

One option is to use str_wrap from stringr to wrap the text.一种选择是使用str_wrap中的stringr来换行文本。

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

在此处输入图像描述

You can play around with the width parameter according to your choice.您可以根据自己的选择使用width参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM