Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 在ggplot2中重新排列镶嵌面_R_Ggplot2_Facet - Fatal编程技术网

R 在ggplot2中重新排列镶嵌面

R 在ggplot2中重新排列镶嵌面,r,ggplot2,facet,R,Ggplot2,Facet,我已经在网站上查看了更改刻面顺序的示例,但似乎没有任何东西适合我的具体案例。任何关于如何使用我的数据设置的想法都将不胜感激。我对ggplot2有点陌生,非常困惑。我想我的模型订购M1,M2,M3,M1在每个方面的顶部。谢谢 Outcome<-c(1,1,1,2,2,2,3,3,3) OR<-c(1.97,2.47,3.56,1.73,2.25,4.09,1.21,1.48,2.25) min<-c(1.37,1.74,2.55,1.13,1.52,2.84,0.74,0.95,

我已经在网站上查看了更改刻面顺序的示例,但似乎没有任何东西适合我的具体案例。任何关于如何使用我的数据设置的想法都将不胜感激。我对ggplot2有点陌生,非常困惑。我想我的模型订购M1,M2,M3,M1在每个方面的顶部。谢谢

Outcome<-c(1,1,1,2,2,2,3,3,3)
OR<-c(1.97,2.47,3.56,1.73,2.25,4.09,1.21,1.48,2.25)
min<-c(1.37,1.74,2.55,1.13,1.52,2.84,0.74,0.95,1.49)
max<-c(2.83,3.49,4.98,2.66,3.35,5.9,1.97,2.33,3.41)
Aces<-c(1,2,3,1,2,3,1,2,3)
ace<-data.frame(cbind(Outcome,OR,min,max,Aces))

ace_labels <- list('1'="1 ACE",'2'="2 ACEs",'3'="3 ACEs")

ace_labeller <- function(variable,value){return(ace_labels[value])}

ace$Outcome = factor(ace$Outcome, levels=c("1","2","3"), 
                 labels=c("M1: Depression","M2: MH Barrier to Work",
                          "M3: MH Barrier to Work"))            

ggplot(data=ace,aes(x=OR,y=Outcome,label=OR))+
geom_point(aes(size=8))+
geom_text(size=5,vjust=1.8)+
geom_errorbarh(aes(xmin=min,xmax=max),height=.1)+
geom_vline(xintercept=1,linetype="dashed")+
scale_x_log10(breaks=seq(1,6,1),name="Figure 1: Odds Ratios (OR) and 95% Confidence Intervals for Models 1, 2 and 3 (N=1073)
            \nEach OR is compared to 0 ACEs")+
labs(y="")+
facet_grid(Aces~., labeller=ace_labeller)+
guides(size=FALSE)+
theme_bw()+
theme(axis.text.y=element_text(size=12))+
theme(strip.text.y=element_text(size = 12))+
theme(axis.title.x=element_text(face="italic", size = 12,hjust=.5))

Outcome如果你颠倒你为
ace$Outcome
设置的标签,你可以让它在每个方面的顶部绘制M1,在底部绘制M2和M3

只要改变一下:

ace$Outcome = factor(ace$Outcome, levels=c("3","2","1"), 
             labels=c("M3: MH Barrier to Work",
                      "M2: MH Barrier to Work",
                      "M1: Depression"))
使用其生成的相同样本数据和绘图代码:

您的代码(仅更改了上述代码):


outcome如果你需要帮助,你应该提供一些有用的数据
Outcome<-c(1,1,1,2,2,2,3,3,3)
OR<-c(1.97,2.47,3.56,1.73,2.25,4.09,1.21,1.48,2.25)
min<-c(1.37,1.74,2.55,1.13,1.52,2.84,0.74,0.95,1.49)
max<-c(2.83,3.49,4.98,2.66,3.35,5.9,1.97,2.33,3.41)
Aces<-c(1,2,3,1,2,3,1,2,3)
ace<-data.frame(cbind(Outcome,OR,min,max,Aces))

ace_labels <- list('1'="1 ACE",'2'="2 ACEs",'3'="3 ACEs")

ace_labeller <- function(variable,value){return(ace_labels[value])}

ace$Outcome = factor(ace$Outcome, levels=c("3","2","1"), 
                 labels=c("M3: MH Barrier to Work",
                          "M2: MH Barrier to Work",
                          "M1: Depression"))            

library(ggplot2)

ggplot(data=ace,aes(x=OR,y=Outcome,label=OR))+
geom_point(aes(size=8))+
geom_text(size=5,vjust=1.8)+
geom_errorbarh(aes(xmin=min,xmax=max),height=.1)+
geom_vline(xintercept=1,linetype="dashed")+
scale_x_log10(breaks=seq(1,6,1),name="Figure 1: Odds Ratios (OR) and 95% Confidence Intervals for Models 1, 2 and 3 (N=1073)
            \nEach OR is compared to 0 ACEs")+
labs(y="")+
facet_grid(Aces~., labeller=ace_labeller)+
guides(size=FALSE)+
theme_bw()+
theme(axis.text.y=element_text(size=12))+
theme(strip.text.y=element_text(size = 12))+
theme(axis.title.x=element_text(face="italic", size = 12,hjust=.5))