gpt4 book ai didi

r - 创建自定义几何图形来计算汇总统计数据并在绘图区域*外部*显示它们

转载 作者:行者123 更新时间:2023-12-03 14:50:22 26 4
gpt4 key购买 nike

我是R包的创建者EnvStats .

有一个我经常使用的函数,叫做 stripChart .我刚开始学习ggplot2 ,并在过去几天里仔细研究了 Hadley 的书、Winston 的书、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)

这是一个 geom 的早期草稿,用于尝试重现 stripChart 的大部分功能:
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")

您可以看到这些图几乎相同。我遇到的问题是弄清楚如何在每组下方添加样本量,并在顶部添加均值和标准差,以及方差分析的结果(此时忽略不等方差的问题) .我知道计算汇总统计数据然后将它们绘制为绘图区域内的点或文本很简单,但我不想这样做。

我已经找到了展示如何将文本放置在图外的示例(例如,使用 annotation_custom() ):
How can I add annotations below the x axis in ggplot2?

Displaying text below the plot generated by ggplot2

问题在于示例展示了如何在用户预先定义注释是什么的情况下执行此操作。我的问题是在 geom_stripchart 内,我必须根据调用 ggplot() 时定义的数据计算汇总统计数据和测试结果。 ,然后将这些结果传递给 annotation_custom() .我不知道如何获取在对 ggplot() 的调用中定义的 x 和 y 变量。 .

最佳答案

我在这里发布了这个问题的简单版本:ggplot2: Adding sample size information to x-axis tick labels

我已经更新了 EnvStats包中包含 geomgeom_stripchart这是 EnvStats 的改编版功能 stripChart .见 help filegeom_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")

Demo of geom_stripchart

关于r - 创建自定义几何图形来计算汇总统计数据并在绘图区域*外部*显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39993117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com