Implement new coord_mirror() and theme_rtl() for RtL plotting #2817
Conversation
I would expect a |
@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. |
I think all you need to do is add the following function to your 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. |
Great! Thanks for the quick code @clauswilke! It's implemented now and things actually mirror as you would expect without the 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 |
Another point: Maybe make 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 |
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
Online graph searchI found it surprisingly difficult to search for graphs in RTL languages, but I hit on a few methods that seem to work ok:
|
I'm with @isteves on this one. The mathematical convention of left to right usually remains even when the language is RTL (at least from my perspective as a native Hebrew speaker). The only real thing that is truly needed in terms of RTL charts is not actually covered in this pull request. This is the direction of text. I.e., if you have punctuation marks in the text they will appear at the wrong side. |
@adisarid This is of interest to me, as I'm planning to improve text handling in ggplot2. How does right-to-left text usually work? Is it reasonable to assume that layouting works just like left-to-right only in the other direction, or are there additional issues? And what are the conventions for mixed text? It seems to get complicated pretty quickly. |
@clauswilke - good question. To implement this in For mixed text, e.g. English with Hebrew/Arabic. It's indeed more complicated, because you have to guess when an English word is meant to end a Hebrew sentence or start a new English sentence. Makes sense? |
One issue in these languages ( at least in arabic) is that you might have separate rules for numerals: |
@dpseidel what is the status of this PR? |
It's functional and pretty close to finished, just needs a few visual tests. I agree it could be nice to make it an incomplete theme as suggested by @clauswilke. It begin as simple curiosity during my internship. Ultimately it stalled because it wasn't clear if there was consensus on exactly how and when RtL plotting is used in the wild and thus, how it should be it should be implemented in ggplot2, if at all. @hadley, what do you think? Is this a feature you'd still like to see in ggplot2? If we decide it's worth considering further I'm happy to finish it off either in time for 3.2 or for a release down the road a bit. |
I think it's worth finishing off by making it a partial theme, and removing the x-axis mirroring. |
Alright, I have reverted the x-axis mirroring, converted |
We are closing this. It appears that only the titles should be right-aligned in an rtf theme which is so easy to change that the addition of a new theme seems overkill. Work on supporting correctly rendered non-western scripts (e.g. Arabic, Urdu, Hebraic) continues in ragg and textshaper |
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 newcoord_mirror()
and then added a newtheme_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:@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.