To plot image data aggregated by classes represented by an image band, use
ui.Chart.image.byClass(). For example, the following code plots Landsat
spectra in Rhode Island, aggregated by land cover class:
// Load a region representing Rhode Island state.
var rhodeIsland = ee.Feature(ee.FeatureCollection('ft:17aT9Ud-YnGiXdXEJUyycH2ocUqreOeKGbzCkUw')
.filter(ee.Filter.eq('id', 'RI'))
.first());
// Define a broad list of land cover categories.
var classNames = ee.List(['Water', 'Forest', 'Shrub', 'Grass', 'Urban']);
// Load MODIS land cover data.
var classifiedImage = ee.Image('MODIS/051/MCD12Q1/2012_01_01')
// Select the IGBP classification.
.select(['Land_Cover_Type_1'])
// Reclassify to the broad categories.
.remap([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17],
[0,1,1,1,1,1,2,2,1,3,3,0,3,4,3,0,4,0]);
// Load Landsat 8 TOA reflectance, add the IGBP band.
var image = ee.Image('LANDSAT/LC8_L1T_32DAY_TOA/20130407')
.select('B[1-7]')
.addBands(classifiedImage);
// Define a list of Landsat 8 wavelengths for X-axis labels.
var wavelengths = [0.44, 0.48, 0.56, 0.65, 0.86, 1.61, 2.2];
// Define chart customization options.
var options = {
lineWidth: 1,
pointSize: 2,
hAxis: {title: 'Wavelength (micrometers)'},
vAxis: {title: 'Reflectance'},
title: 'Spectra in classified regions of Rhode Island'
};
// Make the chart, set the options.
var chart = ui.Chart.image.byClass(
image, 'remapped', rhodeIsland, ee.Reducer.mean(), 500, classNames, wavelengths)
.setOptions(options);
// Print the chart.
print(chart);
In this example, note that the remap() output is a new image with a band named
remapped. The name of this band is passed to
ui.Chart.image.byClass() to identify the band containing class values. The
output should look something like Figure 1. Note that a list of class names makes the
legend more meaningful.