我在R中有以下代码。
df <- data.frame(Region = rep(1:3,3), education = rep(1:3,each =3),
mean = runif(9,0,1) , lo = runif(9, -1 , 0),
up = runif(9, 1, 2))
ggplot(df, aes(Region, mean)) + geom_point(size=4) +
geom_errorbar(aes(ymin = lo, ymax = up))
如您所见,置信区间相互交叉。但是,我想要的是在变量教育之间有三个不同的置信区间,并且可能有三种不同的颜色。我们用另一个分类变量分隔条形图的方式相同
由于
答案 0 :(得分:1)
解决方案可能是:
library(ggplot2)
set.seed(1)
df <- data.frame(Region = rep(1:3,3), education = rep(1:3,each =3),
mean = runif(9,0,1) , lo = runif(9, -1 , 0),
up = runif(9, 1, 2))
df$RegEdu <- with(df, interaction(Region, education), drop = TRUE )
ggplot(df, aes(x=RegEdu, y=mean, colour=factor(education),
pch=factor(Region))) + geom_point(size=4) +
geom_errorbar(aes(ymin = lo, ymax = up))
修改强>
使用facet_grid
,您可以将错误栏组合在一起education
ggplot(df, aes(x=Region, y=mean, colour=factor(education))) +
geom_point(size=4) +
geom_errorbar(aes(ymin = lo, ymax = up))+
facet_grid(.~education)