-
Notifications
You must be signed in to change notification settings - Fork 24
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
Allowing text from points in geom_textpath
#23
Comments
Yes I think it very much makes sense to do this. We'd have to make it clear in the docs, readme and possibly introduction vignette that this is the behaviour to be expected. But this raises the question whether point-textpath (sounds like a paradox), should support an I'm also curious how the small path would be calculated, because if you draw it too long, the |
Yes, this code is inside the At the moment I am calculating the little path from the string width (taken from shape_string). It needs to be at least as long as the text string, otherwise the curvature doesn't work. I have also changed all linetypes to 0, since we assume that in this case the user doesn't need to know about the path. You're right that this makes
Personally, I quite like number 2. It wouldn't be difficult to implement angle aesthetic for the "point textpaths", and I actually quite like the idea. However, I agree that it doesn't make sense for "path textpaths". Since we are already detecting which of the two is intended by the user, I don't see a problem with implementing it only for point-textpaths. We should be able to warn users that angle will be ignored for "path textpaths", but only where angle is specifically passed as a parameter. |
For the point-texpaths that makes perfect sense.
I'm not saying this won't work, but from a theoretical perspective, this returns metrics in pixels, which at
That would probably be the nicest for users, I've sketched a small function for this (y-axis == hjust). x <- c(1,1,1)
y <- c(0, 0.5, 1)
small_path <- function(x, y, angle, width, hjust) {
angle <- angle / 180 * pi
xmin <- x + cos(angle + pi) * width * hjust
xmax <- x + cos(angle) * width * (1 - hjust)
ymin <- y + sin(angle + pi) * width * hjust
ymax <- y + sin(angle) * width * (1 - hjust)
data.frame(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)
}
path <- small_path(x, y, 0, 2, hjust = y)
plot(x, y, xlim = c(-1, 3), ylim = c(-0.5, 1.5))
segments(path$xmin, path$ymin, path$xmax, path$ymax) # Change angle
path <- small_path(x, y, 45, 2, hjust = c(0, 0.5, 1))
plot(x, y, xlim = c(-1, 3), ylim = c(-0.5, 1.5))
segments(path$xmin, path$ymin, path$xmax, path$ymax) Created on 2021-11-30 by the reprex package (v2.0.1) |
Actually, I don't think it's possible to hack the hjust from within |
I'll push the commit anyway, since it seems to be a desirable feature, and we can work out how (or if) we handle hjust with "point textpaths" once we have had more of a play with it. If we feel the hjust is vital we can consider handling it inside |
I have implemented the angles for point-like textpaths inside Anyway, the current behaviour is this: library(geomtextpath)
#> Loading required package: ggplot2
pie <- data.frame(values = c(5, 4, 2.5),
mids = c(2.5, 7, 10.25),
fruit = c("banana", "orange", "apple"),
colour = c("yellow", "orange", "green"),
x = 1)
p <- ggplot(pie, aes(x = x, y = mids, label = fruit)) +
geom_col(aes(x = x, y = values, fill = colour)) +
scale_fill_identity() +
theme_void()
p_00 <- p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 0)
p_90 <- p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 90)
p_45 <- p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 45)
p_00 p_00 + coord_polar(theta = "y") p_90 p_90 + coord_polar(theta = "y") p_45 p_45 + coord_polar(theta = "y") Created on 2021-11-30 by the reprex package (v2.0.0) |
Right. I think I've cracked this. I wasn't happy with the little paths and angles being outside of All that aside, I'm very pleased with the results: library(geomtextpath)
#> Loading required package: ggplot2
pie <- data.frame(values = c(5, 4, 2.5),
mids = c(2.5, 7, 10.25),
fruit = c("banana", "orange", "apple"),
colour = c("yellow", "orange", "green"),
x = 1)
p <- ggplot(pie, aes(x = x, y = mids, label = fruit)) +
geom_col(aes(x = x, y = values, fill = colour)) +
geom_point() +
scale_fill_identity() +
theme_void()
p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 0, hjust = 0) p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 0, hjust = 1) p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 0, hjust = 1) +
coord_polar(theta = "y") p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 45, hjust = 0) p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 45, hjust = 1) p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 45, hjust = 1) +
coord_polar(theta = "y") p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 90, hjust = 0) p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 90, hjust = 1) p + geom_textpath(size = 8, flip_inverted = TRUE, angle = 90, hjust = 1) +
coord_polar(theta = "y") Created on 2021-11-30 by the reprex package (v2.0.0) |
At the moment, we need to specify a path for each label in
geom_textpath
, but it would make the geom easier to use if, instead of emitting a warning when each group only consists of a single observation, we interpret this as a request by the user to have a label at a single point. If we construct a little flat path for the label to sit on that is the same size as the text itself, it should behave much the same asgeom_text
, except curve in polar co-ordinates. This is much the same as what I was proposing in #5 except we don't need a separate geom to do it.Since the calculation would only happen in the situation where otherwise the geom would fail, it doesn't add any overhead to the existing use cases.
Essentially , the behaviour would be like this:
p_textpath
Created on 2021-11-30 by the reprex package (v2.0.0)
Does this fit with our conception of how the geom should behave? It means that under the correct circumstances
geom_textpath
could act as either exactly likegeom_text
, or exactly likegeom_path
, but usually some handy mixture of the two. I like this, but wanted to find out if there were any objections or something I'm missing before committing.The text was updated successfully, but these errors were encountered: