I plotted a raster onto Google Earth Engine, with a colour palette, and created a legend as shown.
// Pull the package from the GitHub repo which stores colour palettes to be used in GEE
// See https://github.com/gee-community/ee-palettes for more info
var palettes = require('users/gena/packages:palettes');
// Choose the yellow, orange, red colour palette with 9 classes
var palette = palettes.colorbrewer.YlOrRd[9];
var vis = {min: 0, max: 335, palette: palette};
// Add a raster layer and enable the colour palette
Map.addLayer(image, {min: 0, max: 335, palette: palette});
// Creates a color bar thumbnail image for use in legend from the given color palette
function makeColorBarParams(palette) {
return {
bbox: [0, 0, 1, 0.1],
dimensions: '100x10',
format: 'png',
min: 0,
max: 1,
palette: palette,
};
}
// Create the colour bar for the legend
var colorBar = ui.Thumbnail({
image: ee.Image.pixelLonLat().select(0),
params: makeColorBarParams(vis.palette),
style: {stretch: 'horizontal', margin: '0px 8px', maxHeight: '24px'},
});
// Create a panel with three numbers for the legend
var legendLabels = ui.Panel({
widgets: [
ui.Label(vis.min, {margin: '4px 8px'}),
ui.Label(
((vis.max-vis.min) / 2+vis.min),
{margin: '4px 8px', textAlign: 'center', stretch: 'horizontal'}),
ui.Label(vis.max, {margin: '4px 8px'})
],
layout: ui.Panel.Layout.flow('horizontal')
});
// Legend title
var legendTitle = ui.Label({
value: 'Carbon (tons C)',
style: {fontWeight: 'bold'}
});
// Add the legendPanel to the map
var legendPanel = ui.Panel([legendTitle, colorBar, legendLabels]);
Map.add(legendPanel);
The issue with this solution is that 0 values are included as the lightest shade in my colour palette, but I'd like them excluded (either as transparent or white). Is there a way this can be done?