Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot如何创建成对的错误条_R_Plot_Ggplot2_Visualization - Fatal编程技术网

R ggplot如何创建成对的错误条

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 ,

我对同一模型的一对结果进行了估计,我想用误差条显示这些结果。如何创建一对图,将样本数据中的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") 

如果我没弄错的话,你需要刻面。它可以是这样的:

# 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次subsetting
df
,就像答案的最后一行一样。您的意思是我应该运行ggplot2 4次吗?这有点过分了。再次谢谢你。我真正想要的是在一个绘图中有4对错误条。抱歉误会了,谢谢你。然而,我想要的是rea和其他两对图。因此,我总共需要4对误差条,每次带有rea.@Meso所以,你需要4个单独的绘图?每个地块是rea还是其他估算?在这种情况下,您可以每次调用
ggplot
4次subsetting
df
,就像答案的最后一行一样。您的意思是我应该运行ggplot2 4次吗?这有点过分了。再次谢谢你。我真正想要的是在一个绘图中有4对错误条。对不起,误解了。@redmode和Didzis,我想要的是一张图中的4对绘图。我想要的绘图示例可以在以下链接中找到:@Meso如果你在问题中添加示例图像,你会更早得到答案,因为没有图像,很难找到答案understand@redmodeDidzis,我想要的是一张图中的4对图。我想要的绘图示例可以在以下链接中找到:@Meso如果你在问题中添加示例图像,你会更早得到答案,因为没有图像很难理解