New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Implement new coord_mirror() and theme_rtl() for RtL plotting #2817

Open
wants to merge 3 commits into
base: master
from

Conversation

Projects
None yet
3 participants
@dpseidel
Member

dpseidel commented Aug 9, 2018

This PR is a WIP. Inspired by this article, I have begun playing around with how to implement RtL plotting in ggplot2. Though there seems to be some debate about what is a correct implementation of RtL axes and it seems many RtL languages plot LtR anyhow, I think the functionality is really neat and ggplot2 should consider adding features that make it possible for the audiences that would use it. In that vein....

Currently this implementation was really quite simple. I simply adapted coord_flip() to create a new coord_mirror() and then added a new theme_rtl(). This doesn't yet deal with the text encoding issue mentioned in the original article but as they note much of that is OS specific so possibly/probably outside the scope of ggplot2.

As currently implemented, a RtL plot requires 3 specifications: the new theme, the new coord, and (at least for continuous data) scale_x_reverse(). I wonder if there is a better way to go about this or a way to bundle these three elements together in some sort of default. Example below:

library(ggplot2)
library(tibble)
df <- tibble(x = 1:10, y = 1:10, z = rep(c("A", "B"), 5))
ggplot(df, aes(x, y, col = z)) + 
  geom_point() + 
  coord_mirror() + 
  theme_rtl() + 
  scale_x_reverse() +
  ggtitle("Right Aligned Title")

df2 <- tibble(x = letters[1:10], y = 1:10, z = rep(c("A", "B"), 5))
ggplot(df2, aes(x, y, col = z)) + 
  geom_point() + 
  coord_mirror() + 
  theme_rtl() +
  ggtitle("Right Aligned Title")

@isteves has been super helpful in thinking about this, and may have some opinions to add to any discussion or iteration so I'm tagging her here.

dpseidel added some commits Jul 18, 2018

@clauswilke

This comment has been minimized.

Member

clauswilke commented Aug 9, 2018

I would expect a coord_mirror() to reverse the x axis.

@dpseidel

This comment has been minimized.

Member

dpseidel commented Aug 9, 2018

@clauswilke certainly it could be implemented that way. The plot I was original trying to replicate, from the article linked above, did not flip the x axis (they were plotting a discrete variable) which is why I left it the way I did, but you're right that's an easier implementation.

@clauswilke

This comment has been minimized.

Member

clauswilke commented Aug 9, 2018

I think all you need to do is add the following function to your CoordMirror():

  setup_panel_params = function(self, scale_x, scale_y, params = list()) {
    params <- ggproto_parent(CoordCartesian, self)$setup_panel_params(scale_x, scale_y, params)
    
    params$x.range <- rev(params$x.range)
    params$x.labels <- rev(params$x.labels)
    params
  }

And it seems to work with discrete and continuous scales. I would assume in right-to-left writing the first letter in the alphabet is shown in the right-most position, which this addition to the coord achieves.

@dpseidel

This comment has been minimized.

Member

dpseidel commented Aug 9, 2018

Great! Thanks for the quick code @clauswilke! It's implemented now and things actually mirror as you would expect without the scale_x_reverse() call.

library(ggplot2)
library(tibble)
df <- tibble(x = 1:10, y = 1:10, z = rep(c("A", "B"), 5))
ggplot(df, aes(x, y, col = z)) +
  geom_point() +
  coord_mirror() +
  theme_rtl() +
  ggtitle("Right Aligned Title")

df2 <- tibble(x = letters[1:10], y = 1:10, z = rep(c("A", "B"), 5))
ggplot(df2, aes(x, y, col = z)) +
  geom_point() +
  coord_mirror() +
  theme_rtl() +
  ggtitle("Right Aligned Title")

Created on 2018-08-09 by the reprex
package
(v0.2.0).

@clauswilke

This comment has been minimized.

Member

clauswilke commented Aug 10, 2018

Another point: Maybe make theme_rtl() not a complete theme so it can be combined with other themes. I.e., something like:

theme_rtl <- function() {
  theme(
    legend.position = "left",
    plot.title = element_text(hjust = 1)
  )
}

(Maybe there's a better name for the function, though, so it sounds like a verb and not a noun. Maybe make_theme_rtl() or reverse_theme().)

@isteves

This comment has been minimized.

isteves commented Aug 10, 2018

Hi all, I don't have anything technical to contribute to this discussion, but I'd like to summarize my investigation into RTL graphs here (@dpseidel has already heard much of this).

I'm of the camp that RTL graphs with continuous axes should not be encouraged. (In my mind, it's a bit like all agreeing to the metric system and then deciding to switch back into non-standard units.) Mathematics has a standard convention with regards to axes, and I think we should stick with it.

Conversations with people familiar with RTL languages

  • Iranian lady (psychology PhD) that I met in a hostel - graphs are always LTR, tables are RTL. Math is LTR
  • Pakistani taxi driver (Aamer) - both RTL/LTR are taught in schools, depending on which language (Urdu/English) is being used
  • Israeli friend 1: "I did it only once or twice. It is rare in papers that I saw."
  • Israeli friend 2: "I also hate RTL in this context, and changed it for my school pupils when they gave me such figures." (note: this is what Excel defaults to for RTL languages)
  • East Asian studies PhD: In China, writing is almost always LTR. In Taiwan, it's still mixed. You can get writing that is RTL/LTR/up-to-down, but typically horizontal writing is now LTR and vertical writing is RTL. Regardless, all graphs are LTR.

Online graph search

I found it surprisingly difficult to search for graphs in RTL languages, but I hit on a few methods that seem to work ok:

  • Google Image search "stock market" or "growth" or "statistics" - I used Google Translate to get words in Arabic/Hebrew/Persian/Urdu for my searches. The vast majority of the results seemed to be LTR.
  • Google Scholar search. I tried a similar tactic with Google scholar, but it was a bit more difficult because graphs are hidden in PDFs. *Note: the place of origin are educated guesses based on the URL but may be wrong
    • Iran: example 1 example 2
    • Iraq: example 1 example 2
    • Israel: Most of the papers that came up were locked behind JSTOR, so I didn't bother with them. My friend sent me newspaper photos instead (from Haaretz):

downloads

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment