19

I have working on journaling the visualization of some spatial data using Raster and RMarkdown, but am having a problem with there being a bunch of negative space above each figure. Here is the RMarkdown code (somewhat simplified):

```{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)
```

Everything runs fine, but when the markdown is output to HTML, a bunch of white space is included as well. [Here's the published RPub] (now solved). (http://rpubs.com/pbwilliams/80167). I think this is a Raster problem, as I haven't had this issue with figures, for example, in ggplot.

I have been able to temporarily fix this by shrinking the image down, but anytime I enlarge the picture to anything reasonable, the extra space is added. If anyone knows how to fix this, it would be greatly appreciated.

Patrick Williams
  • 694
  • 8
  • 22
  • 4
    I don't know the root cause yet, but I guess using the chunk option `fig.keep = 'last'` should fix this particular problem, since each code chunk seems to have two plots, and the first one is a blank one (you only want to keep the last one). – Yihui Xie May 20 '15 at 22:34
  • 1
    @PatrickWilliams It might be worthwhile to provide Yihui's comment as a solution, and then accept it. Otherwise, it looks like this question is still unanswered when searching through StackOverflow. – Mike Williamson Nov 24 '15 at 06:11
  • @MikeWilliamson thanks. I was unaware I could do that. – Patrick Williams Nov 24 '15 at 06:14

1 Answers1

3

As suggested in the comments, using the chunk option fig.keep = 'last' should fix this particular problem, since each code chunk seems to have two plots, and the first one is a blank one (you only want to keep the last one).

Patrick Williams
  • 694
  • 8
  • 22