Skip to content
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

Plotmath and expressions #25

Closed
AllanCameron opened this issue Dec 2, 2021 · 2 comments
Closed

Plotmath and expressions #25

AllanCameron opened this issue Dec 2, 2021 · 2 comments

Comments

@AllanCameron
Copy link
Owner

AllanCameron commented Dec 2, 2021

One of the things users will presumably want to do is to have expressions as textpaths. This isn't currently handled. A textGrob can take expressions, hence geom_text can parse text into expressions and pass them on directly to textGrob. However, the rendering of expressions in textGrob happens via C code in plotmath.c, meaning that parsed expressions produce a single object that cannot be broken up and curved in the way that a text string can.

There are a few ways to handle this

  1. Just don't support expressions, and advise folks to use Unicode if they want subscripts, superscripts etc.
  2. Allow expressions, but accept that they need to be straight and have them sitting at the most appropriate angle on the path.
  3. Figure out a way of allowing curved expressions with a completely different mechanism to the one we use already. One method that comes to mind here is to convert the expression to svg (which is easily done), then convert the svg to a bunch of lines and symbols (difficult / complex), then curve them (moderately difficult).
    However, if we were going to go to the bother of doing this, the mechanism would also work for plain text, and we would be as well changing the entire implementation, which I really don't fancy doing.
@teunbrand teunbrand mentioned this issue Dec 2, 2021
@AllanCameron
Copy link
Owner Author

AllanCameron commented Dec 3, 2021

I have started implementing this. My approach was to keep the labels flat but angled rather than trying to curve them. Expressions need to be handled differently from text, but I thought the best thing to do was to try to create the same output structure that measure_text produces. This can't be done exactly, since you can't easily fit multiple expressions in a data frame column. However, we can do it in a list, so that mostly the same accessors and subsetters still work.

When we implement the "dumb" textpath, we should probably make every expression "dumb", though the placement is good on smooth curves at present.

Current behaviour is:

library(geomtextpath)
#> Loading required package: ggplot2

lab <- expression(paste("y = ", frac(1, sigma*sqrt(2*pi)), " ",
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)}))

df <- data.frame(x = seq(-2, 0, len = 100),
                 y = dnorm(seq(-2, 0, len = 100)),
                 z = as.character(lab))

p <- ggplot(df, aes(x, y, label = z))

p + geom_textpath(vjust = -0.2, hjust = 0.1, size = 8, parse = TRUE)

and

ggplot() + xlim(0, 1) + 
  stat_function(fun = \(x) x, label = "paste('y = ', x)",
                geom = "textpath", vjust = -0.2, parse = TRUE) +
  stat_function(fun = \(x) x^2, label = "paste('y = ', x^2)",
                geom = "textpath", vjust = -0.2, parse = TRUE) +
  stat_function(fun = \(x) x^3, label = "paste('y = ', x^3)",
                geom = "textpath", vjust = -0.2, parse = TRUE)

image

@AllanCameron
Copy link
Owner Author

The latest commit allows multiple parsed expressions and fixes some line spacing problems with the above implementation, so plotmath should have the same functionality as text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant