将 RMarkdown 编织到 HTML 时出错(HTML 中没有显示图表)

Errors when knitting RMarkdown to HTML (no graphs showing up in the HTML)

关于编织RMarkdown的问题

我在编写 Rmarkdown 文件到 HTML/pdf 时遇到问题。当我 运行 我在 Rmarkdown 文件中的代码块时,一切都 运行 很顺利(我得到了我的图表用 ggplot)但是当编织时我得到一个没有图表和错误的输出(eval 错误, ggplot 错误,打印错误)。 有人有这方面的经验吗?

错误:

eval(lhs, parent) 错误:找不到对象‘iso3166’

ggplot(inci_100k, aes(long, lat, map.id=mapname,fill=inci) 错误:找不到对象'inci_100k'

打印错误(INCIPLOT):未找到对象“INCIPLOT”

代码:

---
title: "R Markdown MAP"
author: "Alexandra V"
date: "1/4/2020"
output:
  html_document: default
  pdf_document: default
  word_document: default
---
```{r,echo = FALSE, warning = FALSE, message=FALSE, error=TRUE}
knitr::opts_chunk$set(cache=TRUE)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(error = TRUE)
```

Loading the packages we will need for the following analysis.
```{r echo=FALSE, warning=FALSE}
library(tidyverse)
library(lubridate)
library(ggmap)
library(countrycode)
library(grid)
library(gridExtra)
```

To only keep the data needed to make a worldmap of TB incidences, only the relevant data will be taken from the TB_burden_countries_2020-01-04.csv file. Column 1: country names, column 3: iso3 (country codes), column 6: years, column 8: e_inc_100k (estimated incidence all TB forms per 100.000). To make the file easier to work with the names of the columns will be changed to: country, code, year and inci respectively. 

```{r, message=FALSE}
TB.burden <- read.csv("TB_burden_countries_2020-01-04.csv")
TBworldINC.map <- as.data.frame(TB.burden[,c(1,3,6,8)], drop=false)
write.csv(TBworldINC.map, 'TBworldINC.map.csv', row.names = FALSE) 
tb.INC <- read_csv("TBworldINC.map.csv") %>%
  setNames(c("country", "code", "year", "inci"))
```

```{r}
world <- map_data("world")
tb_some_years <- tb.INC %>%
  filter(year %in% c(2005, 2010, 2015, 2018))
inci_100k <- tb_some_years %>%
  inner_join(iso3166 %>% select(a3, mapname), by = c(code = "a3")) %>%
  left_join(world, by = c(country = "region"))

INCIPLOT <- ggplot(inci_100k, aes(long, lat, map_id = mapname, 
                                                fill = inci)) +
  geom_map(map = world) +
  scale_fill_gradient(low = "blue", high = "yellow") +
  theme_void() +
  coord_map(xlim = c(-180, 180)) +
  labs(fill = "Incidence per year") +
  facet_wrap(~ year, ncol = 2)
print(INCIPLOT)
```

picture of the output I get in Rstudio

我在 R 中制作地图时遇到了类似的问题。一种解决方法是创建图表并将其保存在本地并包括图像。在 R Markdown 中添加图片的语法是 ![alt text](path to image)