Navigation Menu

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

Wrong order of labels/text if parse=TRUE #114

Closed
pakidermo5000 opened this issue Aug 27, 2018 · 7 comments
Closed

Wrong order of labels/text if parse=TRUE #114

pakidermo5000 opened this issue Aug 27, 2018 · 7 comments
Assignees
Labels

Comments

@pakidermo5000
Copy link

pakidermo5000 commented Aug 27, 2018

Summary

If we have labels that require parsing to be displayed like greek letters (alpha, beta, etc). They are shown in the wrong place when the option parse=TRUE and ielse in the labels are used. Of course, if the option is not used, the parsing does not take place but the labels are shown in the correct position.

Minimal code example

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

Take the following data frame:

Define data

Name<-c("boron_nitride" , "Borane_tetrahydrofuran" ,"alpha~boron_carbide" ,"bicyclo_diboroxane", "hydrogen_bromide")
Origin<-c("Mylist", "Mylist" ,"List2", "List2", "List2")
similarity<-c(0.00952381, 0.04729730 ,0.02962963 ,0.03007519, 0.00000000)
XFinger<-c(125.6716, 109.0577, 121.3035,121.8249, 127.8916)
YFinger<-c(7.456731, 20.432524, 4.887834, 6.534948, 7.150559)

myData<-data.frame(Name,Origin,similarity,XFinger,YFinger)

Plot

ggplot(myData,aes(XFinger,YFinger)) +
geom_point()+
geom_label_repel(aes(label=ifelse(Origin=="List2",as.character(Name),"")), # I print only the labels I need
parse=TRUE,
show.legend=FALSE,
force=1)+

Labels

 xlab("PC1")+
 ylab("PC2")+
 ggtitle("Myplot")  

When parse=FALSE we get this image
image

When parse=TRUE we get this other image with the labels in the wrong position
image

Suggestions

I do not know how to solve the issue, but it is clear that parse=TRUE changes the order of the labels,

Version information

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

 

| >

Paste output here

R version 3.4.3 (2017-11-30) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1  Matrix products: default  locale: [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                            [5] LC_TIME=English_United Kingdom.1252      attached base packages: [1] stats     graphics  grDevices utils     datasets  methods   base       other attached packages:  [1] forcats_0.3.0   stringr_1.2.0   dplyr_0.7.4     purrr_0.2.4     readr_1.1.1     tidyr_0.8.0     tibble_1.4.2    tidyverse_1.2.1 ggrepel_0.7.0   ggplot2_2.2.1    loaded via a namespace (and not attached):  [1] Rcpp_0.12.15     cellranger_1.1.0 pillar_1.1.0     compiler_3.4.3   plyr_1.8.4       bindr_0.1        tools_3.4.3      digest_0.6.15    jsonlite_1.5     [10] lubridate_1.7.2  nlme_3.1-131.1   gtable_0.2.0     lattice_0.20-35  pkgconfig_2.0.1  rlang_0.2.1      psych_1.7.8      cli_1.0.0        rstudioapi_0.7   [19] parallel_3.4.3   haven_1.1.2      bindrcpp_0.2     xml2_1.2.0       httr_1.3.1       hms_0.4.2        grid_3.4.3       glue_1.2.0       R6_2.2.2         [28] readxl_1.1.0     foreign_0.8-69   modelr_0.1.2     reshape2_1.4.3   magrittr_1.5     scales_0.5.0     rvest_0.3.2      assertthat_0.2.0 mnormt_1.5-5     [37] colorspace_1.3-2 labeling_0.3     stringi_1.1.6    lazyeval_0.2.1   munsell_0.4.3    broom_0.4.3      crayon_1.3.4
--
 
> | >
>


@slowkow
Copy link
Owner

slowkow commented Aug 27, 2018

Thanks for reporting this bug! I reproduced the same error in ggrepel 0.8.0, the latest version on CRAN right now. I'll try to fix this soon, or else I'd be happy to accept a pull request.

I reduced your code to a shorter snippet:

library(ggrepel)
library(patchwork)

d <- data.frame(
  x = c(1, 2, 3),
  y = c(1, 0, 1),
  label = c("alpha", "", "gamma")
)

p <- ggplot(d, aes(x = x, y = y, label = label)) + geom_point()

p1 <- p + geom_label_repel(parse = TRUE) + ggtitle("parse = TRUE")

p2 <- p + geom_label_repel(parse = FALSE) + ggtitle("parse = FALSE")

p1 + p2

image

As you described, the labels are correct when parse = FALSE, but they are not correct when parse = TRUE.

@slowkow slowkow added the bug label Aug 27, 2018
@slowkow slowkow self-assigned this Aug 27, 2018
@slowkow
Copy link
Owner

slowkow commented Aug 27, 2018

The parse() function does not behave the way I expected!

> x <- c("alpha", "", "gamma")
> parse(text = x)
expression(alpha, gamma)

It just deletes the empty string:

> length(x)
[1] 3
> length(parse(text = x))
[1] 2

I'm not sure how to fix this! If you have any suggestions, please let me know.

@slowkow
Copy link
Owner

slowkow commented Aug 27, 2018

There is a workaround. You can create a second dataframe for the text labels.

Here is an example:

library(ggrepel)
library(patchwork)

d <- data.frame(
  x = c(1, 2, 3),
  y = c(1, 0, 1),
  label = c("alpha", "", "gamma")
)

p <- ggplot(d, aes(x = x, y = y, label = label)) + geom_point()

# Here we create a new dataframe that only includes the labeled points.
d2 <- d[d$label != "",]
p3 <- p + geom_label_repel(data = d2, parse = TRUE)

image

I'm still looking for a way to fix this issue without creating the second dataframe.

@aphalo
Copy link
Contributor

aphalo commented Aug 27, 2018

Using NA instead of "" produces the correct figure, but issues warnings which can be silenced with na.rm = TRUE.

library(ggrepel)
library(patchwork)

d <- data.frame(
  x = c(1, 2, 3),
  y = c(1, 0, 1),
  label = c("alpha", NA, "gamma")
)

p <- ggplot(d, aes(x = x, y = y, label = label)) + geom_point()

p1 <- p + geom_label_repel(parse = TRUE, na.rm = TRUE) + ggtitle("parse = TRUE")
p2 <- p + geom_label_repel(parse = FALSE, na.rm = TRUE) + ggtitle("parse = FALSE")
p1 + p2

rplot01

Of course it would be natural for "" to behave in the same way as NA.

@slowkow
Copy link
Owner

slowkow commented Aug 27, 2018

Thank you, Pedro! I didn't think to try NA.

Maybe I should update the vignette to recommend NA instead of recommending the empty string "".

@pakidermo5000
Copy link
Author

Thank you both !!

NA does work and is what I will use.

Thanks a lot for looking into this, I really appreciate it.

@slowkow slowkow closed this as completed in 91877ca Sep 4, 2018
@slowkow
Copy link
Owner

slowkow commented Sep 4, 2018

Thanks to your report, I discovered that ggplot2 also has the same issue. I fixed the issue in ggplot2 with this pull request.

In ggrepel, I used the same code to fix this issue in 91877ca.

After the fix, here is the behavior we can expect when we use the empty string "" or NA:

library(ggrepel)
library(patchwork)

d <- data.frame(
  x = c(1, 2, 3),
  y = c(1, 0, 1),
  label = c("alpha", "", "gamma")
)

p <- ggplot(d, aes(x = x, y = y, label = label)) + geom_point(color = "red") +
  scale_x_continuous(expand = c(0, 1)) +
  scale_y_continuous(expand = c(0, 1))
p1 <- p + geom_text_repel(size = 5, parse = TRUE)
p2 <- p + geom_label_repel(size = 5, parse = TRUE)
p1 + p2

image

library(ggrepel)
library(patchwork)

d <- data.frame(
  x = c(1, 2, 3),
  y = c(1, 0, 1),
  label = c("alpha", NA, "gamma")
)

p <- ggplot(d, aes(x = x, y = y, label = label)) + geom_point(color = "red") +
  scale_x_continuous(expand = c(0, 1)) +
  scale_y_continuous(expand = c(0, 1))
p1 <- p + geom_text_repel(size = 5, parse = TRUE)
p2 <- p + geom_label_repel(size = 5, parse = TRUE)
p1 + p2
#> Warning messages:
#> 1: Removed 1 rows containing missing values (geom_text_repel). 
#> 2: Removed 1 rows containing missing values (geom_label_repel). 

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

3 participants