-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Comments
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) |
Thanks for your help. The 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 I know it is quite hard but was still wondering if there is any possibility that ggplot2 can achieve this. |
I understand it's nice if we can do this, but I'm afraid grid doesn't provide such options. |
I've got a solution from Joel in Rstudio community and here is the reply:
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 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) |
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 |
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/ |
The axis and legend texts are not aligned. The problem also occurred when using
expression
insteadbquote
. I triedscale_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?
The text was updated successfully, but these errors were encountered: