Skip to content

expressions as tick label broken #6638

@RCinbo

Description

@RCinbo

This issue was first posted as a reply to this issue. I was asked to make a new issue.
This code worked perfectly a while ago.

library(ggplot2)
library(latex2exp)
ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() +
    scale_x_continuous(breaks = 3, labels = TeX("$\\alpha$"))

yields

Error in vec_size():
! x must be a vector, not a <latexexpression/expression> object.

In a previous issue on a similar problem for annotations in a ggplot, specifying output = characterin the TeX function and parse = TRUE in the annotate function works. However, this cannot solve the problem at hand here:

ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() +
    scale_x_continuous(breaks = 3, labels = TeX("$\\alpha$", output = character), parse = TRUE)

yields:

Error in scale_x_continuous(breaks = 3, labels = TeX("$\alpha$", output = character), :
unused argument (parse = TRUE)

I found a workaround since labelling tick marks with a vector still seemed to work. I add a second label to a tick that I do not show on the graph by setting axis limits:

ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() +
    scale_x_continuous(breaks = c(0, 3),
                       labels = TeX(c("",  "$\\alpha$")),
                       limits = c(1.5, 5.5))

Another workaround, as suggested by @yutannihilation is using unclass or use parse(text =) instead of latex2exp.

ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() +
    scale_x_continuous(breaks = 3, labels = unclass(latex2exp::TeX("$\\alpha$")))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions