Black Lives Matter. Please consider donating to Black Girls Code today.

Displaying Arabic and RTL Languages

Although plotly does display Arabic text correctly, when I try to use str_wrap or other means to wrap titles, it doesnt display the text properly. It appears to wrap the text incorrectly, breaking the sentence up at the wrong points.

I produce thousands of graphs in loops and they often have long titles, so I need a solution that would work iterably for different lengthed titles.

Heres a minimal working example:

string = “هل يمكن أن تخبرني بعمرك التقريبي”

title = str_wrap(string, width = 20)
plot_ly(data = data.frame(x=sample(1:4,100, replace = TRUE), y=sample(1:100,100, replace= TRUE)), x=~x, y=~y)%>%
add_histogram()%>%
layout(title = title)

I’m running OSX

Update:

plot_ly(data = data.frame(x=sample(1:4,100, replace = TRUE), y=sample(1:100,100, replace= TRUE)), x=~x, y=~y)%>%
add_histogram()%>%
layout(title = paste( rev(strwrap(string, 20, prefix = “\u202e”, initial = “\u202e”)), collapse = “\u202e \n \u202e”, sep = “\u202e \u202e”))

I used stringwrap to splice the text into a vector, recollapse the vector with line breaks and RTL unicode characters and it seems to work. I think the issue is either R or plotly not reading RTL correctly so its breaking the text in the wrong spot.