繁体   English   中英

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

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

我需要在 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 。但是几天后我真的没有更进一步。我没有找到任何关于这个问题的例子(展示案例)(图.cap for htmlwidgets in R markdown 在网络中)所以我希望有人可以给我一些帮助,在我的虹膜数据集示例中。在图 1 中,与图 2 相比,标题不起作用。

我的虹膜设置示例 RMD 文件:

YAML

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

代码块 1:加载库和数据

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

代码块 2:图 1

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

代码块 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) 

有一个使用 Magick package 的解决方法。您使用grVizToPNG将图像另存为 .png(确保在渲染文档之前注释掉这一行或将其放在单独的块中并使用 '{r eval = FALSE},否则您将渲染时出错:

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

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

在这里,您使用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