简体   繁体   English

将图形标题分配给 R markdown Z78E6221F6393D1356681DB398 中的 html 小部件(vtree 包)

[英]Assign figure caption to html widget (vtree package) in R markdown output

I need to implement a figure caption in a plot that is generated by the vtree package in R markdown.我需要在 plot 中实现一个图形标题,它是由 R Z590366 中的 vtree package 生成的I learned that this is a htmlwidget and figure captions should now be possible for htmlwidgets used in R markdown with install.packages('webshot') and webshot::install_phantomjs() (reference: https://bookdown.org/yihui/bookdown/html-widgets.html#ref-R-DT . But days after I am not really any step further. I did not find any example (show case) for this issue (fig.cap for htmlwidgets in R markdown in the net) so my hope is that someone out there can give me some help, In my iris dataset example. in Fig. 1 the caption is not working in contrast to Fig. 2. I learned that this is a htmlwidget and figure captions should now be possible for htmlwidgets used in R markdown with install.packages('webshot') and webshot::install_phantomjs() (reference: https://bookdown.org/yihui/bookdown /html-widgets.html#ref-R-DT 。但是几天后我真的没有更进一步。我没有找到任何关于这个问题的例子(展示案例)(图.cap for htmlwidgets in R markdown 在网络中)所以我希望有人可以给我一些帮助,在我的虹膜数据集示例中。在图 1 中,与图 2 相比,标题不起作用。

my iris set example RMD file:我的虹膜设置示例 RMD 文件:

YAML YAML

---
title: "test"
author: "TJ"
date: "14 12 2020"
output: html_document
---

code chunk 1: load libraries and data代码块 1:加载库和数据

knitr::opts_chunk$set(echo = TRUE)
library(vtree)
library(webshot)
library(tidyverse)
attach(iris)
df <- iris %>%
  select(Species) %>% 
  cbind(sapply(levels(.$Species), `==`, .$Species))

code chunk 2: Figure 1代码块 2:图 1

{r fig1, echo=FALSE, fig.cap="Vtree plot"}
vtree(iris, "Species")

code chunk 3: Figure 2代码块 3:图 2

{r fig2, echo=FALSE, fig.cap="Scatter plot iris dataset"}
plot(Sepal.Length, Sepal.Width, main="Scatterplot Example",
     xlab="Sepal Length ", ylab="Sepal Width ", pch=19) 

There is a workaround using the Magick package.You save the image as.png using grVizToPNG (make sure you comment this line out before you render your document or put it in a separate chunk with ´{r eval = FALSE}, otherwise you will get an error during rendering:有一个使用 Magick package 的解决方法。您使用grVizToPNG将图像另存为 .png(确保在渲染文档之前注释掉这一行或将其放在单独的块中并使用 '{r eval = FALSE},否则您将渲染时出错:

```{r eval=FALSE, echo = FALSE}

myimage <- vtree(iris, "Species")
saveMyimage <- grVizToPNG(myimage, width=800)
```

Here you use the Magick package:在这里,您使用Magick package:

```{r magick, echo= FALSE}

MyimagePNG <- image_read("myimage.png")
image_annotate(MyimagePNG, "Vtree plot", size = 35, gravity = "southwest")

```

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM