创建自定义geom以计算摘要统计信息并在*绘图区域外显示它们

Ste*_*e M 6 r ggplot2 envstats

我是R包EnvStats的创建者.

有一个我经常使用的函数叫做stripChart.我刚刚开始学习ggplot2,过去几天一直在研究哈德利的书,温斯顿的书,StackOverflow和其他资源,试图创造一个geom近似于它的东西stripChart.我无法弄清楚如何geom计算汇总统计数据和测试结果,然后将它们放在x轴刻度线下方以及绘图顶部(绘图区域外).以下是使用内置数据集的简单示例mtcars:

library(EnvStats)
stripChart(mpg ~ cyl, data = mtcars, col = 1:3, 
  xlab = "Number of Cylinders", ylab = "Miles per Gallon", p.value = TRUE)
Run Code Online (Sandbox Code Playgroud)

以下是尝试重现stripChart的大部分功能的geom的早期草稿:

geom_stripchart <- 
function(..., x.nudge = 0.3, 
  jitter.params = list(width = 0.3, height = 0), 
  mean.params = list(size = 2, position = position_nudge(x = x.nudge)), 
  errorbar.params = list(size = 1, width = 0.1, 
  position = position_nudge(x = x.nudge)), 
  n.text = TRUE, mean.sd.text = TRUE, p.value = FALSE) {
    params <- list(...)
    jitter.params   <- modifyList(params, jitter.params)
    mean.params     <- modifyList(params, mean.params)
    errorbar.params <- modifyList(params, errorbar.params)

    jitter <- do.call("geom_jitter", jitter.params)
    mean   <- do.call("stat_summary", modifyList(
      list(fun.y = "mean", geom = "point"), 
      mean.params)
    )
    errorbar <- do.call("stat_summary", modifyList(
      list(fun.data = "mean_cl_normal", geom = "errorbar"), 
      errorbar.params)
    )

    stripchart.list <- list(
      jitter, 
      theme(legend.position = "none"),
      mean, 
      errorbar
    )

    if(n.text || mean.sd.text) {
# Compute summary statistics (sample size, mean, SD) here?
      if(n.text) {
# Add information to stripchart.list to 
# compute sample size per group and add text below x-axis
      }
      if(mean.sd.text) {
# Add information to stripchart.list to 
# compute mean and SD and add text above top of plotting region
      }
    }
    if(p.value) {
# Add information to stripchart.list to 
# compute p-value (and 95% CI for difference if only 2 groups) 
# and add text above top of plotting region
    }
    stripchart.list
}


library(ggplot2)
dev.new()
p <- ggplot(mtcars, aes(x = factor(cyl), y = mpg, color = factor(cyl)))
p + geom_stripchart() + 
    xlab("Number of Cylinders") + 
    ylab("Miles per Gallon")
Run Code Online (Sandbox Code Playgroud)

你可以看到这些情节几乎是一样的.我遇到的问题是如何在每组下面添加样本量,并在顶部添加均值和标准差,以及ANOVA检验的结果(忽略此时不等方差的问题) .我知道计算摘要统计数据然后将它们绘制为绘图区域的点或文本是直截了当的,但我不想这样做.

我已经找到了显示如何在文本外部放置文本的示例(例如,使用annotation_custom()):
如何在ggplot2中的x轴下添加注释?

在ggplot2生成的图表下方显示文本

问题是示例显示了如何在用户预定义注释的位置执行此操作.我的问题是,在内部geom_stripchart,我必须根据调用中定义的数据计算摘要统计信息和测试结果ggplot(),然后将这些结果传递给annotation_custom().我不知道如何获得调用中定义的x和y变量ggplot().

Ste*_*e M 1

我在这里发布了这个问题的一个简单版本: ggplot2:将样本大小信息添加到 x 轴刻度标签

我已经更新了EnvStats包以包含一个geom调用,它是EnvStats函数geom_stripchart的改编版。有关详细信息和示例列表,请参阅帮助文件。下面是一个简单的例子:stripChartgeom_stripchart

library(ggplot2)
library(EnvStats)

p <- ggplot(mtcars, aes(x = factor(cyl), y = mpg, color = factor(cyl))) 

p + geom_stripchart(test.text = TRUE) + 
  labs(x = "Number of Cylinders", y = "Miles per Gallon")
Run Code Online (Sandbox Code Playgroud)

geom_stripchart 演示