html - 遵循 HTML knit - RMarkdown 包括空白 block

标签 html r knitr raster r-markdown

我一直在使用 Raster 和 RMarkdown 记录一些空间数据的可视化,但我遇到了一个问题,即每个图形上方都有一堆负空间。这是 RMarkdown 代码(稍微简化):

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=12, fig.height=8, echo=FALSE,
                      warning=FALSE, message=FALSE)
```

```{r r-packages}
library(maptools)
library(raster)
library(rgdal)
```
###Description of data
Data are taken from the National Land Cover Database - 2011 and represent land cover at a 30m X 30m resolution.
location of data: [National Land Cover Database - 2011]('http://gisdata.usgs.gov/TDDS/DownloadFile.php?TYPE=nlcd2006&FNAME=nlcd_2006_landcover_2011_edition_2014_10_10.zip')

###Import raster file for US landcover and shapefile for state borders and counties

```{r Import raster file for us landcover}
rfile <- '~/Documents/Data/nlcd_2006_landcover_2011_edition_2014_10_10/nlcd_2006_landcover_2011_edition_2014_10_10.img' #location of raster data
r1 <- raster(rfile)

##Import shapefile for state borders
statepath <- '~/Documents/Data/'
setwd(statepath)
shp1 <- readOGR(".", "states")
##Transform shapefile to fit raster projection
shp1 <- spTransform(shp1, r1@crs)
##Remove hawaii and alasks which are not in raster image
shp1.sub <- c("Hawaii","Alaska")
states.sub <- shp1[!as.character(shp1$STATE_NAME) %in% shp1.sub, ]

##Import county data
#data source: ftp://ftp2.census.gov/geo/tiger/TIGER2011/COUNTY/tl_2011_us_county.zip
countypath <- '~/Documents/Data/tl_2011_us_county'
setwd(countypath)
shp2 <- readOGR(".", "tl_2011_us_county")
##Transform shapefile to fit raster projection
counties <- spTransform(shp2, r1@crs)
counties.sub <- counties[as.character(counties$STATEFP) %in% states.sub$STATE_FIPS, ]
```
Raster plot of US with state and county border overlays
```{r plot landcover with state borders}
#Plot state borders over raster
plot(r1)
plot(counties.sub, border = "darkgrey",lwd=.65,add=T)
plot(states.sub,border = "darkblue",add=T)
```
Raster cropped and masked to extent of California
```{r crop raster to a single state (California)}
shp.sub <- c("California")
shp.ca <- states.sub[as.character(states.sub$STATE_NAME) %in% shp.sub, ]

r1.crop   <- crop(r1, extent(shp.ca))

plot(r1)
```

一切正常,但是当 markdown 输出到 HTML 时,也会包含一堆空白。 [这是已发布的 RPub](现已解决)。 (http://rpubs.com/pbwilliams/80167)。我认为这是一个栅格问题,因为我没有遇到过这个问题,例如,在 ggplot 中。

我已经能够通过缩小图像来临时解决这个问题,但是每当我将图片放大到任何合理的程度时,都会添加额外的空间。如果有人知道如何解决此问题,将不胜感激。

最佳答案

正如评论中所建议的那样,使用 block 选项 fig.keep = 'last' 应该可以解决这个特定问题,因为每个代码块似乎都有两个图,第一个是空白一个(你只想保留最后一个)。

关于html - 遵循 HTML knit - RMarkdown 包括空白 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30355240/

相关文章:

javascript - 从 JavaScript 对象创建 HTML 表格

regex - 删除R中两种字符串模式之间的字母

r - 在R中使用 "foreach()"函数时如何创建进度条?

r - 带有长文本、项目符号和特定表格宽度的表格

html - margin 已调整但未阻止

javascript - 使用 jquery xmlhttp.responsetext 结果无法出现在 div 中

javascript - 如何更改导致折叠/展开 div 的图像按钮

r - 错误消息 : Error in fn(x, ...):已更新的 VtV 不是正定的

r - 将长文本换行到 kable 表列中

r - 在 R Markdown 中为代码块添加换行符