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

Producing plots with R Markdown when “knitting” to html

更新:我解决了一些愚蠢的问题。我必须在代码块与图之前包含一个行空间,然后它按我希望的那样工作。感谢您抽出宝贵的时间来回答这个问题。

我有一个Markdown文档,当我"编织"为Word或pdf时,它可以正常工作。但是,当我"编织"到html时,没有任何图出现。在堆栈溢出上也有类似的问题,但是我找不到能重复出现的问题

这是我试图在代码块中使用的指令(它不起作用):

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

但是,当我将指令设置为此时,我得到了图:

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

问题是,我不想在html文档中生成代码,而仅是情节。

我尝试了如下所示的"股票"降价示例,但该示例也未出现在html文档中:

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

这是YAML信息:

1
2
3
4
5
6
---
title:"HTML TEST"
author:"Pete Miksza"
date:"12/12/2017"
output: html_document
---

这是不可复制的,但是这是我的实际情节的代码块:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
```{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 ="\
Valence \
(- negative to + positive)", y ="\
Arousal \
(- 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"))
```

我没有收到任何警告消息,但这包含在控制台的markdown输出中:

1
/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'


作为一种快速的临时解决方案,可以将绘图另存为.jpg文件,然后将图像读取到RMarkdown文件中。

这是一个例子:

1
2
3
4
5
6
7
8
```{r, echo=FALSE}
library(jpeg)

img <- readJPEG('cat.jpeg')

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

enter image description here
该代码块将生成没有代码的图像。 同样,这不是一个永久性的解决方案,但是如果您有最后期限,这可能会起作用。

另外,尝试散列ggplot对象中的一些辅助代码(例如labstheme)并编织降价促销,看看是否有任何变化