Right to left languages support in R, using Mac
Asked Answered
C

4

24

I am wondering if there is anyway to support right-to-left languages in R while using Mac

For instance suppose the following code:

x <- data.frame(a=runif(10),b=runif(10))
ggplot(x, aes(a,b)) + geom_point() + xlab("سلام")

Here is the result:

enter image description here

Here I tried to change the x-label as a Persian/Arabic word (سلام = Hello). While displayed correctly in the code (using R-Studio), in the graph the characters of سلام are displayed in the reverse order (left-to-right).

Also the linkage between two consecutive letters (i.e. س connected to ل would be سل) is broken.

Do you have any idea how to fix it?

Update

With the answer of @agstudy I found R under Ubuntu is perfectly OK. However I have no idea how to solve the problem in Mac (OS X 10.9.1) having updated version of R and R-Studio

Cruise answered 15/3, 2014 at 12:23 Comment(18)
Maybe rev(unlist(strsplit(your_label_text))) as a cheap workaround?Latvia
@Cruise it works just fine for me? Can you add your sessionInfo please?Transpontine
@CarlWitthoft it might be a step forward for the order of the letters, however the case is more complicated since there are non-joining (similar to english) or joining letters (the letters that join to the consecutive letters). It needs a lot of code. I am looking if there is something already available for the unicode languagesCruise
@Transpontine You mean the plot is correctly displaying سلام? Could you please provide me a link to your plot (uploaded in free image sharing services)?Cruise
@Cruise side note : Just one precision the word سلام is peace in Arabic ( I guess same thing in Persian). though I agree it is used to say hello :)Transpontine
@Transpontine I can replicate the problem. I'm running OSX with R 3.0.0Intervalometer
@ScottRitchie I am under windows but I can test it under ubuntu also.Transpontine
@Transpontine The direction seems ok for me under ubuntu, but I don't know any arabic, so I can't judge the linkage. ggplot did throw a tonne of warnings though. The version of R running on my ubuntu servers is 3.0.2Intervalometer
Work fine under ubuntu also : R version 3.0.3 (2014-03-06)/ Platform: x86_64-pc-linux-gnu (64-bit)Transpontine
@Cruise . I guess you need just to update your R version.Transpontine
@Transpontine I can reproduce this on my mac. It seems to be system dependent.Goop
@Goop I agree. It is MAC bug. Can you contact the maintainer please ( Or the OP should do it) ( I am from a PC right now).Transpontine
@Transpontine I wouldn't bother hadley with this because I'm not sure ggplot2 is at fault. I would need to investigate this, but since I'll never need arabic labels I'm not that interested.Goop
@Goop I see. neither me but for other reason : I don't have a MAC :)Transpontine
I have updated to R 3.0.3 and the issue still persists. The issue also persists with base plot.Intervalometer
@ScottRitchie intersting. Looks like a serious encoding problem.Transpontine
Looks like it has been around for a while, the problem also persists in R 2.15.0. I'm surprised this hasn't been picked up before.Intervalometer
I would recommend reformulating this as a non-ggplot question (for example, I can confirm with MacOS 10.6.8, R 3.0.3 that plot(1:10,1:10,ylab="hello",xlab="سلام",cex.lab=5) shows the same problem, whereas Linux (Ubuntu 12.04, R 3.1.0/devel) does not, and posting it either to the R devel mailing list ([email protected]) or the R MacOS SIG mailing list ...Heap
T
9

It works fine for me. I just change the size of labels to better show it.

library(ggplot2)
x <- data.frame(a=runif(10),b=runif(10))
ggplot(x, aes(a,b)) + 
  geom_point()+ xlab('سلام') +
  theme( axis.title=element_text(size=100,face="bold"))

enter image description here

I am using :

other attached packages:
[1] ggplot2_0.9.3.1
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

Also works fine under :

R version 3.0.3 (2014-03-06)
Platform: x86_64-pc-linux-gnu (64-bit)
Transpontine answered 15/3, 2014 at 13:0 Comment(1)
Thanks. It seems the code works perfectly in Linux, however not in Mac. I updated the question accordinglyCruise
F
0

I had the same problem with Hebrew letters, and I manage to work around it by reading the label from an Excel file instead of typing it directly into R studio. This method works as long as you don't need to mix letters with numbers, in that case things start to get messy. for example: for example

Flaunty answered 23/2, 2017 at 12:39 Comment(2)
While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please edit to provide example code to show what you mean. Alternatively, consider writing this as a comment instead.Acaleph
@TobySpeight I can't add comments yet, can you edit my answer to a comment instead?Flaunty
W
0

I know this is an old question, but I recently wrote an R package to deal with it. It's a simple R wrapper around some python code, since this problem also occurs when using python in mac. The package reverses the Arabic string and then reconnects the letters correctly using Abdullah Diab's python-arabic-reshaper module.

My package is here. Abdullah Diab's module (for python) is here.

Whitebait answered 9/11, 2020 at 17:25 Comment(0)
F
0

This question may be old, but I recently faced a similar issue and spent the last two days trying to find a solution. I attempted various solutions suggested in the responses, such as using cairo_pdf, but none of them worked for me on Windows.

I realized that I needed to find a graphics device that wouldn’t convert text to glyphs. Luckily, the svglite package does exactly that. However, incorporating an svg file into a LaTeX document posed another challenge.

Well, this is a solution:

current_plot_hook <- knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) {
  res <- current_plot_hook(x, options)

  filename <- file.path(options$fig.path, paste0(options$label,"-", options$fig.num))
  filenameext <- paste0(filename, ".svg")

  rsvg::rsvg_pdf(svg = filenameext, 
                 file = paste0(filename, ".pdf"),
                 height = options$fig.height * 92,
                 width = options$fig.width * 92)
 res
})
opts_chunk$set(
  dev = "svglite"
) 
Fourierism answered 9/11, 2023 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.