R 使用“gpplot2”使用多因子变量复制“sciplot”
我非常喜欢使用R 使用“gpplot2”使用多因子变量复制“sciplot”,r,R,我非常喜欢使用sciclot获取实验数据,因为我不需要手动计算误差条。在过去,我用它对两个因素变量进行分组,例如: plot1<-bargraph.CI( df$factor1, #categorical factor for the x-axis df$y, #numerical DV for the y-axis df$factor2 #grouping factor ) 您可以通过对stat\u summary的两个调用(在一定程度上)复制sc
sciclot
获取实验数据,因为我不需要手动计算误差条。在过去,我用它对两个因素变量进行分组,例如:
plot1<-bargraph.CI(
df$factor1, #categorical factor for the x-axis
df$y, #numerical DV for the y-axis
df$factor2 #grouping factor
)
您可以通过对stat\u summary
的两个调用(在一定程度上)复制sciplot
您可以将两个因素级别合并为交互
(使用交互
)或使用镶嵌面
我将使用ToothGrowth
,它与base R一起在数据集包中提供
# add third factor
ToothGrowth$F3 <- letters[1:2]
# coerce dose to a factor
ToothGrowth$dose <- factor(ToothGrowth$dose, levels = c(0.5,1,2))
# interaction on the x axis
ggplot(ToothGrowth, aes(y = len, x = interaction(supp, F3))) +
stat_summary(fun.y = 'mean', fun.ymin = function(x) 0, geom = 'bar',
aes(fill =dose), position = 'dodge') +
stat_summary(fun.ymin = function(x) mean(x) - sd(x),
fun.ymax = function(x) mean(x) + sd(x), position ='dodge',
geom = 'errorbar', aes(group = dose))
实际上,这在sciplot中是可能的。下面是两种解决方案,第一种是将分组因子指定为列表,第二种是从ggplot复制刻面解决方案
library(sciplot)
## add third factor as in above example
ToothGrowth$F3 <- letters[1:2]
## Adding group as a list
bargraph.CI(response=len, x.factor=supp, group=list(dose, F3),
data=ToothGrowth, legend=TRUE, x.leg=14, xlim=c(0,19),
err.width=0.025)
当你说“三因素变量分组”时,你是指分组因子包含3个变量,还是希望在x轴分组因子之间包含第三个因子变量?后者-我希望在x轴分组因子之间包含第三个因子变量!(+1)你现在可以发布图片了
# facetting on the third factor
ggplot(ToothGrowth, aes(y = len, x = supp )) +
stat_summary(fun.y = 'mean', fun.ymin = function(x) 0, geom = 'bar',
aes(fill =dose), position = 'dodge') +
stat_summary(fun.ymin = function(x) mean(x) - sd(x),
fun.ymax = function(x) mean(x) + sd(x), position ='dodge',
geom = 'errorbar', aes(group = dose))+
facet_wrap(~F3)
ggplot(ToothGrowth, aes(y = len, x = supp)) +
stat_summary(fun.y = 'mean', fun.ymin = function(x) 0,
geom = 'bar', aes(fill =interaction(dose, F3)),
position = 'dodge') +
stat_summary(fun.ymin = function(x) mean(x) - sd(x),
fun.ymax = function(x) mean(x) + sd(x),
position ='dodge', geom = 'errorbar',
aes(fill =interaction(dose, F3)))
library(sciplot)
## add third factor as in above example
ToothGrowth$F3 <- letters[1:2]
## Adding group as a list
bargraph.CI(response=len, x.factor=supp, group=list(dose, F3),
data=ToothGrowth, legend=TRUE, x.leg=14, xlim=c(0,19),
err.width=0.025)
## Using "panels"
par(mfrow=c(1,2), xpd=NA)
bargraph.CI(response=len, x.factor=supp, group=dose, data=ToothGrowth,
subset=F3=="a", xlab="a", cex.lab=1.25,
legend=TRUE, x.leg=7.5, err.width=.025)
bargraph.CI(response=len, x.factor=supp, group=dose, data=ToothGrowth,
subset=F3=="b", xlab="b", cex.lab=1.25, err.width=.025)