Visualization

# Arabic Plots in R

Many R packages do not work well with right-to-left languages like Arabic. Here's how I've gotten around this problem.

I’ve read that this is not a problem for Linux users, but for Mac users like myself, neither base R graphics nor ggplot can properly display Arabic script. This appears to be more than an encoding problem; changing encodings to more Arabic-friendly alternatives doesn’t fix the problem. Here’s a basic bar plot with Arabic script, using the same data I used in this previous post; the pre-summarized data from the Arab Barometer III survey may be downloaded from here. Arabic readers will note that the text is both in the wrong direction and that letters are not properly connected, despite the fact that the script renders properly within the RStudio environment, i.e., where a string is assigned to mytitle and myname.

Of course the plot renders with no problem in English, as below.

I think that dealing with the encoding problem at the system-level would certainly be the best approach, but this is beyond what I can do. The same problems with rendering Arabic script in Microsoft Office for Mac persist, and I’d like to think that if there were an easy fix for this, Microsoft would have implemented this by now. After all, there are nearly 300 million native Arabic speakers, making it the fifth most-spoken language in the world.

Because nearly every modern web browser has no problem with the display of Arabic text, I passed the messy Arabic ggplot object through plotly, and it worked quite well. After transforming this to a plotly object, I only had to make a few minor adjustments. I moved the y-axis to the right-hand side, increased the right margin slightly. I moved the legend to the top-left corner. And the legend labels were “lost,” reverting to the trace name, so I had to re-apply these manually. Not every plot needs to be interactive. I appended .svg to the plotly url to embed a static version of the plot in this post.

GGPLOT · PLOTLY
tutorial survey arabic