0

下面的代码是一个完整的 .Rmd 文件,它成功地生成了一个带有表面温度 hovmoller 图的 .pdf 文件。(我很抱歉粘贴了整个文件,但它很短,我不确定是什么导致了问题 - 尽管我相信它在代码的最后一部分。)数据文件在这里:https:// crudata.uea.ac.uk/cru/data/temperature/HadCRUT.4.6.0.0.median.nc

当我运行 .Rmd 文件时,它似乎可以完美运行。它会生成一个 230kb 的 .pdf 文件,其中包含一个(相当丰富的)hovmoller 图。但是,如果我将代码放入 .R 文件中(或从 .Rmd 文件中逐行运行),它仍然会生成一个 .pdf 文件,但它是空的,并且如果我尝试打开它会生成一条错误消息.

谁能告诉我为什么会这样?

---
title: "Hovmoller plot - HadCRUT4 through November 2018"
author: "jbtg"
date: "January 1, 2019"
output: html_document
---

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

```{r warning=FALSE,message=FALSE}
library(maptools)
library(ncdf4)
library(raster)
library(rasterVis)
library(RColorBrewer)
library(zoo)
library(sf)
library(rgdal)
```

```{r}
#HADCRUT4 combined air temp/SST anomaly as of November 1, 2018
#read a 3D netCDF dataset
hadcrut4_path <- "C:/Users/jtene/Dropbox/!!!netcdf/Rmarkdown/"
hadcrut4_name <- "HadCRUT.4.6.0.0.median.nov18.nc"
hadcrut4_file <- paste0(hadcrut4_path,hadcrut4_name)
hadcrut4 <- brick(hadcrut4_file) #opens anomaly netCDF file (calls nc_open)
hadcrut4 #gives a summary of the file contents
print(c(filename(hadcrut4), hasValues(hadcrut4), inMemory(hadcrut4)))
```

Set up and produce the Hovmöller plot:

```{r}
#setup
idx <- seq(as.Date('1850-01-01'), as.Date('2018-11-01'), 'month')
idx <- as.yearmon(idx)
tmpplt <- setZ(hadcrut4, idx)
names(tmpplt) <- as.character(idx)

#plot
trellis.device('pdf', file = 'hov_hadcrut4.pdf')
hovmoller(tmpplt, dirXY=y,
      at = do.breaks(c(-3.5,3.5),14),
      contour = F, interpolate = F,
      par.settings=RdBuTheme(region=rev(brewer.pal(9,'RdBu'))),
      main="HadCRUT4 Anomalies 1850-2018 (1961-1990 base period)")
dev.off
```
4

0 回答 0