R ggplot如何创建成对的错误条
我对同一模型的一对结果进行了估计,我想用误差条显示这些结果。如何创建一对图,将样本数据中的rea估计值与所有其他数据(rea和sci、rea和mat等)进行比较。在所有图中,rea应始终作为图中的第一个出现,并且数据的顺序不应改变R ggplot如何创建成对的错误条,r,plot,ggplot2,visualization,R,Plot,Ggplot2,Visualization,我对同一模型的一对结果进行了估计,我想用误差条显示这些结果。如何创建一对图,将样本数据中的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 ,
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")
如果我没弄错的话,你需要刻面。它可以是这样的:
# 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])
sapply(as.character(df$labels[df$labels != "rea"]), function(s) ggsave(paste0(s,".png"), one.plot(s)) )
更新:
为了生成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个*.png
文件保存到工作目录中
更新2:
要在单个绘图中生成4对错误条,可以使用gridExtra
package。这很容易:
library(gridExtra)
grid.arrange(one.plot("sci"),
one.plot("mat"),
one.plot("apt"),
one.plot("hor"),
ncol=2, main="Main Title")
产生:
如果我没弄错,你需要刻面。它可以是这样的:
# 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])
sapply(as.character(df$labels[df$labels != "rea"]), function(s) ggsave(paste0(s,".png"), one.plot(s)) )
更新:
为了生成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个*.png
文件保存到工作目录中
更新2:
要在单个绘图中生成4对错误条,可以使用gridExtra
package。这很容易:
library(gridExtra)
grid.arrange(one.plot("sci"),
one.plot("mat"),
one.plot("apt"),
one.plot("hor"),
ncol=2, main="Main Title")
产生:
您可以更改数据帧以获得所需的结果 首先,制作数据帧
df2
,该数据帧为sci
、mat
、apt
和hor
的每一级包含一行,以及四行rea
。然后将列标签
按您需要的顺序作为带有级别的因子(rea
作为第一级,然后是所有其他级别)。添加列labels2
,该列包含与labels
相同的级别名称,但rea
除外,并且每个级别使用两次(一次用于相同级别,一次用于rea
)
使现代化
要获取一个图中的所有线范围,请添加到数据框df2
(在上面的示例中制作)列类型
,并使用两个级别-rea
和其他
df2$type<-rep(c("other","rea"),each=4)
您可以更改数据帧以获得所需的结果 首先,制作数据帧
df2
,该数据帧为sci
、mat
、apt
和hor
的每一级包含一行,以及四行rea
。然后将列标签
按您需要的顺序作为带有级别的因子(rea
作为第一级,然后是所有其他级别)。添加列labels2
,该列包含与labels
相同的级别名称,但rea
除外,并且每个级别使用两次(一次用于相同级别,一次用于rea
)
使现代化
要获取一个图中的所有线范围,请添加到数据框df2
(在上面的示例中制作)列类型
,并使用两个级别-rea
和其他
df2$type<-rep(c("other","rea"),each=4)
谢谢你。然而,我想要的是rea和其他两对图。因此,我总共需要4对误差条,每次带有rea.@Meso所以,你需要4个单独的绘图?每个地块是rea还是其他估算?在这种情况下,您可以每次调用
ggplot
4次subsettingdf
,就像答案的最后一行一样。您的意思是我应该运行ggplot2 4次吗?这有点过分了。再次谢谢你。我真正想要的是在一个绘图中有4对错误条。抱歉误会了,谢谢你。然而,我想要的是rea和其他两对图。因此,我总共需要4对误差条,每次带有rea.@Meso所以,你需要4个单独的绘图?每个地块是rea还是其他估算?在这种情况下,您可以每次调用ggplot
4次subsettingdf
,就像答案的最后一行一样。您的意思是我应该运行ggplot2 4次吗?这有点过分了。再次谢谢你。我真正想要的是在一个绘图中有4对错误条。对不起,误解了。@redmode和Didzis,我想要的是一张图中的4对绘图。我想要的绘图示例可以在以下链接中找到:@Meso如果你在问题中添加示例图像,你会更早得到答案,因为没有图像,很难找到答案understand@redmodeDidzis,我想要的是一张图中的4对图。我想要的绘图示例可以在以下链接中找到:@Meso如果你在问题中添加示例图像,你会更早得到答案,因为没有图像很难理解