0
votes

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.

1
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
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 first. - neilfws
@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
@SylviaRodriguez: you can create a count column and use label_glue from the stickylabeller package to add them to facet labels - Tung
Thank you, @Tung. I will have a look. - Sylvia Rodriguez

1 Answers

1
votes

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