在rmarkdown的hist函数中使用add = TRUE时出错

时间:2018-07-27 05:32:42

标签: r r-markdown

我想用hist函数创建一个重叠的相对频率表。 我在r脚本中运行以下代码,它可以工作,但是当我在markdown中运行它时,会给我一个错误

with(newtable,hist(weight[Habit == 'NonSmoker'],prob = TRUE, density =  20, col = 'red',xlab = 'Weight(in ounces)', main = 'Histogram of weight by Smoking Status' , xlim = range(weight), ylim = c(0,0.03)))
with(newtable,hist(weight[Habit == 'Smoker'],prob = TRUE, density = 30, col = 'blue', breaks = 20, add = TRUE))

错误

Error in segments(lx1, ly1, lx2, ly2, ...) : plot.new has not been called yet

有人可以告诉我如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我无法复制您的问题。

下面的最小RMarkdown示例不会产生任何错误。

---
title: "Untitled"
output:
  html_document: default
---

```{r}
breaks <- seq(16, 34, by = 2)
with(mtcars, hist(mpg[cyl == 6], col = rgb(1,0,0,0.5), breaks = breaks))
with(mtcars, hist(mpg[cyl == 4], col = rgb(0,0,1,0.5), breaks = breaks, add = T))
```

输出

enter image description here