Skip to content

Labels of axis and legend are misaligned using superscript in expression #3216

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

Closed
codetrainee opened this issue Mar 29, 2019 · 7 comments
Closed

Comments

@codetrainee
Copy link
Contributor

library(tidyverse)

p <- 
  ggplot(ToothGrowth, 
            aes(supp, 
                len,
                color = supp)) + 
  geom_point()

p + 
  scale_x_discrete(label = c("engouh to overlap T7", bquote(T7^"-/-" ~ "test"))) +
  scale_color_discrete(label = c("T7", bquote(T7^"-/-" ~ "test"))) +
  theme(legend.position = "bottom")

Rplot09

The axis and legend texts are not aligned. The problem also occurred when using expression instead bquote. I tried scale_x_discrete(label = c(bquote("engouh to overlap" ~T7^" "), bquote(T7^"-/-" ~ "test"))) but with no luck.

Do you have any idea to align the text?

@karawoo
Copy link
Member

karawoo commented Mar 29, 2019

The axis text is aligned, just to the top rather than the bottom. The legend text is aligned in the center -- the superscript adds to the overall height of the text, so the top is higher and the bottom is lower than the other legend label. Turning on debugging makes it a little clearer what is going on:

I'm guessing you are looking for the bottom of the text to be in alignment. You can do this by setting vertical justification to 0. However then you will have legend labels that are misaligned with the legend keys, since they are no longer aligned to the center of the keys.

library(tidyverse)

p <- ggplot(ToothGrowth, aes(supp, len, color = supp)) + 
  geom_point()

p + 
  scale_x_discrete(label = c("engouh to overlap T7", bquote(T7^"-/-" ~ "test"))) +
  scale_color_discrete(label = c("T7", bquote(T7^"-/-" ~ "test"))) +
  theme(
    legend.position = "bottom",
    axis.text.x = element_text(vjust = 0, debug = TRUE),
    legend.text = element_text(vjust = 0)
  )

Created on 2019-03-29 by the reprex package (v0.2.1)
If you need more guidance on customizing the appearance of plots, you might try asking on https://community.rstudio.com/.

@codetrainee
Copy link
Contributor Author

codetrainee commented Mar 30, 2019

Thanks for your help.

The vjust can just solve the problem when there is a superscript or subscript in only one axis label. However, if we mix the two kinds of expression in different label position. vjust won't help.

library(tidyverse)

p <- ggplot(ToothGrowth, aes(supp, len, color = supp)) + 
  geom_point() +
  scale_x_discrete(label = c(bquote(T7["-/-"]), bquote(T7^"-/-"))) 
 
p + theme(
  axis.text.x = element_text(vjust = 0.5, debug = TRUE)
)

Created on 2019-03-30 by the reprex package (v0.2.1)

My ideal scenario is that all texts excluding the superscripts and subscripts can be aligned to the center themselves (for the legend, they can be aligned to the key center as well). For example, we only focus on the alignment of the part of T7 even though the expression is bquote(T7["-/-"]).

I know it is quite hard but was still wondering if there is any possibility that ggplot2 can achieve this.

@yutannihilation
Copy link
Member

all texts excluding the superscripts and subscripts can be aligned to the center themselves

I understand it's nice if we can do this, but I'm afraid grid doesn't provide such options.

@codetrainee
Copy link
Contributor Author

codetrainee commented Mar 30, 2019

I've got a solution from Joel in Rstudio community and here is the reply:

There may be a less hacky way, but one option is to create a phantom superscript in the first label and
a phantom subscript in the second. The phantom function creates a gap where a given string would
be, but doesn't draw the string. As you can see in the image, "T7" is now in the same vertical position
in each label. The theme statement is not necessary for vertical alignment.

library(tidyverse)

ggplot(ToothGrowth, aes(supp, len, color = supp)) + 
  geom_point() +
  scale_x_discrete(label = c(bquote(T7["-/-"]^phantom("/")), 
                             bquote(T7[phantom("/")]^"-/-"))) +
  theme(axis.text.x = element_text(vjust = 0.5, debug = TRUE))

Created on 2019-03-31 by the reprex package (v0.2.1)

Inspired by this idea, I am thinking if we can create a small function that insert phantom("/")[phantom("/")]^phantom("/") ~ to all text labels so they can have the superscripts and subscripts at the same time.

Although it is an ugly workaround, we circumvent the limitations that grid has.

library(tidyverse)

create_label <- function(x) {
  insert_label <- "phantom('/')[phantom('/')]^phantom('/')"
  
  new_label <- vector("list", length(raw_label))
  
  for (i in seq_along(x)) {
    new_label[[i]] <- paste(insert_label, deparse(x[[i]]), sep = "~")
  }
  
  label <- sapply(new_label, function(x) parse(text = x))
}

p <- ggplot(ToothGrowth, aes(supp, len, color = supp)) + 
  geom_point()

# This can be extracted from ggplot object, but to make it clear, I manually created one. 
raw_label <- c(parse(text = 'T7["-/-"]'), 
               parse(text = 'T7^"-/-"'))
label <- create_label(raw_label)

p +  
  scale_x_discrete(label = label) +
  theme(axis.text.x = element_text(vjust = 0.5, debug = TRUE))

Created on 2019-03-31 by the reprex package (v0.2.1)

@thomasp85
Copy link
Member

I think this is a nice workaround, but not something that should be build into ggplot2... you could consider submitting it to one of the general-purpose extension packages such as ggforce or ggalt

@paleolimbot
Copy link
Member

This is something that will be possible to extend more readily after #3322 and #3329.

@lock
Copy link

lock bot commented Nov 17, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Nov 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants