0

I want to generate a R markdown html document with plots and it should be possible to jump to a certain plot by search-function (in my example there are 3 plots and I want to jump in the html-doc to the plot, where the main is "rivers"). I think, the problem is, that main and axis labels of a plot are grafical elements, like the plot itself, and not text. So the search-function doesn't work.

Of course it would be possible to add manually text before each plot, but as all my plots are generated with a for-loop, I don_t know how to do it.

is there a possibilty to include text-output in this kind of for-loop or are there other ideas, how the main or axis labels of a plot can be suitable for search-function?

thanks in advance!

---
title: "search function test"
author: "Michel Grün"
date: "last edited `r format(Sys.Date(),'%d.%m.%Y')`"
output: 
  html_document:
    df_print: paged
---


knitr::opts_chunk$set(echo = TRUE,warning = FALSE)




df<-data.frame(x=seq(1,20),
               trees=rnorm(20,4,3),
               mountains=rnorm(20,6,3),
               rivers=rnorm(20,4,4))



for(i in 2:length(colnames(df))){
  plot(df$x,df[,i],
       main=colnames(df)[i],
       xlab=colnames(df)[1],
       ylab=colnames(df)[i])
}

solved in another issue: https://stackoverflow.com/a/57034752/16578253

in this issue, the question is slightly different, but a solution shown there was also the solution for my problem. The idea is to create headings + outputs within a loop. As result, in the output dokument every header is followed by a plot and the header is of course suitable for search-function. It's important to use the argument results='asis' in the chunk konfiguration to allow that cat() is interpreted as Markdown syntax. Furthermore the cat()ing tshould be surrounded by some newlines to make sure it's interpreted properly.

michel
  • 33
  • 6

1 Answers1

1

You can combine a svg device with a knitr hook:

---
title: "search function test"
author: "Michel Grün"
date: "last edited `r format(Sys.Date(),'%d.%m.%Y')`"
output: 
  html_document:
  df_print: paged
---

```{r setup}
library(ggplot2)
library(knitr)

# see https://github.com/yihui/knitr/issues/754
local({
  hook_plot <- knit_hooks$get("plot")
  knit_hooks$set(plot = function(x, options) {
    x <- paste(x, collapse = ".")
    if (!grepl("\\.svg", x)) {
      return(hook_plot(x, options))
    }
    # read the content of the svg image and write it out without <?xml ... ?>
    paste(readLines(x)[-1], collapse = "\n")
  })
})

opts_chunk$set(echo = TRUE, warning = FALSE, dev = "svglite")


df <- data.frame(
  x = seq(1, 20),
  trees = rnorm(20, 4, 3),
  mountains = rnorm(20, 6, 3),
  rivers = rnorm(20, 4, 4)
)
```

```{r}

for (i in 2:length(colnames(df))) {
  plot(df$x, df[, i],
    main =paste0(colnames(df)[i], " äöα"),
    xlab = colnames(df)[1],
    ylab = colnames(df)[i]
  )
}
```

enter image description here

danlooo
  • 10,067
  • 2
  • 8
  • 22
  • This works! But can you try to explain, what that part after "local({ " does? Does this have any other consequences for plots? thanks for your help! – michel Feb 23 '22 at 07:41
  • I found out, that now special characters from utf8 (like äöü?or ² ) are no longer displayed correctly; is it possible to include utf8 in the part with "local({" ? – michel Feb 23 '22 at 07:49
  • UTF8 works on my machine (docker container rocker/verse:4.1.2), see updated answer. Right click on selected text in the figure and click on Inspect in your browser. Then you can see the font. It must be installed on the PC or embedded as a webfont in the HTML. Consequences are mostly due to the fact that SVGs are vector graphics, the picture is much sharper, but file size is bigger if the plot has >1000 points. Maybe the font used in `ggplot2` will work for your better. Anotehr option is to set the font using CSS. – danlooo Feb 23 '22 at 08:02
  • ok the selected text in my figure is "arial" ... not an unusual one right? Do you think it would help, if I change the font in the plot command (plot( ... ,font="_______") ? – michel Feb 23 '22 at 08:41
  • Put `options(Encoding="UTF-8")` to the setup chunk before `library` and try again – danlooo Feb 23 '22 at 08:50
  • Even putting options(Encoding="UTF-8") didn't solve the problem. I discovered, that also the font type changes, if I compare mains with and without special utf8 characters :( – michel Feb 23 '22 at 10:59
  • Which Operating system are you using? I'm using Linux and RMarkdown does different things on Windows. – danlooo Feb 23 '22 at 11:00
  • its windows in my case – michel Feb 23 '22 at 11:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242308/discussion-between-michel-and-danlooo). – michel Feb 23 '22 at 11:03