4

I'm trying to plot accumulated precipitation in Brazil for a given period, see code below:

Load packages

library(raincpc)
library(SDMTools)
library(raster)
library(ggplot2)
library(rgdal)
library(maptools)
library(rgeos)
library(mapproj)

Use cpcrain package to obtain data

cpc_get_rawdata(2014, 1, 01, 2014, 1, 08)

rain1 <- cpc_read_rawdata(2014, 1, 01)
rain2 <- cpc_read_rawdata(2014, 1, 02)
rain3 <- cpc_read_rawdata(2014, 1, 03)
rain4 <- cpc_read_rawdata(2014, 1, 04)
rain5 <- cpc_read_rawdata(2014, 1, 05)

rain_tot <- rain1 + rain2 + rain3 + rain4 + rain5

Zoom in Brazil

lon_vals <- seq(275, 330     , 0.5)
lat_vals <- seq(-60, 20      , 0.5)
reg_box <- expand.grid(lon = lon_vals, lat = lat_vals,
stringsAsFactors = FALSE, KEEP.OUT.ATTRS = FALSE)
reg_box$rain <- extract.data(reg_box, rain_tot)
reg_box$rain_chunks <- cut(reg_box$rain, breaks = c(0, 80, 160, 240, 320), include.lowest = TRUE)

Use ggplot for visualization

gfx_gg <- 
  ggplot(data = reg_box) +
              geom_raster(aes(lon, lat, fill = rain_chunks)) +
              scale_fill_manual(values = c("lightgrey", "red", "green", "blue")) +
              theme(axis.text = element_blank()) + # , axis.ticks = element_blank() 
              labs( x = NULL, y = NULL, fill = " Precipitation (mm)") +
              ggtitle("Rainfall over South America for selected period")

which, as expected, yields:

enter image description here

Add state boundaries

brazil1 <- getData("GADM", country = "BRA", level = 1)

gfx_gg +
  geom_polygon(data = brazil1, aes(x = long, y = lat, group = group),
               color = "black", show_guide = FALSE, fill=NA) +
  labs(x = "x", y = "y") +
  theme_bw()

But I'm getting an unexpected error:

enter image description here

How can I fix this (projection?) error?

2

I believe what you are experiencing is more or less a copy of this question.

The coordinates in the rainfall data are in longitude/latitude, but with values ranging from 0 to 360, instead of -180 to 180 (as your political boundaries are). See the GPCC spatial note here (emphasis mine):

Spatial Coverage:

  • 0.5 degree latitude x 0.5 degree longitude global grid (720x180)

  • 1.0 degree latitude x 1.0 degree longitude global grid (360x180)

  • 2.5 degree latitude x 2.5 degree longitude global grid (144x72)

  • 90.0N - 90.0S, 0.0E - 360.0E

This blog from the above referenced answer explains how to use gdalwarp to convert the data to the standard -180, 180 coordinates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.