条形图允许我们使用误差线绘制条形图。它还允许通过自变量(因子)进行分组。我想按因变量分组,我怎样才能实现
bargraph.CI(x.factor, response, group=NULL, split=FALSE,
col=NULL, angle=NULL, density=NULL,
lc=TRUE, uc=TRUE, legend=FALSE, ncol=1,
leg.lab=NULL, x.leg=NULL, y.leg=NULL, cex.leg=1,
bty="n", bg="white", space=if(split) c(-1,1),
err.width=if(length(levels(as.factor(x.factor)))>10) 0 else .1,
err.col="black", err.lty=1,
fun = function(x) mean(x, na.rm=TRUE),
ci.fun= function(x) c(fun(x)-se(x), fun(x)+se(x)),
ylim=NULL, xpd=FALSE, data=NULL, subset=NULL, ...)
bargraph.CI的规格如上所示。响应变量通常是数字向量。这次,我真的想要针对相同的自变量绘制三个响应变量(A,B,C)。让我用数据框“mpg”来说明问题。我可以通过以下代码获得一个情节,这里的DV是hwy
data(mpg)
attach(mpg)
bargraph.CI(
class, #categorical factor for the x-axis
hwy, #numerical DV for the y-axis
group=NULL, #grouping factor
legend=T,
ylab="Highway MPG",
xlab="Class")
我也可以成功获得一个情节,唯一的变化是DV(从hwy改为cty)
data(mpg)
attach(mpg)
bargraph.CI(
class, #categorical factor for the x-axis
cty, #numerical DV for the y-axis
group=NULL, #grouping factor
legend=T,
ylab="Highway MPG",
xlab="Class")
但是,如果我想同时使用这两个DV,我的意思是,对于每个组,我想显示两个条形图,一个用于cty,一个用于高速公路。
data(mpg)
attach(mpg)
bargraph.CI(
class, #categorical factor for the x-axis
c(cty,hwy), #numerical DV for the y-axis
group=NULL, #grouping factor
legend=T,
ylab="Highway MPG",
xlab="Class")
由于尺寸不匹配,它不起作用。我怎样才能做到这一点?那么,实际上通过使用Boxplot schmoxplot: How to plot means and standard errors conditioned by a factor in R?和ggplot2的方法可以实现条形图的类似效果。所以,如果您对如何使用ggplot2有所了解,那对我来说也没问题。
答案 0 :(得分:0)
通常在显示数据时,您应首先操作数据,然后使用bargraph.CI
。在您的expamle中,您想要显示的data.frame
如下:
df <- data.frame(class=c(mpg$class, mpg$class),
value=c(mpg$cty, mpg$hwy),
grp=rep(c("cty", "hwy"), each=nrow(mpg)))
然后,您可以对此新bargraph.CI
使用data.frame
。
bargraph.CI(
class, #categorical factor for the x-axis
value, #numerical DV for the y-axis
group=grp, #grouping factor
data=df,
legend=T,
ylab="Highway MPG",
xlab="Class")