0

I am using the ggscatter function in the ggpubr package in R. See following code using the ToothGrowth dataset as an example:

library("ggpubr")
ggscatter(ToothGrowth, x = "dose", y = "len",
  color = "supp", facet.by = "supp")

I would like to add the sample sizes (n) to the facet labels, so that they read: "OJ (n=30)" and "VC (n=30)". I assume that I need to use "panel.labs". Instructions say:

panel.labs = list(sex = c("Male", "Female"), rx = c("Obs", "Lev", "Lev2")

But I am not able to modify this to get where I hope to get. It also was not clear what "rx" means. Any suggestions would be terrific. Thank you.

Edit

Thanks for your answers below. I would like to clarify that I would like to include the "n=30" in the code, so that the '30' is automatically added from the data, not just as a text label saying 'n=30'. I need to do this at a larger scale, on which it is not possible to add each individually as text. Apologies for not being clearer in my initial question. Thank you.

Sylvia Rodriguez
  • 1,203
  • 2
  • 11
  • 30
  • `rx` was the name of a variable. It's most likely supposed to stand for a treatment as in the "Rx" the doctor gives you . – IRTFM May 15 '19 at 21:56
  • Can I make a suggestion. You have a lot of questions about `ggpubr` and I think part of the issue is that it is built upon, but "hides" the inner workings of, `ggplot2`. I think it may be helpful to learn more about [how ggplot2 works](https://ggplot2.tidyverse.org/) first. – neilfws May 15 '19 at 22:29
  • @neilfws - Thank you for your suggestion. You are right that I am not too experienced. I will study that information more to improve my skills in general. Thank you for the link. That said, please also see my comment below, because my question was intended as a slightly more complicated one than originally posted. – Sylvia Rodriguez May 16 '19 at 00:44
  • 1
    @SylviaRodriguez: you can create a `count` column and use [`label_glue` from the `stickylabeller` package to add them to facet labels](https://stackoverflow.com/a/52217208/786542) – Tung May 17 '19 at 05:22
  • Thank you, @Tung. I will have a look. – Sylvia Rodriguez May 21 '19 at 04:20

1 Answers1

1

This worked for me (as an argument to ggscatter). It needs to have the variable name that hold the factor variable name and then it need to have the items in the proper order, in this case the OJ label first:

panel.labs = list( supp=c( "OJ (n=30)" , "VC (n=30)") ) )

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thank you. This answers my question. However, I apologize for not being more clear in my question. I hoped to have this 'automated', so that n=30 will be in the code reading it from the data, rather than typed as "n=30" in the code. The reason is that I will need to do this on a large scale, so automating this would be very helpful, if you are able to help with that. Thank you very much. – Sylvia Rodriguez May 16 '19 at 00:41