R语言 如何在静止状态下将两个图形合并为一个图形?

5f0d552i  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(173)

数据是fraway引导
我想
绘制一个图,显示年龄-x轴和唐氏出生比例
每个年龄组的y轴。添加两条曲线显示预测概率
来自主效应模型和二次模型。带图例。

lmod2 <- glm(prop ~ age + I(age^2),
             family=quasibinomial, finaldata)
lmod <- glm(prop ~ age,
            family=quasibinomial, finaldata)

我尝试过的事情:

plot(predict(lmod,lmod2, newdata = list(age = 16:50), type = 'response'), type = 'l',
+      ylab = "Probability of Down's syndrome per live birth", 
+      xlab = 'Maternal age')
Error in !se.fit : invalid argument type

> plot(lmod,lmod2)
Error in plot.lm(lmod, lmod2) : 'which' must be in 1:6

请给我带路。

eqqqjvef

eqqqjvef1#

您可以使用par and mfrow来设置您希望绘图的布局。

data(mtcars)
lmod <- glm(vs ~ mpg, family = quasibinomial, mtcars)
lmod2 <- glm(vs ~ mpg + I(mpg^2), family = quasibinomial, mtcars)

par(mfrow=c(1, 2))
# or par(mfrow=c(2, 1))
plot(predict(lmod, type = 'response'), type = 'l',
     ylab = "Probability of Down's syndrome per live birth", 
     xlab = 'Maternal age')
plot(predict(lmod2, type = 'response'), type = 'l',
     ylab = "Probability of Down's syndrome per live birth", 
     xlab = 'Maternal age')

如果你想把两条线都绘制在同一张图上,你可以把第二条线plot()改为lines()

data(mtcars)
lmod <- glm(vs ~ mpg, family = quasibinomial, mtcars)
lmod2 <- glm(vs ~ mpg + I(mpg^2), family = quasibinomial, mtcars)

par(mfrow=c(1,1))
plot(predict(lmod, type = 'response'), type = 'l', 
     col = "red",
     ylab = "Probability of Down's syndrome per live birth",  
     xlab = 'Maternal age')

lines(predict(lmod2, type = 'response'), type = 'l',
      col = "blue",
      ylab = "Probability of Down's syndrome per live birth", 
      xlab = 'Maternal age')

相关问题