Brani
Brani

Reputation: 6784

rmarkdown: plot dissapears when grid.raster is inside `if` block

I used the grid::grid.raster command to add a logo below a ggplot, in an Rmd document. It worked fine until I entered the code inside an if block.

In the following example, If I enable the if block, I get the logo ...without the plot ?!?!

---
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'))
#}

Upvotes: 2

Views: 242

Answers (1)

Clemsang
Clemsang

Reputation: 5491

Please use print around your plot to render it.

Upvotes: 2

Related Questions