在rmarkdown的文本中打印ggplot2形状

请考虑以下内容:

对于使用bookdown(HTML和PDF)编写的报告,我想引用在ggplot2图形中使用的形状。在rmarkdown中,可以通过调用object内联引用`r object`或R代码。


问题:我们如何“打印” ggplot2 shape内联?


示例
下图描述了cyl数据集中变量mtcars的标准箱线图。描述数据均值不是标准,但很高兴。此处,平均值使用shape数字23添加到箱线图中。

library(ggplot2)
ggplot(data = mtcars,aes(x = cyl,y = disp,group = cyl)) +
        geom_boxplot() +
        stat_summary(fun.y = mean,geom = "point",shape = 23,size = 3) +
        facet_wrap(~ cyl,scales = "free")

在rmarkdown的文本中打印ggplot2形状

reprex package(v0.3.0)于2019-11-20创建

在我的文字中,我想通过编写类似以下内容的内容来显式引用此shape内联:

`r print(shape == 23)`是指圆柱[...]的平均值。

ieboy2001 回答:在rmarkdown的文本中打印ggplot2形状

您可以使用unicode来表示文本中的形状:

`r knitr::asis_output("\U20DF ")` refers to the mean of cyl [...]

rmarkdown render

编辑: 我应该注意,如果您使用Rstudio并编织为HTML,则可以将unicode符号直接粘贴到rmd文件中(这可能比我发布的原始答案更有意义)。

◇ refers to the mean of cyl [...]

如果要编织成pdf,它应该可以工作,但里程数可能会因操作系统和乳胶安装而异。

本文链接:https://www.f2er.com/3067414.html

大家都在问