简体   繁体   English

“编织”到html时使用R Markdown生成图

[英]Producing plots with R Markdown when “knitting” to html

UPDATE: I solved this problem with something silly. 更新:我解决了一些愚蠢的问题。 I had to include a line space prior to the code chunks with the plots, then it worked as I'd hoped. 我必须在代码块与图之前包含一个行空间,然后按我希望的那样工作。 Thank you for your time with this question. 感谢您抽出宝贵的时间回答这个问题。

I have a markdown document that is working perfectly when I "knit" to Word or pdf. 我有一个Markdown文档,当我“编织”为Word或pdf时,它可以正常工作。 However, when I "knit" to html none of the plots appear. 但是,当我“编织”到html时,没有任何图出现。 There are similar questions on stack overflow, but nothing that I could find that duplicates the issue 在堆栈溢出上也有类似的问题,但是我找不到能重复出现的问题

This is the instruction I am trying to use in the code chunk (it doesn't work): 这是我试图在代码块中使用的指令(它不起作用):

{r, emotional circumplex, fig.align = "center", echo=FALSE}

However, when I set the instruction to this, I get the plot: 但是,当我将指令设置为此时,我得到了图:

{r, emotional circumplex, fig.align = "center", echo=TRUE}

The problem is, I don't want to produce the code in the html document, just the plot. 问题是,我不想在html文档中生成代码,而仅是情节。

I tried the "stock" markdown example, presented below, but that didn't appear in an html document either: 我尝试了下面显示的“股票”减价示例,但该示例也未出现在html文档中:

```{r pressure, echo=FALSE}
plot(pressure)
```

This is the YAML info: 这是YAML信息:

---
title: "HTML TEST"
author: "Pete Miksza"
date: "12/12/2017"
output: html_document
---

It's not reproducible, but here's the code chunk for my actual plot: 这是不可复制的,但是这是我的实际情节的代码块:

```{r, emotional circumplex, fig.align = "center", echo=FALSE}
# Emotional Circumplex
ggplot(charlie_brown_dat, aes(x = scale(energy), y = scale(valence))) + 
  geom_point(aes(size = Tempo, shape = PopularitySplit), color = "darkgreen") +
  geom_hline(yintercept = 0, color = "red") +
  geom_vline(xintercept = 0, color = "red") +
  geom_label_repel(aes(label = track_name), size = 1.7, point.padding = .75) +
  scale_x_continuous(limits = c(-2.5, 2.5)) +
  scale_y_continuous(limits = c(-2.5, 2.5)) +
  labs(title = "Where do the Tracks Sit on an Emotion Circumplex?", 
       x = "\nValence \n(- negative to + positive)", y = "\nArousal \n(- low energy to + high energy)") +  
  theme(axis.text.x = element_text(size = 8),
        axis.text.y = element_text(size = 8),
        axis.title.x = element_text(size = 9, face = "italic"),
        axis.title.y = element_text(size = 9, face = "italic"),
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        plot.title = element_text(size = 11, face = "bold"),
        legend.title = element_text(size = 8),
        legend.key.size =  unit(.05, "in"),
        legend.text = element_text(size = 6.5),
        panel.background = element_rect(fill = "white"),
        panel.grid.major = element_line(color = "lightgray"))
```

I receive no warning messages, but this is included in the markdown output in my console: 我没有收到任何警告消息,但这包含在控制台的markdown输出中:

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS Music_from_a_Charlie_Brown_Christmas.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output Music_from_a_Charlie_Brown_Christmas.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /Users/pmiksza/Library/R/3.4/library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable 'theme:bootstrap' --include-in-header /var/folders/pw/1hpqdrzn5853ys3whns5mg34vh62tx/T//RtmpOt8rhU/rmarkdown-str1dc15577eea.html --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' 

As a quick temporary fix it is possible to save your plots as .jpg files and then read the images into the RMarkdown file. 作为一种快速的临时解决方案,可以将绘图另存为.jpg文件,然后将图像读取到RMarkdown文件中。

Here's an example: 这是一个例子:

```{r, echo=FALSE}
library(jpeg)

img <- readJPEG('cat.jpeg')

plot(1:2, type='n')
graphics::rasterImage(img,1,1,2,2)
```

在此处输入图片说明 This code chunk will produce the image without the code. 该代码块将生成没有代码的图像。 Again not a permanent solution but if you have a deadline this might work. 同样,这不是一个永久性的解决方案,但是如果您有最后期限,则可能会起作用。

Also, try to hash out some of the auxiliary code in your ggplot object (such as the labs and the theme ) and knit the markdown, and see if anything changes 另外,尝试散列ggplot对象中的一些辅助代码(例如labstheme )并编织markdown,看看是否有任何变化

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

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