Rmarkdown编织HTML-图形未显示

时间:2018-08-02 21:30:32

标签: r ggplot2

我有以下几行代码:

ggplot(data = subset(gapminder1, year %in% c(seq(1900, 1990, by = 10))), aes(x = year, y = lifeExp)) + geom_boxplot(aes(group = year))
ggplot(subset(gapminder1,country=="United States"),aes(x = year, y = lifeExp)) + geom_line()
ggplot(subset(gapminder1,country %in% c("China","India","United States","Indonesia","Brazil")) , aes(x = year, y = lifeExp)) + geom_line(aes(color=country))

运行代码时,该图在rmd文件中显示正常。但是,当我编织文档时,图形不会显示(出现空白图形)。谁能告诉我该怎么办?

1 个答案:

答案 0 :(得分:0)

请参阅下面的代码,将其编入HTML文件:

---
title: "check"
author: "Artem"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

```

## R Markdown
Check graph

```{r gapminder, echo=FALSE}
gapminder <- read.csv("https://raw.githubusercontent.com/birdsarah/pydata-nc/master/tutorial/assets/gapminder.csv", header = TRUE)
gapminder1 <- setNames(gapminder, c(c("country", "year", "lifeExp", "population", "income", "region"
)))

library(ggplot2)

gpm_sub <- subset(gapminder1, country %in% c("China","India","United States","Indonesia","Brazil"))

g1 <- ggplot(gpm_sub, aes(x = year, y = lifeExp)) +
  geom_line(aes(color=country))


gpm_sub_us <- subset(gapminder1,country=="United States")
g2 <- ggplot(gpm_sub_us,aes(x = year, y = lifeExp)) +
  geom_line()

g3 <- ggplot(gpm_sub, aes(x = year, y = lifeExp)) + 
  geom_line(aes(color=country))

library(gridExtra)
grid.arrange(g1, g2, g3, nrow = 2)

```

输出(没有问题)。

enter image description here