R脚本图但Rmarkdown没有

时间:2016-11-15 23:29:15

标签: r rstudio r-markdown

我试图在Rmarkdown做练习。我试图这样做:

regresion <- lm(sbp ~ bmi, data = framingham)
plot(framingham$sbp, framingham$bmi, xlab = "SBP", ylab = "BMI")
abline (regresion)

当我执行此块时,绘图不显示在RStudio的Plots面板内,然后 abline 显示:

Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : plot.new has not been called yet

但是,如果我尝试在RStudio中的简单R脚本中执行绘图:

plot(c(-2,3), c(-1,5), type = "n", xlab = "x", ylab = "y", asp = 1)
abline(h = 0, v = 0, col = "gray60")

在情节面板中显示正常。

我完全被封锁了。你可以帮帮我吗?提前致谢

2 个答案:

答案 0 :(得分:1)

您在plot命令中交换了x和y变量,它应该是plot(x,y)而不是plot(y,x)

以下是最低工作示例:

```{r, echo=FALSE}
regression <- lm(qsec ~ hp, data = mtcars)
plot(mtcars$hp, mtcars$qsec, xlab = "horsepower", ylab = "1/4 mile time")
abline(regression)
```

答案 1 :(得分:0)

而不是测试

plot(c(-2,3), c(-1,5), type = "n", xlab = "x", ylab = "y", asp = 1)

你应该测试

plot(framingham$sbp, framingham$bmi, xlab = "SBP", ylab = "BMI")

看这个情节是否正常。很可能你的问题就在这里 - 也许framingham不是数据框,或类似的东西。