-
-
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
How to extract labels shown by ggrepel #254
Comments
Sorry, but I don't know how to help. My understanding is that ggrepel should only be creating a grob for labels that are being plotted, but it should not be creating grobs for unplotted labels. If that's not happening, then maybe the plot is not being rendered appropriately? I don't know. If anyone eventually finds something that works, I'd appreciate if you share it here so everyone can learn. |
Thanks for the reply! I got it working! It seems that the main problem was incorrect units in the library(tidyverse)
library(ggrepel)
library(grid)
#dev.size()
create_data <- function(n=10) {
x <- runif(n)
y <- runif(n)
label <- paste0("***",1:n,"***")
df <- tibble(x=x, y=y, label=label)
df
}
set.seed(123)
tmp <- create_data(n=500)
g <- tmp %>% ggplot(aes(x, y, label=label)) + geom_text_repel()
extract_labels <- function(g, width=9.8, height=7) {
force(g)
# Trying to imitate solution to this https://github.com/slowkow/ggrepel/issues/24
grid.newpage()
#vp <- viewport(width = width, height = height) # Wrong units!!!
vp <- viewport(width = unit(width, "in"), height = unit(height, "in"))
pushViewport(vp)
gg <- g %>% ggplotGrob() %>% grid.force()
print(g, vp=vp) # Make the plot appear
repeltree <- gg %>% getGrob("textrepeltree", grep = TRUE)
popViewport()
repeltree
}
res <- extract_labels(g, 5, 5)
n_distinct(res$data$label) # all 500 are here
length(res$children) # now this prints 20!!!! |
This is great! Thank you so much for sharing 🙏 I think this might help others who are trying to use the outputs from ggrepel. |
Hi,
How can I extract a list of those labels that ggrepel shows in the result figure?
In the below example I create 500 points, and ggrepel shows label for 395 of these points only. However, when I
try to extract the list of these 395 labels, I get all 500 labels. Any idea what I am doing wrong?
Here is an image of the output produced by the code:
The text was updated successfully, but these errors were encountered: