Skip to content
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

Closed
erikor opened this issue Apr 13, 2016 · 4 comments
Closed

add "seed" argument to request specific seed #33

erikor opened this issue Apr 13, 2016 · 4 comments
Labels

Comments

@erikor
Copy link

@erikor erikor commented Apr 13, 2016

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.

@erikor
Copy link
Author

@erikor erikor commented Apr 14, 2016

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.

@slowkow
Copy link
Owner

@slowkow slowkow commented Apr 14, 2016

Thanks for opening this issue and describing the problem! I would be very happy to review a pull request with this feature.

@slowkow slowkow added the enhancement label Apr 14, 2016
slowkow added a commit that referenced this issue Oct 6, 2016
Call `set.seed(rnorm(1))` within `geom_text_repel()` and `geom_label_repel()`
to allow recreating identical plots. Fixes issue #33.
@slowkow
Copy link
Owner

@slowkow slowkow commented Oct 6, 2016

@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)

image

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)

image

The plots are identical, except for the missing "Pontiac Firebird".

@slowkow slowkow closed this Oct 6, 2016
@erikor
Copy link
Author

@erikor erikor commented Mar 16, 2017

Great! Thank you.

slowkow added a commit that referenced this issue Jul 17, 2017
Addresses issues #33 and #73

Now users can choose to make plots reproducible if they wish.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.