how to color a SpatialLinesDataFrame according to some given value (Rainbow)

grid <- raster(extent(FRA))
# Choose its resolution. I will use 2 degrees of latitude and longitude.
res(grid) <- 0.3
# Transform this raster into a polygon and you will have a grid, but gwithout Brazil (or your own shapefile).
spdf <- rasterToPolygons(grid)
sldf <- as( spdf , "SpatialLinesDataFrame") # Create a lines object. This gives you the borders of the polygons

I have a set of data that I interpolate that I get a raster like this:


#json data
dataJson <- fromJSON(readLines('/Users/macbook/Documents/mesdocument/exempleR/jsonallcoor.js'))
# shp
#contour (interpolation idw)
for ( i in 1:103) { 
  x[i]<- as.data.frame.factor(dataJson[[i]]$longitude)# define x & y as longitude and latitude
  y[i]<- as.data.frame.factor(dataJson[[i]]$latitude)
  t[i]<- as.data.frame.factor(dataJson[[i]]$temperature)

x<- as.numeric(x) 
y <- as.numeric(y) 
temperature<- as.numeric(t)
frame.xy = frame[c("x", "y")]
coordinates(frame.xy ) <- ~x+y
#Define the grid extent
x.range <- as.numeric(c(-4.445833,9.484722))  # min/max longitude of the interpolation area
y.range <- as.numeric(c(40.50306,51.2))  # min/max latitude of the interpolation area

#Create a data frame from all combinations of the supplied vectors or factors. See the description of the return value for precise details of the way this is done. Set spatial coordinates to create a Spatial object. Assign gridded structure:
grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 0.1), 
                   y = seq(from = y.range[1], to = y.range[2], by = 0.1)) 
# expand points to grid
coordinates(grd) <- ~x + y
#to pixel
gridded(grd) <- TRUE
#les lon et lat
points(frame.xy, pch =1, col = "black", cex = 0.1)
#Interpolate surface and fix the output
idw <- idw(formula = temperature ~ 1, locations = frame.xy, 
           newdata = grd)  # apply idw model for the data
residual_grid = raster(idw, "var1.pred")
proj4string(residual_grid) <- CRS("+init=epsg:4326") # OSM Mercator projection

# Create an empty raster 
grid <- raster(extent(FRA))

# Choose its resolution. I will use 2 degrees of latitude and longitude.
res(grid) <- 0.2

# Make the grid have the same coordinate reference system (CRS) as the shapefile.
gridpolygon <- rasterToPolygons(grid)
# Initialisation 
m <- leaflet()
#m <- addTiles(leaflet())
m <- fitBounds(map = m, 
               lng1 = -4.445833,
               lat1 = 40.50306,
               lng2 = 9.484722, 
               lat2 =  50.56417)
pal <- colorNumeric(c("Spectral"), values(residual_grid),
                    na.color = "transparent")

m<-addRasterImage(map=m,residual_grid, colors = pal, opacity = 0.8)
m<-addLegend(map=m,pal = pal, values = values(residual_grid),
             title = "pression")
m<-addPolygons(map=m,data =gridpolygon,fillOpacity=1,opacity=1,smoothFactor = 1,fillColor = "transparent",

## Dimensions de la carte
m$width <- 1000
m$height <-700

saveWidget(m, '/Users/macbook/Documents/maps/contour.html', selfcontained = FALSE)

result like this: enter image description here Now I want spatial lines (grid) take raster colors. For that I searched and I found this Example I applied like this:

sldf <- as( gridpolygon , "SpatialLinesDataFrame") # Create a lines object. This gives you the borders of the polygons
ra <- rasterize( sldf ,residual_grid ) 

result: enter image description here

closed as unclear what you're asking by whyzar, xunilk, ArMoraer, BERA, aldo_tapia Apr 23 '18 at 14:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    What are you trying to do? Just plot a SpatialLinesDataFrame in some colour scheme? Why do all that raster processing? What's that got to do with the title? – Spacedman Apr 24 '16 at 20:00
  • thanks for reply , I have to go through it to get the 'grid' (for another need), I want to color my grid according to some value – zina_GIS Apr 24 '16 at 22:03
  • I try to do exactly example in stackoverflow.com/questions/15092358/… for my grid – zina_GIS Apr 24 '16 at 22:08
  • 1
    Please use the edit button beneath your question to revise it with any requested clarifications. I notice that you have not yet taken the 2-minute Tour that is designed to introduce you to this site's protocols. – PolyGeo Apr 24 '16 at 22:42
  • 1
    This q is perfectly clear IMO - the default sp plotting is very limited for thematic formatting - you can pass in cols to plot with the "col" argument, but you'll need to map the colour you to each rowin the spdf on your own. But, since the borders of each pixel are collinear you will get a complex pattern of colours based on incremental overlapping borders. Consider ggplot2::fortify(spdf) to go down another alternative for plotting. Make a new question or modify this one with more details about how you want the lines to be formatted. – mdsumner Apr 25 '16 at 0:44

Are you trying to do something as simple as this?

> FRAl = as(FRA,"SpatialLines")
> FRAp = as(FRAl, "SpatialPoints")
> FRAr = rasterize(FRAp, residual_grid, mask=TRUE)
> plot(FRAr)

enter image description here

The conversion to points before rasterisation should be unneccesary, I think rasterize(FRAl,....) should work, but is very slow on my machine. So slow I was too impatient to wait for it to finish. FRA has a lot of vertices which is probably why its (a) slow and (b) points work pretty well.

Note: please try and minimise your question. All that leaflet stuff is confusing and unnecessary. Also, all the idw calculation is unnecessary. Just create a raster with some random data and use that for examples rather than forcing us to download data sets and do all that setup code you've put in which is irrelevant to your actual problem, if I've understood correctly what your actual problem is...

  • thanks Spacedman, but I want rastariser grid lines (in the map) and not the outline of country – zina_GIS Apr 25 '16 at 14:12
  • sorry, maybe I was not clear enough, I try to edit my question again – zina_GIS Apr 25 '16 at 14:14
  • I want the grid takes the raster color values – zina_GIS Apr 25 '16 at 14:15
  • 2
    Which grid? Which raster? You've got so many grids and rasters and polygons its just not clear. Why do you want the grid to take the raster values, why can't you just use the raster? Plus, rasters don't intrinsically have colours, they have values, possibly several, at each pixel. These are mapped to colours in some way when the thing is plotted. – Spacedman Apr 25 '16 at 14:29
  • hello, I would like to display more than one variable on the same map that's why I think working with the texture but apparently it does not work, thank you for your efforts.. – zina_GIS Apr 26 '16 at 7:40

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