【问题标题】:Histogram in bookdown document appears two timesbookdown 文档中的直方图出现两次
【发布时间】:2021-05-24 03:14:34
【问题描述】:

当我在包含以下代码段的文档上执行bookdown::render_book(...)

#```{r eval=TRUE, include=TRUE, echo=FALSE}  # only that you can see my chunk options (it's of course not commented out)

# My data for a histogram
Distance = rnorm(n = 1000, mean = 5, sd = 0.5)

# Creating the histogram
hist(Distance,
    freq=FALSE,
    xlim = c(0, 9),
    ylim = c(0, max(hist(Distance)$density + 0.2)),
    xlab = "Distance [mm]",
    ylab = "Density",
    main = "Histogram")
abline(v=5.5,lwd=2,lty=2,col="red")

#```

我得到这个输出

就像代码建议的那样,第一个直方图不应该出现 - y 轴上有 Frequency 的直方图。

问题:我应该怎么做才能避免出现第一个直方图?!

提前致谢!

【问题讨论】:

    标签: r r-markdown markdown bookdown


    【解决方案1】:

    问题在于您调用距离直方图的ylim = c(0, max(hist(Distance)$density + 0.2)), 行。这会在返回您需要的值作为实际绘图的参数之前绘制距离的直方图。

    如果您想使用直方图的属性作为参数,您应该在绘制真实直方图之前将直方图存储在一个变量中,然后直接引用它。

    所以,在做任何其他事情之前,请使用h = hist(Distance)

    然后,您可以将 ylim 规范更改为:ylim = c(0, max(h$density + 0.2)),

    那么应该只有一个直方图。

    【讨论】:

    • 就像上面的评论者猜测的那样。要获得没有情节,您必须将 h = hist(Distance) 添加到带有选项 {r eval=TRUE, include=FALSE, echo=FALSE} 的新代码块中 - 将 include 设置为 FALSE。
    猜你喜欢
    • 2021-02-25
    • 2014-02-20
    • 1970-01-01
    • 2011-06-04
    • 2022-11-23
    • 2023-03-05
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多