Data description:

PRISM provides gridded raster climate data for the continental USA. The data are temporal & spatial.

Accessing PRISM data in r

prism package:

library(devtools) #needed to download prism from github
library(reshape2) ##melting dataframes
library(dplyr) #data wrangling
library(raster) ##working with raster data
library(sp) ##manipulationg spatial data

install_github(repo = "prism", username = "ropensci")
library(prism) ##prism data access

Getting the data

PRISM data is available for multiple temporal scales; annual, monthly, daily, and 30 year normals. Each has a unique function that requires the climatic variable you are interested in (type) and the resolution (4km or 800m).

30-yr normals

Based on data from 1981-2010.

Pull 30 year precipitation normals for July at a 4km grid resolution

##to do
##get dailys for 2016 - ppt, tmean

options(prism.path = "/Volumes/collnell/CAT/data/prism/prism_temp")
get_prism_normals(type = 'tmean', resolution = '4km', mon = 12, keepZip = TRUE)

‘mon’ refers to the month of interest (July). Exchanging ‘mon’ for ‘annual = TRUE’ will download the normals for the whole year.

Look at the downloaded prism files in your directory


Normals data are labelled as PRISM_[climate variable abbreviation]_30yr_normal_[resolution]_[month]_bil

Convert raster data to point data

new_file<-1#this number corresponds to the row of the file of interest
RS <- prism_stack(ls_prism_data()[12,1]) ##raster file of data
proj4string(RS)<-CRS("+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs") ##assign projection info

To convert the raster data to a dataframe:

##convert raster to point data frame
df <- data.frame(rasterToPoints(RS))
m.df <- melt(df, c("x", "y"))
names(m.df)[1:2] <- c("lon", "lat") #rename columns

Raster data is easiest to plot when it is in a dataframe format. Raster format is easier for data manipulate (e.g. crop, change resolution & projection, extract values, aggregate data). I find it easiest to work with the raster data and extract what is needed, and reduce the size. Then, convert to the dataframe format to plot using ggplot2 and ggmap, which have the most flexibility in terms of aestetics.

Save files:

name<-paste0("/Volumes/collnell/CAT/data/prism/monthly_temp/USA/PRISM_tmean_", year,".csv")
write.csv(m.df, name)
writeRaster(RS, name)

Now we have a dataframe with longitude & latitude coordinates for each grid and the associated 30 year normal precipitation values.

##   X       lon      lat                           variable value
## 1 1 -95.12500 49.41667 PRISM_ppt_30yr_normal_4kmM2_07_bil 93.85
## 2 2 -95.16667 49.37500 PRISM_ppt_30yr_normal_4kmM2_07_bil 92.91
## 3 3 -95.12500 49.37500 PRISM_ppt_30yr_normal_4kmM2_07_bil 93.41
## 4 4 -95.08333 49.37500 PRISM_ppt_30yr_normal_4kmM2_07_bil 93.60
## 5 5 -95.04167 49.37500 PRISM_ppt_30yr_normal_4kmM2_07_bil 93.84
## 6 6 -95.00000 49.37500 PRISM_ppt_30yr_normal_4kmM2_07_bil 94.17

PRISM temporal data

Annual, monthly, and daily data follow a similar format using one of the following functions for a designated time period:


Data is available from 1891 through present however all data prior to 1981 are downloaded as a single file.

Monthly data

Download precipitation for the month of July from 1985 to 2016

get_prism_monthlys(type = 'ppt', years=1990:2016, mon = 7, keepZip = TRUE)

new_file<-c(1) ##change to corresponding file numbers
RS <- prism_stack(ls_prism_data()[new_file,1]) ##raster file
to_slice <- grep("_201607",RS[,1],value=T)##search through names

df <- data.frame(rasterToPoints(RS)) ##creates a dataframe of points
month.df <- melt(df, c("x", "y"))
names(month.df)[1:2] <- c("lon", "lat") #rename columns

Filtering data

PRISM datasets are large, easier to use once filtered to the region/time of interest.

Plot PRISM 30 year normals data

library(ggplot2) #plotting
library(ggmap) ##theme_nothing()

  geom_raster(data=m.df, aes(x=lon, y=lat, fill=value))+
  theme_nothing(legend = TRUE)+
  scale_fill_gradient2("Precipitation\n(inches)", low='red',mid='lightblue',high = 'darkslateblue', midpoint=100)+
  labs(title="July Precipitation: 30-year normals")+coord_fixed(ratio=1.3)