R 如何在函数中包装均值和CI图
下面是由R 如何在函数中包装均值和CI图,r,function,ggplot2,functional-programming,R,Function,Ggplot2,Functional Programming,下面是由summarySE生成的数据集,显示了t和sex组的平均值和置信区间 mn.bmd <- structure(list(sex = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("female", "male"), class = "factor"), t = c(10L, 12L, 1
summarySE
生成的数据集,显示了t
和sex
组的平均值和置信区间
mn.bmd <- structure(list(sex = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L), .Label = c("female", "male"), class = "factor"),
t = c(10L, 12L, 14L, 16L, 18L, 10L, 12L, 14L, 16L, 18L),
N = c(2731L, 2750L, 2607L, 2524L, 2397L, 2427L, 2452L, 2374L,
2343L, 1935L), bmd = c(0.771745743658987, 0.852563274643638,
0.959264663475704, 1.00448137517321, 1.03961818701633, 0.78197475849084,
0.84601311310275, 0.953283665154095, 1.0561553454168, 1.14395286996851
), sd = c(0.0546859583968217, 0.0728002055433497, 0.0765731777406101,
0.0729628520321917, 0.0752411677480204, 0.0524685598606996,
0.060935438701901, 0.085630182993752, 0.0964219075622181,
0.100009937518834), se = c(0.00104644155540708, 0.00138824544949947,
0.00149970608925882, 0.00145230263867668, 0.00153681471482534,
0.00106503592133958, 0.00123057959424098, 0.00175746431217515,
0.00199200110406779, 0.00227354037595468), ci = c(0.00205189747680689,
0.00272210959875271, 0.00294073574524029, 0.00284782704999121,
0.00301362384271727, 0.00208847400752308, 0.00241308331525491,
0.0034463245617893, 0.003906269195061, 0.00445884772686761
)), class = "data.frame", row.names = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10"), .Names = c("sex", "t", "N", "bmd",
"sd", "se", "ci"))
我想将这个ggplot代码包装在一个函数中,以便对不同的数据帧重复它(这些数据帧都具有相同的结构,但是对于y
具有不同的列名)-我尝试过使用aes\u string
,但没有成功
my_plot <- function(df, y) {
ggplot(df, aes_string(x="t", y=y, colour="sex")) +
geom_errorbar(aes(ymin=y-ci, ymax=y+ci), size=0.3, width=.3) +
geom_line() +
geom_point(size=3, shape=21)
}
#Error message
Error in y - ci : non-numeric argument to binary operator
my_plot我认为,只要所有数据帧都有相同的列名,就应该在函数中包装ggplot调用
如果数据帧没有相同的colname,则必须使用get()
函数将colname作为字符串传递给ggplot()
。所以,比如说
ggplot(x,aes(x=t))
你会的
ggplot(x,aes(x=get(colname_x)))
其中,colname\u x
是一个字符串,包含要传递给ggplot()
asx
编辑
针对OP的评论:
我会将列的名称添加到函数的参数中,并在对ggplot()的调用中添加get()语句,如下所示
my_plot如果所有数据帧都以相同的方式形成,则此功能应能正常工作:
library(dplyr)
my_plot <- function(df, y) {
ymin <- df[[y]] - df$ci
ymax <- df[[y]] + df$ci
ggplot(df, aes_string(x="t", y=y, colour="sex")) +
geom_errorbar(aes(ymin=ymin, ymax=ymax), size=0.3, width=.3) +
geom_line() +
geom_point(size=3, shape=21)
}
# you can replace mn.bmd with other data frames and check the result
my_plot(df = mn.bmd, y = "bmd")
库(dplyr)
我的图为什么bmdlh
用于geom\u errorbar
而不是bmd
?很抱歉,在这里复制代码时出现了一个错误-我只是更改了列名-但这与我遇到的问题无关。请您提供一个带有dput
输出的最小数据集,并对您的问题进行更多的评估?所有数据帧都将具有相同的结构,对吗?谢谢-我提供了dput输出,并澄清了所有数据帧都将具有相同的结构库-不同的数据帧具有不同的y名称(您将如何编辑代码以实现此目的?)感谢您的帮助,但是仍然存在关于ymax和ymin的问题-它们应该是'ymin=y-ci,ymax=y+ci',这是因为y列会有所不同。但是,当我进行此编辑时,会出现以下错误:“y-ci中的错误:二进制运算符的非数值参数”@aelhak;我忘了调整那部分。请查看此内容。谢谢-因此,请您澄清如何编辑下面的代码以处理具有不同y列名的数据帧我的计划没问题!我编辑了我的答案来回答你的问题
my_plot <- function(df,colname_y) {
ggplot(df, aes(x=t, y=get(colname_y), colour=sex)) +
geom_errorbar(aes(ymin=bmd-ci, ymax=bmd+ci), size=0.3, width=.3) +
geom_line() + geom_point(size=3, shape=21)
}
library(dplyr)
my_plot <- function(df, y) {
ymin <- df[[y]] - df$ci
ymax <- df[[y]] + df$ci
ggplot(df, aes_string(x="t", y=y, colour="sex")) +
geom_errorbar(aes(ymin=ymin, ymax=ymax), size=0.3, width=.3) +
geom_line() +
geom_point(size=3, shape=21)
}
# you can replace mn.bmd with other data frames and check the result
my_plot(df = mn.bmd, y = "bmd")