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

Label limits #67

Merged
merged 7 commits into from Jan 9, 2017
Merged

Label limits #67

merged 7 commits into from Jan 9, 2017

Conversation

@lukauskas
Copy link
Contributor

@lukauskas lukauskas commented Jan 6, 2017

Implements custom limits for label limits as discussed in #66 .
Takes the parameters limits.x and limits.y to be a vector with two elements, the first one being the lower limit, and the other one being the higher one.
The vectors are in data coordinates.
NAs in the vector are replaced to the image lower or upper bounds respectively.

Usage example:

library(ggrepel)
set.seed(42)
data <- mtcars
mu <- mean(data$wt)

left <- data[data$wt < mu,]
right <- data[data$wt >= mu,]

ggplot(data) +
  geom_vline(aes(xintercept=mu)) +
  geom_point(aes(wt, mpg), color = 'red') +
  geom_text_repel(aes(wt, mpg, label = rownames(left), colour='Left half'),
                  data=left,
                  limits.x=c(NA, mu)) +
  geom_text_repel(aes(wt, mpg, label = rownames(right), colour='Right half'),
                  data=right,
                  limits.x=c(mu, NA)) +
  theme_classic(base_size = 16)

ggsave('example.png')

Result:

example

@slowkow
Copy link
Owner

@slowkow slowkow commented Jan 9, 2017

This is great! I have a few requests before I merge this into the master branch:

  • To be consistent with ggplot2, use xlim and ylim instead of limits.x and limits.y. For example, xlim and ylim are used in ggplot2::coord_cartesian().

  • Both geom_text_repel and geom_label_repel should have this feature.

  • Add an example to vignettes/ggplot2.Rmd that demonstrates the usage of this feature.

Please paste this code into the vignettes/ggplot2.Rmd file. Try to describe your feature.

### Limit labels to a specific area

TODO: Write a 1-2 sentence description of what this feature is about.

```{r label-limits, echo=TRUE, fig.width=9, fig.height=7}
set.seed(42)
data <- mtcars
mu <- mean(data$wt)

left <- data[data$wt < mu,]
right <- data[data$wt >= mu,]

ggplot() +
  geom_vline(xintercept = mu) +
  geom_point(
    data = data,
    mapping = aes(wt, mpg)
  ) +
  geom_text_repel(
    data = left,
    mapping = aes(wt, mpg, label = rownames(left), colour = 'Left half'),
    limits.x = c(NA, mu)
  ) +
  geom_text_repel(
    data = right,
    mapping = aes(wt, mpg, label = rownames(right), colour = 'Right half'),
    limits.x = c(mu, NA)
  ) +
  theme_classic(base_size = 16)
```
@lukauskas
Copy link
Contributor Author

@lukauskas lukauskas commented Jan 9, 2017

Have a look now, I think this ticks off everything on your TODO list.

@slowkow slowkow merged commit 3269da9 into slowkow:master Jan 9, 2017
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@slowkow
Copy link
Owner

@slowkow slowkow commented Jan 9, 2017

Great work, thank you! I just made some minor changes and merged your commit. You're now a contributor 👍

@lukauskas
Copy link
Contributor Author

@lukauskas lukauskas commented Jan 9, 2017

Thanks!

And hehe, was not expecting to appear in the description. :)

zkamvar added a commit to zkamvar/ggrepel that referenced this pull request May 10, 2017
This originated with slowkow#67. The xlim and ylim did not
have defaults. This wasn't picked up in the package itself because these defaults were provided in the `geom_*_repel()` functions. If a function is provided that doesn't explicitly set xlim and ylim, the ggproto goes into an infinite loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

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