0

Using the mtcars dataset as an example, I am using "annotate" in the ggscatter function of ggpubr in R. This is my code:

library("ggpubr")
ggscatter(mtcars[mtcars$cyl == 8, ], x = "mpg", y = "qsec",
     facet.by = "cyl", add = "reg.line", add.params = list(color = "black", fill = "lightgrey"), 
     conf.int = TRUE, cor.method = "spearman", cor.coef = TRUE, xlab = "mpg", ylab = "qsec")
     + annotate("text", x=10, y=15, parse=TRUE, label = paste0("n = ", with(mtcars, sum(mtcars$cyl == 8))))

It all works perfectly, except the annotation, specifically this part:

label = paste0("n = ", with(mtcars, sum(mtcars$cyl == 8)))

I would like the annotated text to read: "n = 14". However, it reads: "=(n, 14)". How come it looks so different? Any suggestions are very welcome. Thank you.

Sylvia Rodriguez
  • 1,203
  • 2
  • 11
  • 30
  • 1
    I'm not sure if it's the issue, but the code is a little awkward... `with(mtcars, sum(cyl==8))` or `sum(mtcars$cyl == 8))` would do the trick. – cory May 16 '19 at 12:03
  • @cory - Thank you for your suggestion, but unfortunately this does not work for me. When I replace `with(mtcars, sum(mtcars$cyl == 8))` and instead use either of your suggestions, it keeps coming out the same. Seems odd, if this solves it for you? Regarding the little awkward code, you are right, it is unusual, because I "simplified" it from a more complex code, just for the purpose of this example. I thought it is irrelevant to what I am trying to solve. Thank you. – Sylvia Rodriguez May 16 '19 at 12:25
  • @cory - Just following up, even when I simply use `label = paste0("n = 14")` then it still shows "=(n, 14" instead of "n = 14". Seems so strange to me. – Sylvia Rodriguez May 16 '19 at 12:46
  • @cory - Okay, I solved it. It was caused by `parse=TRUE`. When I deleted it from the code, it suddenly worked. I do not understand it, but that is okay. I am happy that it works now. Thanks for your help and for reading all my comments. – Sylvia Rodriguez May 16 '19 at 12:52

1 Answers1

0

As per my comment above, deleting parse=TRUE from the code solved the problem.

Sylvia Rodriguez
  • 1,203
  • 2
  • 11
  • 30