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

Labelled contour lines #27

Closed
AllanCameron opened this issue Dec 7, 2021 · 8 comments
Closed

Labelled contour lines #27

AllanCameron opened this issue Dec 7, 2021 · 8 comments

Comments

@AllanCameron
Copy link
Owner

One of the things I had hoped this package would handle would be labelled contour lines, where there are breaks in the lines for the labels. This is something that comes up now and again on SO, and none of the existing solutions seem that great.

I was about to explore how we might implement this, when I realized that...we already have:

library(geomtextpath)
#> Loading required package: ggplot2

set.seed(1)

df  <- data.frame(x = rnorm(100), y = rnorm(100))

ggplot(df, aes(x, y)) + 
  stat_density2d(geom = "textpath", aes(label = after_stat(level))) +
  theme_classic()

I think we want to include an example like this in our readme and gallery, but the issue is: is this useful enough to warrant its own geom? It would be very little work to create since it's essentially already implemented.

Created on 2021-12-07 by the reprex package (v2.0.0)

@teunbrand
Copy link
Collaborator

teunbrand commented Dec 7, 2021

when I realized that...we already have

Haha, that made me chuckle!
Is there anything that we could add beyond simply the convenience? For example with your reprex, would we want to offer the option to label only one of the outer paths that are broken up by the plot limits (personally I think it is 'correct' to label them all)? Are you considering stat_density2d() as well as stat_contour()?

On a note that just occurred to me looking at the plot, do we want to make the 'padding'/'breathing room' adjustable?

@AllanCameron
Copy link
Owner Author

AllanCameron commented Dec 7, 2021

I thought we had made padding adjustable, but I see now that we haven't. It's certainly a parameter within some of the internal functions, so it should be easy enough to expose it to end-users.

I think the labelling looks good as-is. The only thought that occurs is that you might want the label appearing more than once on the path. This ties in with something you mentioned before about having repeated labels along a path. This would be a nice additional feature, but it would be a bit of a headache to implement, requiring an overhaul of .get_surrounding_lines and .get_path_points

The examples I have added to the readme demonstrate both stat_contour and stat_density2d (which actually calls stat_contour internally after it has called kde2d).

I was thinking more of a convenience wrapper geom, but not sure how necessary that is.

@teunbrand
Copy link
Collaborator

Well the repeated label thing I had I mind would be as easy as:

label <- "Repeat me!"
before_measurement <- paste0(rep(label, 12), collapse = " ")

Wherein the 12 should be replaced by a number large enough to cover the path, but I can also see the merit of having a more limited number of repeats. I agree it would be a headache to implement, so we could avoid doing it until more use-cases would require it.

A convenience wrapper sounds nice if you're motivated to make one (but no pressure, it is not an essential feature, I think). If you're looking for some inspiration, the metR package also has some contour labelling options.

@teunbrand teunbrand mentioned this issue Dec 7, 2021
@AllanCameron
Copy link
Owner Author

AllanCameron commented Dec 9, 2021

I have now exposed the padding argument in geom_labelcontour which definitely improves the look. I will implement the 2d density next. It was a little more complicated than I thought to wrap the labelled contours in their own geom, since getting the labels passed through required a new stat as well.

The main feature that metR seems to offer is that it places the label on the flattest part of the curve. I'm not sure how big a difference this makes, but if there was an algorithm to do that for general paths and not just contours, we might be able to have a (?default) "auto" setting on the hjust in the that tries to put the labels in a nice spot. I'm not sure how hard this will be, but again the curvature might help us here.

@teunbrand
Copy link
Collaborator

teunbrand commented Dec 9, 2021

Isn't searching for the flattest part of a curve equivalent to finding the angles that are closest to zero or multiples of 2*pi? If you want to get more fancy, you can search in a textwidth-sized window for the smallest average absolute angle. It could be nice to have this for regular geom_textpath() as well. Probably .anchor_points() is the appropriate place for such logic.

@AllanCameron
Copy link
Owner Author

It could be nice to have this for regular geom_textpath() as well.

Yes, that's what I was thinking - perhaps allowing an "auto" hjust to be interpreted as "place the text on the flattest part of the path". Though, having said that, it seems a shame to write a package for curved text only to have it default to flat text!

@teunbrand
Copy link
Collaborator

teunbrand commented Dec 10, 2021

a shame to write a package for curved text only to have it default to flat text

Well we can just make hjust = "auto" a default in the contour plots, but not in the textpath plots. Its probably just for legibility. In textpath plots it would just be an option to search for the flattest part and default to hjust = 0.5.

@AllanCameron
Copy link
Owner Author

I have implemented geom_labeldensity2d in the latest commit. I know we haven't addressed the "flattest point" issue, but I feel that this is a separate issue, so I will close this "Labelled contour lines" issue for now.

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

2 participants