首页 > 解决方案 > rmarkdown:当 grid.raster 在 `if` 块内时,绘图消失

问题描述

我使用该grid::grid.raster命令在文档中的 ggplot 下方添加徽标Rmd。直到我在一个if块中输入代码之前它工作得很好。

在下面的示例中,如果我启用该if块,我会得到徽标......没有情节?!?!

---
title: "testing raster logo"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
```

## R Markdown

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r echo=FALSE, warning=FALSE, message=FALSE, fig.width=10}

library(hrbrthemes)
library(magick)

logo <- image_read("https://i.pinimg.com/originals/2b/d4/79/2bd4796ef4d27df0eea97f88ee18fac6.png")

#if (nrow(iris)>0){

    ggplot(iris, aes(x = Petal.Width, y = Petal.Length, color = Species)) + 
    geom_point() +
    labs(title = 'Some plot',
         subtitle = 'Petal.Width vs Petal.Length',
         x = 'This axis title intentionally left blank',
         y = 'This axis title intentionally left blank',
         caption = 'iris data') +

scale_color_manual(name = '',
                              labels = c('Black', 'Red', 'Gray'),
                              values = c('#000000', '#EC0108', '#ACAEAD')) +
    theme_ipsum() +
    theme(plot.title = element_text(color = "#EC0108"),
          plot.caption = element_text(color = "#EC0108", face = 'bold'))



grid::grid.raster(logo, x = 0.09, y = 0.005, just = c('left', 'bottom'), width = unit(1.75, 'cm'))
#}

标签: rggplot2r-markdown

解决方案


print在您周围使用plot来渲染它。


推荐阅读