2

I have a raster with values ranging from 1 to 5 like the following:

library(raster)
library(rasterVis)
library(viridis)

my_class <- raster(x=matrix(sample(x = 1:5, size = 12, replace=TRUE),4,3),
               xmn=7, xmx=10, ymn=-12, ymx=-8,
               crs=CRS("+init=epsg:4326"))
levelplot(my_class,col.regions=viridis,at=seq(1, 5,len=6))

I want to plot my raster highlighting only the areas with values 2 and 3 and keep the rest shaded, something like the first example map in this question (value by alpha). Also, I want to have lat/lon on my axes.

Is there a way to do this in R?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
geo_dd
  • 604
  • 9
  • 23

1 Answers1

1

I'm guessing from context that the missing packages to make your code work are

library(raster)
library(rasterVis)
library(viridis)

The second question

Also, I want to have lat/lon on my axis.

is rather easy to answer: If your raster has the proper spatial reference, you will have lat/lon on your axis automatically. Here's an example that assumes that your raster is located between 7 and 10 degrees East and 8 and 12 degrees South, with a cell size of 1x1 degrees in an unprojected geographic coordinate reference system (EPSG 4326 = plain WGS84):

my_class <- raster(x=matrix(sample(x = 1:5, size = 12, replace=TRUE),4,3),
                   xmn=7, xmx=10, ymn=-12, ymx=-8,
                   crs=CRS("+init=epsg:4326"))

levelplot(my_class, col.regions=viridis, at=seq(1, 5, len=6))

The main question is not quite clear to me (I should probably put that into a comment but I don't have enough reputation here yet …): The figure you use as an example has two layers of data, one is represented though the hue (color, purple-to-orange gradient) and the other is represented through the overlay of a shadow (basically black with varying degrees of transparency a.k.a. "alpha channel") on top of that. Your example on the other hand has only one layer of data, so I'm wondering what would govern the varying degree of shading?

Edit: based on the explanation below, I think it might be best to manually specify the colors for certain values. You can use viridis as a starting point (or get a nice palette from colorbrewer2.org). Calling that function directly gives you n colors in html notation:

> viridis(n = 5)
[1] "#440154FF" "#3B528BFF" "#21908CFF" "#5DC863FF" "#FDE725FF"

You could store them in a variable and use that to specify the colors you want in your plotting function:

cols <- viridis(n = 5)
levelplot(my_class, col.regions=cols, at=seq(1, 5, len=6))

But you could also edit them first – most graphics programs can help you with that, or you use a dedicated color picker program (there are plenty free options, I'm using Gpick). With that you could for example de-saturate the colors for 1, 4, and 5, leaving the ones for 2 and 3 untouched:

cols <- c("#503E54FF", "#3B528BFF", "#21908CFF", "#9BC89EFF", "#FDF5ABFF")
levelplot(my_class, col.regions=cols, at=seq(1, 5, len=6))

Doesn't quite look nice yet, so you'd have to play around with the colors a bit. Using a more saturated palette to start with would probably bring make the effect more obvious.

  • the code works on my PC because I have the required libraries which I forgot to mention, so thank you for the notice. I have never done any similar plotting and so I am not that familiar with it. I want the varying degree of shading to relate to my raster values (e.g. values 2 and 3, in light and values 1,4,5 in dark). I will try to implement your suggestion. Thank you. – geo_dd Nov 04 '19 at 15:51
  • In that case it might be best to manually specify the colors for each value. I've edited my answer to include one possible way of doing that. – Where's my towel Nov 04 '19 at 16:36