rmapsr-spr-maptools

r sp/maps/maptools libraries: Map of annual solar irradiation in R


I am trying to plot annual solar irradiation data with different color on a world map. I am using R programming for the purpose.

The data I am using is from http://eosweb.larc.nasa.gov/sse/global/text/global_radiation

First, I am converting this data to a spatial dataframe and also the world map that is obtained using map(world) function to a spatial form.

I am following an example for practice from https://www.r-bloggers.com/maps-of-solar-radiation/

for plotting my code is as follows. I believe the code is completely correct but I am getting some errors

library(sp)
library(maps)
library(mapdata)
library(maptools)
library(RColorBrewer)

nasafile <- 'http://eosweb.larc.nasa.gov/sse/global/text/global_radiation'
nasa <- read.table(file=nasafile, skip=13, header=TRUE)

proj <- CRS('+proj=latlon +ellps=WGS84')
coords = nasa[,2:1]
datos = nasa[,3:15]
nasaSP <- SpatialPixelsDataFrame(points=coords, data=datos, proj4string=proj)

world <- map("world", plot=FALSE)

llCRS <- CRS("+proj=latlong +ellps=WGS84")
world_sp <- map2SpatialLines(world, proj4string=llCRS)

colar_sp = colorRampPalette(c("snow1", "thistle1", "plum2", "red4"))


map_sp <- list('sp.lines', world_sp)
spplot(nasaSP["Ann"], col.regions=color_sp, sp.layout=map_sp)

The main errors I'm getting are related to projections. I believe the way I have added them in code is correct and have seen many examples in which they have been written the same way. Please have a look at these errors and lemme know how can I make this code work.

Thanks in Advance!

Errors:

Error in CRS("+proj=latlon +ellps=WGS84") : 
  northings must follow eastings: +proj=latlon +ellps=WGS84

Error in checkSlotAssignment(object, name, value) : 
  assignment of an object of class “function” is not valid for slot ‘proj4string’ in an object of class “Spatial”; is(value, "CRS") is not TRUE

Error in CRS("+proj=latlong +ellps=WGS84") : 
  northings must follow eastings: +proj=latlong +ellps=WGS84

Error in initialize(value, ...) : object 'llCRS' not found

Solution

  • I found two problems with your code:

    1) you're using CRS wrongly. In CRS("+proj=latlong...) instead of latlong you must use longlat (see ?CRS);

    2) When you plot your map, your color_sp is called colar_sp.


    Here is your code with the corrections:

    library(sp)
    library(maps)
    library(mapdata)
    library(maptools)
    library(RColorBrewer)
    
    nasafile <- 'http://eosweb.larc.nasa.gov/sse/global/text/global_radiation'
    nasa <- read.table(file=nasafile, skip=13, header=TRUE)
    
    proj <- CRS("+proj=longlat + datum=WGS84")
    coords = nasa[,2:1]
    datos = nasa[,3:15]
    nasaSP <- SpatialPixelsDataFrame(points=coords, data=datos, proj4string=proj)
    
    world <- map("world", plot=FALSE)
    
    llCRS <- CRS("+proj=longlat + datum=WGS84")
    world_sp <- map2SpatialLines(world, proj4string=llCRS)
    
    color_sp = colorRampPalette(c("snow1", "thistle1", "plum2", "red4"))
    
    
    map_sp <- list('sp.lines', world_sp)
    spplot(nasaSP["Ann"], col.regions=color_sp, sp.layout=map_sp)
    

    enter image description here