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

geom_label_repel(..., point.padding = NA) throws error: "'x' argument must be a unit object" #104

Closed
mlell opened this issue May 3, 2018 · 1 comment
Labels

Comments

@mlell
Copy link

mlell commented May 3, 2018

Summary

geom_text_repel(..., point.padding = NA) can be used to avoid text being repelled by other data points. I need this feature because I want to use ggrepel to annotate inside a bar plot, so the text must not be repelled by the bars.

However, if I use geom_label_repel instead in this way, the following error is produced:

Error in convertUnit(x, unitTo, "x", "dimension", "x", "dimension", valueOnly = valueOnly) :
'x' argument must be a unit object

Since Issue #79 / PR #80 it is possible to use

Minimal code example

Here is the minimum amount of code neeeded to demonstrate the issue:

library(ggplot2)
library(ggrepel)
library(dplyr)
t <- tibble(Env = rep(LETTERS[1:3], c(4,3,2)), 
            nRpl = c(4:1, 3:1, 2:1),
            nGt = c(1, 2, 4, 50, 1, 2, 10, 5, 7))
t <- t %>% mutate(nRpl = as.factor(nRpl))
t <- t %>% group_by(Env) %>% mutate(lPos = cumsum(nGt)-nGt/2) # label positions
ggplot(t, aes(x = Env, y = nGt, fill = nRpl)) + geom_col() + 
     geom_text_repel(aes(y = lPos, label = nRpl), point.padding = NA)

Here is an image of the output produced by the code:

image

However, this does not work with geom_label_repel:

ggplot(t, aes(x = Env, y = nGt, fill = nRpl)) + geom_col() + 
       geom_label_repel(aes(y = lPos, label = nRpl), point.padding = NA)
## Error in convertUnit(x, unitTo, "x", "dimension", "x", "dimension", valueOnly = valueOnly) : 
##   'x' argument must be a unit object

Suggestions

It would be nice if geom_label_repel could also ignore other non-text geoms like is the case for geom_text_repel, for example if there are contrast issues between text and underlying graphics.

A similar topic was issue #79.

Version information

Here is the output from sessionInfo() in my R session:

R version 3.4.3 (2017-11-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS: /opt/Bio/R/3.4.3/lib64/R/lib/libRblas.so
LAPACK: /opt/Bio/R/3.4.3/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggrepel_0.7.0     prPretty_0.0      bindrcpp_0.2      viridis_0.4.1    
 [5] viridisLite_0.2.0 tidyr_0.7.2       ggplot2_2.2.1     glue_1.2.0       
 [9] stringr_1.2.0     readxl_1.0.0      magrittr_1.5      dplyr_0.7.4      

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15       RColorBrewer_1.1-2 cellranger_1.1.0  
 [4] pillar_1.1.0       compiler_3.4.3     plyr_1.8.4        
 [7] bindr_0.1          tools_3.4.3        digest_0.6.14     
[10] memoise_1.1.0      tibble_1.4.2       gtable_0.2.0      
[13] pkgconfig_2.0.1    rlang_0.1.6        cli_1.0.0         
[16] rstudioapi_0.7     commonmark_1.4     yaml_2.1.16       
[19] gridExtra_2.3      xml2_1.2.0         withr_2.1.1       
[22] knitr_1.18         roxygen2_6.0.1     devtools_1.13.4   
[25] rprojroot_1.3-2    grid_3.4.3         R6_2.2.2          
[28] purrr_0.2.4        backports_1.1.2    scales_0.5.0      
[31] assertthat_0.2.0   colorspace_1.3-2   labeling_0.3      
[34] utf8_1.1.3         stringi_1.1.6      lazyeval_0.2.1    
[37] munsell_0.4.3      crayon_1.3.4   
@slowkow slowkow added the bug label May 3, 2018
@slowkow slowkow closed this as completed in 7ce55c0 May 3, 2018
@slowkow
Copy link
Owner

slowkow commented May 3, 2018

Thanks for reporting this issue! Also, thank you so much for providing a runnable example!! 👍

It was a bug in my code, but it should be fixed now.

Here's an example with the development version of ggrepel:

library(ggplot2)
library(ggrepel)
library(dplyr)

t <- tibble(Env = rep(LETTERS[1:3], c(4,3,2)),
            nRpl = c(4:1, 3:1, 2:1),
            nGt = c(1, 2, 4, 50, 1, 2, 10, 5, 7))
t <- t %>% mutate(nRpl = as.factor(nRpl))
t <- t %>% group_by(Env) %>% mutate(lPos = cumsum(nGt)-nGt/2) # label positions

p <- ggplot(t, aes(x = Env, y = nGt, fill = nRpl)) + geom_col()

# Works
# p + geom_text_repel(aes(y = lPos, label = nRpl), point.padding = NA)

# Should work now!
p + geom_label_repel(aes(y = lPos, label = nRpl), point.padding = NA)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants