[英]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.