ggplot 2 generates RTL languages text backwords

Hi, I'm trying again to get help with this issue, since r ggplot2::theme() has no attribute for RTL languages I'm kinda lost.

R-markdown HTML document - RTL language text renders correctly
when adding plot with RTL language text it appears reversed

EXAMPLE

example <- data.frame(matrix(nrow = 20, ncol = 2))

example$Var1 <- sample(1:5, 20, replace = TRUE)
example$Var2 <- sample(1:12,20, replace = TRUE)

example$Var1 <- as.factor(example$Var1)

ggplot(data = example, aes(Var2)) + geom_bar(aes(fill = Var1)) +
  scale_fill_manual(values = cbPalette) +
  theme(text = element_text(family = "Arial Unicode MS")) +
  xlab("רמת דתיות") + ylab("אחוזונים מספר ילדים") +
  scale_x_discrete(labels=c("חילוני", "מסורתי", "דתי", "חרדי", "לא-יהודי"))  

can anyone please help solve it?

image

Hi @JonesYaniv
Try reading this article to see if it provides any insights:
https://www.tidyverse.org/blog/2021/02/modern-text-features/

Sorry but this didn't help since ggplot creates the plot with reversed text in it, so saving it to image through agg_ devices kept the reversed text.

While not an elegant solution, it occurs to me that simply characterwise reversing your text will achieve the end goal. see

library(tidyverse)
library(stringi) # for stri_reverse

example <- structure(list(Var1 = structure(c(
  3L, 1L, 1L, 2L, 5L, 5L, 4L,
  2L, 1L, 5L, 3L, 4L, 2L, 5L, 3L, 3L, 1L, 2L, 4L, 4L
), levels = c(  "1",  "2", "3", "4", "5"), class = "factor"), 
Var2 = c(  6L, 6L, 7L,  2L, 12L, 1L, 11L, 9L, 12L, 2L, 5L, 7L, 8L, 11L, 1L, 9L, 1L, 11L,4L, 11L
)), row.names = c(NA, -20L),
class = "data.frame")


ggplot(data = example, aes(Var2)) +
  geom_bar(aes(fill = Var1)) +
  theme(text = element_text(family = "Arial Unicode MS")) +
  xlab(stri_reverse("רמת דתיות")) +
  ylab(stri_reverse("אחוזונים מספר ילדים"))

Yeah I know, but since I have lots of plots to make with lots of text editing, I'm trying to avoid manually reversing all texts.

I'm to explore now r showtext_auto() to see if it can help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.