问题描述
我试图在一张图中绘制三个森林图, 我的数据是这样的:
结果(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 (将#修改为@)