-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
add "seed" argument to request specific seed #33
Comments
In fact, I can make the change myself if this something you are amenable to and you add me as a contributor to the project. |
Thanks for opening this issue and describing the problem! I would be very happy to review a pull request with this feature. |
Call `set.seed(rnorm(1))` within `geom_text_repel()` and `geom_label_repel()` to allow recreating identical plots. Fixes issue #33.
@erikor Sorry for the long delay on this issue. I fixed it in commit 0b0a7ab I wrote some code to go through the scenario you described: library(ggrepel)
library(gridExtra)
dat1 <- mtcars[seq(1, nrow(mtcars), 4),]
set.seed(42)
p1 <- ggplot(dat1) +
geom_point(aes(wt, mpg), color = 'grey', size = 4, shape = 16) +
geom_text_repel(
aes(
wt, mpg,
color = factor(cyl),
label = rownames(dat1),
angle = -10
),
size = 5
) +
scale_color_discrete(name = 'cyl') +
theme_classic(base_size = 16)
dat2 <- mtcars[seq(2, nrow(mtcars), 4),]
set.seed(42)
p2 <- ggplot(dat2) +
geom_point(aes(wt, mpg), color = 'grey', size = 4, shape = 15) +
geom_label_repel(
aes(
wt, mpg,
color = factor(cyl),
label = rownames(dat2)
),
size = 5
) +
scale_color_discrete(name = 'cyl') +
theme_classic(base_size = 16)
grid.arrange(p1, p2, ncol = 2) Now let's drop the "Pontiac Firebird" point and plot again: set.seed(42)
dat3 <- dat1[rownames(dat1) != "Pontiac Firebird",]
p1 <- ggplot(dat3) +
geom_point(aes(wt, mpg), color = 'grey', size = 4, shape = 16) +
geom_text_repel(
aes(
wt, mpg,
color = factor(cyl),
label = rownames(dat3),
angle = -10
),
size = 5
) +
scale_color_discrete(name = 'cyl') +
theme_classic(base_size = 16)
grid.arrange(p1, p2, ncol = 2) The plots are identical, except for the missing "Pontiac Firebird". |
Great! Thank you. |
@slowkow This does not seem to work with cowplot function plot_grid. Are you guys getting consistent label positions with plot_grid just as in grid_arrange? I tried set.seed also before doing ggsave but one label keeps changing position. |
Please consider sharing a reproducible example. https://reprex.tidyverse.org/articles/articles/learn-reprex.html |
If I call set.seed() prior to using geom_text_repel, I get the same layout every time as I would expect.
However, if I am using grid.arrange() (from package gridExtras) to render multiple ggplots that include geom_text_repel, then if I change the parameters (such as nudge and force) in one plot, it may result in a different layout for the other plot(s). I assume this is because rendering the first plot may take a different number of iterations depending on the settings, resulting in the RNG starting in a different place when it is time to render the next plot.
For example, if I do this:
grid.arrange(p1, p2, ncol = 2)
Then changing the parameters in p1 changes the layout in p2.
This is a problem because sometimes I want a particular seed value because it produces a "better" (as subjectively determined by the beholder) layout.
This would be avoided if I could specify the seed value to geom_text_repel directly as an argument.
The text was updated successfully, but these errors were encountered: