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
Comments
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 As you described, the labels are correct when |
The
It just deletes the empty string:
I'm not sure how to fix this! If you have any suggestions, please let me know. |
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) I'm still looking for a way to fix this issue without creating the second dataframe. |
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 Of course it would be natural for "" to behave in the same way as NA. |
Thank you, Pedro! I didn't think to try Maybe I should update the vignette to recommend |
Thank you both !! NA does work and is what I will use. Thanks a lot for looking into this, I really appreciate it. |
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 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
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). |
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
When parse=FALSE we get this image
When parse=TRUE we get this other image with the labels in the wrong position
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:The text was updated successfully, but these errors were encountered: