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

Should label placers be evaluated at drawing time? #27

Open
eliocamp opened this issue Sep 4, 2021 · 1 comment
Open

Should label placers be evaluated at drawing time? #27

eliocamp opened this issue Sep 4, 2021 · 1 comment

Comments

@eliocamp
Copy link
Contributor

eliocamp commented Sep 4, 2021

The placer function computes the angle of the text, so that it follows the isoline, but since it's computed when creating the isoline grob and using the raw x and y values, it doesn't work for arbitrary aspect ratios and / or when changing aspect ratio.

From the example:

library(isoband)
library(grid)

x <- (0:(ncol(volcano) - 1))/(ncol(volcano) - 1)
y <- ((nrow(volcano) - 1):0)/(nrow(volcano) - 1)
lines <- isolines(x, y, volcano, 5*(19:38))



g <- isolines_grob(
    lines, breaks = 20*(5:10),
    label_placer = label_placer_middle(),
    gp = gpar(
        fontsize = 10,
        lwd = c(1, 2, 1, 1),
        col = c("grey50", "grey20", "grey50", "grey50")
    )
)

grid.newpage()
grid.draw(g)

I get

Notice that, for example, the 140 text doesn't perfectly follow it's isoline.

Furthermore, the angles remain constant if I change the aspect ratio of the graphics device.

In metR geom_text_contour() evaluates the angle at draw time, so the angle of the text changes as the aspect ratio changes.

library(ggplot2)
ggplot(reshape2::melt(volcano), aes(Var1, Var2)) +
    geom_contour(aes(z = value)) +
    metR::geom_text_contour(aes(z = value),
                            label.placement = label_placement_fraction())

@eliocamp
Copy link
Contributor Author

eliocamp commented Sep 4, 2021

While #28 solves the issue of changing aspect ratio, I think there are other issues with not computing placement at draw time. One direct application of selecting placement at draw time is labelling on the "flattest" segment. I do something like that with metR::label_placement_flattest, which places labels the segment with minimal curvature. I could be wrong (i haven't done the math or anything) but it seems to be that the least "curvy" segment will change depending on aspect ratio.

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