如何解决带有 facet_wrap 的森林图
我试图在一张图中绘制三个森林图, 我的数据是这样的:
结果(sebze)是一个二项式变量。 bkiodd 是一个有四个选项的多项式变量,模型有三个选项。首先我使用了以下代码:
#
head(mydata)
library(gridExtra)
library(gtable)
library(ggplot2)
library(grid)
# Convert rank to factor.
mydata$bkiodd <- factor(mydata$bkiodd)
#plot 1
lm3 <- glm(sebze~ mydata$bkiodd,data = mydata,family = "binomial")
library(broom)
model_output <- tidy(lm3)
out_conf <- tidy(lm3,conf.int = TRUE)
library(forestmangr)
lm_model_out <- round_df(out_conf,digits=2)
p=ggplot(lm_model_out,aes(x=reorder(term,estimate),y=estimate)) +
geom_errorbar(aes(ymin=conf.low,ymax=conf.high),width = 0.2,size = 1,position = "dodge",color="black") +
geom_hline(yintercept = 0,color = "black",size = 1) +
geom_point(aes(shape = factor(term))) + coord_flip()+theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank())
v=p + theme(legend.position = "none")
#plot 2
lm3 <- glm(meyve~ mydata$bkiodd,digits=2)
g=ggplot(lm_model_out,axis.ticks.x=element_blank())
s=g + theme(legend.position = "none")
#plot 3
lm3 <- glm(dsomeyvesebze~ mydata$bkiodd,digits=2)
q=ggplot(lm_model_out,axis.ticks.x=element_blank())
t=q + theme(legend.position = "none")
#combine the plots
g2 <- ggplotGrob(v)
g3 <- ggplotGrob(s)
g4 <- ggplotGrob(t)
hh <- rbind(g2,g3,g4,size = "last")
hh$widths <- unit.pmax(g2$widths,g3$widths)
grid.newpage()
grid.draw(hh)
#
结果如下图:
但是我需要相同的垂直线来绘制图,然后我决定使用 facet_wrap 和下面的代码:
#
mydata2$bkiodd <- factor(mydata2$bkiodd)
lm3 <- glm(sebze~ mydata2$bkiodd,data = mydata2,axis.ticks.x=element_blank()) +
facet_wrap(~model,ncol=1)
v=p + theme(legend.position = "none")
#
结果有如下错误:
至少一层必须包含所有分面变量:model
.
- 情节缺失
model
- 缺少第 1 层
model
- 第 2 层缺失
model
- 第 3 层缺失
model
运行rlang::last_error()
以查看发生错误的位置。 .. 你能帮我吗,我怎样才能画出这个数字?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。