我对来自同一模型的一对结果有估计,我想使用误差条显示这些结果。如何创建一对比较样本数据中rea估计值与其他数据(rea和sci,rea和mat等等)的图。在所有图中,rea应始终显示为图中的第一个,并且数据的顺序不应更改。
library (ggplot2)
ucl<- c(5.88 , 3.92, 7.0, 3.724, 5.488)
lcl<-c(1.04 , 0.04, 2.04, -0.06 , 0.84)
est<-c(3 , 2, 4, 1.9, 2.8)
df<-data.frame (cbind(est,ucl, lcl))
df$labels<-c("sci","rea","mat","apt","hor")
此代码创建了单独的图,但我需要的是一对比较rea与其他图的图。
ggplot(df, aes(x = labels, y = est,ymin = lcl, ymax = ucl)) +
geom_point(position=position_dodge() ,size = 2.5) +
geom_linerange(position=position_dodge(), size =0.5) +
geom_hline(aes(yintercept = 1))
答案 0 :(得分:3)
如果我弄错了,你需要方面。它可以是这样的:
# Additional column for faceting
df$not.rea <- df$labels!="rea"
# Your solution + facet_grid with labeller and free scales/space
ggplot(df, aes(x=labels, y=est, ymin=lcl, ymax=ucl)) +
geom_point(position=position_dodge(), size=2.5) +
geom_linerange(position=position_dodge(), size=0.5) +
geom_hline(aes(yintercept=1)) +
facet_grid(~not.rea, scales="free", space="free",
labeller=function(var,val) c("rea","other")[val+1])
它产生:
但是如果你想要将每个估算与rea进行面对面的比较,你可以将传递给data.frame
的{{1}}进行子集化,即对于rea与mat:
ggplot2
<强>更新强>
为了生成4个图,您可以使用这种方法:
ggplot(df[df$labels %in% c("rea","mat"),], ...) + ...
显示4个图。如果要将它们保存为单独的文件,可以像这样修改运行程序:
# Convert labels to factor - rea should always be first
df$labels <- factor(df$labels, levels=c("rea","sci","mat","apt","hor"))
# Plotting function
one.plot <- function(filter){
ggplot(df[df$labels %in% c("rea",filter),], aes(x=labels, y=est, ymin=lcl, ymax=ucl)) +
geom_point(position=position_dodge(), size=2.5) +
geom_linerange(position=position_dodge(), size=0.5) +
geom_hline(aes(yintercept=1)) +
facet_grid(~labels, scales="free", space="free")
}
# Runner of the batch
sapply(as.character(df$labels[df$labels != "rea"]), function(s) print(one.plot(s)) )
它将4 sapply(as.character(df$labels[df$labels != "rea"]), function(s) ggsave(paste0(s,".png"), one.plot(s)) )
个文件保存到工作目录。
更新2:
要在单个图中生成4对错误栏,您可以使用*.png
包。这很简单:
gridExtra
产生:
答案 1 :(得分:2)
您可以更改数据框以获得所需的结果。
首先,为df2
,sci
,mat
和apt
的每个级别以及{{{4}行创建包含一行的数据框hor
1}}。然后将列rea
作为因子,按照您需要的顺序(labels
作为第一个,然后是所有其他级别)。添加包含与rea
labels2
相同级别名称的列labels
,每个级别使用两次(一次用于同一级别,一次用于rea
)。
rea
现在使用df2<-rbind(df[-2,],df[rep(2,4),])
df2$labels<-factor(df2$labels,levels=c("rea","sci","mat","apt","hor"))
df2$labels2<-factor(rep(c("sci","mat","apt","hor"),times=2),
levels=c("sci","mat","apt","hor"))
df2
est ucl lcl labels labels2
1 3.0 5.880 1.04 sci sci
3 4.0 7.000 2.04 mat mat
4 1.9 3.724 -0.06 apt apt
5 2.8 5.488 0.84 hor hor
2 2.0 3.920 0.04 rea sci
21 2.0 3.920 0.04 rea mat
22 2.0 3.920 0.04 rea apt
23 2.0 3.920 0.04 rea hor
来绘制数据 - df2
用于x轴,labels
用于构面。在labels2
中添加scales="free_x"
以删除每个方面中未使用的级别。
facet_wrap()
要获取一个绘图中的所有线条,请添加到数据框ggplot(df2,aes(x = labels, y = est,ymin = lcl, ymax = ucl)) +
geom_point(size = 2.5) +
geom_linerange(size =0.5) +
geom_hline(aes(yintercept = 1)) +
facet_wrap(~labels2,scales="free_x")
(在上面的示例中制作)列df2
,其中包含两个级别 - type
和rea
。
other
然后使用df2$type<-rep(c("other","rea"),each=4)
作为x值,使用labels2
设置type
或color=
,或仅使用shape=
作为行范围。
group=