ggplot2: Adding sample size information to x-axis tick labels

前端 未结 3 1672
慢半拍i
慢半拍i 2021-02-13 00:21

This question is related to Create custom geom to compute summary statistics and display them *outside* the plotting region (NOTE: All functions have been simplified; no error

3条回答
  •  执念已碎
    2021-02-13 00:46

    I have updated the EnvStats package to include a stat called stat_n_text which will add the sample size (the number of unique y-values) below each unique x-value. See the help file for stat_n_text for more information and a list of examples. Below is a simple example:

    library(ggplot2)
    library(EnvStats)
    
    p <- ggplot(mtcars, 
      aes(x = factor(cyl), y = mpg, color = factor(cyl))) + 
      theme(legend.position = "none")
    
    p + geom_point() + 
      stat_n_text() + 
      labs(x = "Number of Cylinders", y = "Miles per Gallon")
    

提交回复
热议问题