rmarkdown:当grid.raster在`if`块中时,绘图消失

时间:2019-03-04 14:04:41

标签: r ggplot2 r-markdown

我使用了grid::grid.raster命令在Rmd文档中的ggplot下添加了徽标。直到我在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'))
#}

1 个答案:

答案 0 :(得分:2)

请在print周围使用plot进行渲染。