-
-
Notifications
You must be signed in to change notification settings - Fork 96
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
Multiple geom_text_repel give overlapping labels #153
Comments
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 How it might work, step by step:
That way, each call to 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. |
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 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 |
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
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:
The text was updated successfully, but these errors were encountered: