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

Smoothing bumpy text #11

Closed
AllanCameron opened this issue Nov 19, 2021 · 1 comment
Closed

Smoothing bumpy text #11

AllanCameron opened this issue Nov 19, 2021 · 1 comment

Comments

@AllanCameron
Copy link
Owner

Thought I'd better make a note somewhere about an attempt I made to add in a smoothing parameter. The idea was to "iron out" small bumps in the text by a simple loess of x co-ordinates and another of y co-ordinates. This produces a very nice effect in some cases. For example, with the trend lines. Compare without any smoothing:

library(geomtextpath)

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(alpha = 0.1) +
  geom_textpath(aes(label = Species, colour = Species),
                stat = "smooth", method = "loess", formula = y ~ x,
                size = 7, linetype = 3, fontface = 2, linewidth = 1) +
  scale_colour_manual(values = c("forestgreen", "deepskyblue4", "tomato4")) +
  coord_fixed(ratio = 1) +
  theme_bw()

image

versus with smoothing:

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(alpha = 0.1) +
  geom_textpath(aes(label = Species, colour = Species),
                stat = "smooth", method = "loess", formula = y ~ x,
                size = 7, linetype = 3, fontface = 2, linewidth = 1,
                smooth = 0.75) +
  scale_colour_manual(values = c("forestgreen", "deepskyblue4", "tomato4")) +
  coord_fixed(ratio = 1) +
  theme_bw()

image

To my eye this looks a lot nicer and is closer to publication standard. The problem is that it smooths the underlying path itself, so can't be used when the exact path is important (such as density curves), and often distorts the spacing of the text.

We can actually get the same result as the nice one above by simply calling geom_textpath with a span = 1, which, although we don't use as a parameter, gets passed to stat_smooth:

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(alpha = 0.1) +
  geom_textpath(aes(label = Species, colour = Species),
                stat = "smooth", method = "loess", formula = y ~ x,
                span = 1,
                size = 7, linetype = 3, fontface = 2, linewidth = 1) +
  scale_colour_manual(values = c("forestgreen", "deepskyblue4", "tomato4")) +
  coord_fixed(ratio = 1) +
  theme_bw()

image

So I'm not convinced that a smoothing feature is required. Perhaps a stat_textspline will be the route to smoothing paths, and this is on the to-do list.

@AllanCameron
Copy link
Owner Author

I will close this for now but we can point to this issue in the future if it comes up again.

AllanCameron pushed a commit that referenced this issue Dec 15, 2021
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