Charts by Image Classes

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('TIGER/2016/States')
                                 .filter(ee.Filter.eq('NAME', 'Rhode Island'))

// 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.
    // Reclassify to the broad categories.

// Load Landsat 8 TOA reflectance, add the IGBP band.
var image = ee.Image('LANDSAT/LC8_L1T_32DAY_TOA/20130407')

// 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)

// Print the 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.

Figure 1. Plot of Landsat 8 spectra by MODIS land cover class.