Open dariober opened 4 years ago
Thanks for opening the issue, sharing code, and sharing a figure.
I would like for ggplot2 to allow each layer to depend on previous layers, but this is not currently supported.
Right now I can't think of a way to work around this issue, but maybe someone else has an idea?
+1 for this functionality.
+1 eagerly waiting as well...
A half-baked idea:
What if there's an option geom_text_repel(cascade = TRUE)
that tells ggrepel to write the x,y coordinates to a temporary file?
How it might work, step by step:
geom_text_repel()
creates a temporary file. ggrepel sees it is empty. It writes x,y coordinates to it.geom_text_repel()
reads the temporary file, and sees that it is not empty. So, it adds those x,y coordinates as additional points to repel away from. Then it appends its own x,y coordinates to the file.geom_text_repel()
reads the temporary file, and sees the x,y coords from the first two calls... etc.That way, each call to geom_text_repel()
cumulatively appends more coordinates to repel away from.
What do you think?
I think it's a bit weird to use a temporary file for this, but I don't know how else to get the ggplot2 layers to "see" each other in a cascading way (kinda like CSS for HTML).
I don't think that ggrepel would have any way to know when the cascading is completed, so the temporary files would not be deleted at the end of the ggrepel calls. Instead, they would continue accumulating indefinitely with additional calls to ggrepel.
I'm not knowledgeable enough to know whether the cascade idea would work. But are there any new solutions to this? (+1) Right now I'm just cycling through seeds until randomness provides a decent result.
Not sure how much you're in contact with / part of the ggplot2 team, but I could imagine "layers being aware of each other" might be something they were or are thinking about themselfes.
It seems that the ggplot2 Google Group is not the best place to discuss this feature: https://groups.google.com/g/ggplot2/c/OAw_SijqzVg
I created a new post for discussion here: https://community.rstudio.com/t/feature-discussion-layers-aware-of-each-other-in-ggplot2/108156
Feel free to share your ideas, here or on the RStudio Community page.
Pull requests that implement a new feature to address this issue are welcome!
atusy from RStudio Community shared an excellent code example that we could use to address this issue:
https://community.rstudio.com/t/feature-discussion-layers-aware-of-each-other-in-ggplot2/108156/2
@aphalo I thought you might find this example interesting for your own work — I was not aware of ggplot_add()
.
library(ggrepel)
library(patchwork)
geom_text_repel2 <- function(...) {
layer <- ggrepel::geom_text_repel(...)
layer$ggrepel <- TRUE
class(layer) <- c("ggrepel", class(layer))
return(layer)
}
ggplot_add.ggrepel <- function(object, plot, object_name) {
if (any(do.call(c, lapply(plot$layer, function(x) x$ggrepel)))) {
warning(
"There is more than one ggrepel layers. ",
"This may cause overlap of labels"
)
}
# Optionally, one may modify `object` here.
NextMethod("ggplot_add")
}
dat <- data.frame(x= 1:10, y= 1:10, text= LETTERS[1:10])
p1 <- ggplot(data= dat, aes(x= x, y= y, label= text)) +
geom_point() +
geom_text_repel2(colour = 'blue', seed = 1) +
labs(title = "one layer")
p2 <- p1 + geom_text_repel2(colour = 'red', seed = 2) +
labs(title = "two layers")
#> Warning in ggplot_add.ggrepel(object, p, objectname): There is more than one
#> ggrepel layers. This may cause overlap of labels.
p1 + p2
This might be related, though I don't remember the details...
Not quite the same issue, but it would be helpful to have an option to distribute the labels relatively evenly over the plot grid.
Many times there is one corner or cluster of the data with all of the text labels and 90% of the plot space has nothing.
Not quite what you are after, but the computed-nudge positions from package 'ggpp' can help spread labels more evenly. The closest to what you suggest is position_nudge_to()
with x.action="spread"
and/or "y.action="spread"
. Being positions that add nudging they are compatible with geom_text_repel()
and geom_label_repel()
. HTML help is available. There are also some examples in the package vignette and in the web page Nudging + repulsion with ‘ggrepel’ and ‘ggpp’.
Thanks, that is helpful. Have been doing things manually with a new variable that cuts 90% of labels out, but that doesn't seem optimal
In 'ggpp' there are also a couple of stats that can filter labels based on local density of observations: stat_dens1d_labels() and stat_dens1d_labels(). They are designed to work with geom_text_repel()
and geom_label_repel()
, they replace the character strings mapped to the label
aesthetics by ""
which the repulsive geoms skip without skipping the original location of the observation, so the other labels are still repulsed away from the obaervations that are not labelled.
If you do the filtering manually, keep all coordinates, but replace the labels to be skipped by ""
. This ensures that repulsion works for all observations.
geom_text_repel
andgeom_label_repel
do not "see" labels plotted by other calls of geom_*_repel resulting in overlapping labels.Here I plot the same text twice using
geom_text_repel
, once in blue and one in red (this is pointless of course, just for sake of example)The blue and red labels overlap each other:
In real life I'm using both
geom_text_repel
andgeom_label_repel
on the same plot and I get overlapping texts when the text from the two calls are close to each other.Any fix much appreciated! (Great package anyway!) Dario
Session info: