Spectral profile mapping for Sentinel

google-earth-engineplotprofile

I want to plot spectral profile for Sentinel image.
For this, I write such a code:

var Sentinelbands =['B2','B3','B4','B5','B6','B7','B8','B8A','B11','B12'];
    
var Sentinel=ee.ImageCollection("COPERNICUS/S2_SR")
var image= Sentinel
        .filterBounds(polygon)
        .filterDate('2021-01-01', '2021-11-01')
        .select(Sentinelbands)
        .sort('CLOUDY_PIXEL_PERCENTAGE')
        .first()
        .clip(polygon);
        
print('CLOUD COVER:', image.get('CLOUD_COVERAGE_ASSESSMENT').getInfo())  
print('GRANULE ID:', image.get('GRANULE_ID').getInfo())  
print('WATER PERCENTAGE:', image.get('WATER_PERCENTAGE').getInfo())  
print('SPACECRAFT NAME:', image.get('SPACECRAFT_NAME').getInfo())  


Map.addLayer(image, {bands: ['B4', 'B3', 'B2'],min:0, max: 3000}, 'True colour image');


Map.addLayer(image, {bands: ['B4', 'B3', 'B2'],min:0, max: 3000}, 'True colour image',false);

var subset = image.select(Sentinelbands)
var samples = ee.FeatureCollection([Water,Forest,City,Mine]);
print(subset)

var Chart1 = ui.Chart.image.regions(
    subset, samples, ee.Reducer.mean(), 10, 'label')
        .setChartType('ScatterChart');
print(Chart1);


var plotOptions = {
  title: 'Sentinel 2  Surface Reflectance Spectra',
  hAxis: {title: 'Wavelength (nanometers)'},
  vAxis: {title: 'Reflectance'},
  lineWidth: 1,
  pointSize: 4,
  series: {
    0: {color: 'blue'}, // Water
    1: {color: 'green'}, // Forest
    2: {color: 'red'}, // City
    3: {color: 'maroon'}, // Mine
  }};

var wavelengths = [490,560,665,705,740,783,842,865,1610,2190]


var Chart2 = ui.Chart.image.regions(
    subset, samples, ee.Reducer.mean(), 10, 'label', wavelengths)
        .setChartType('ScatterChart')
        .setOptions(plotOptions);

print(Chart2);

Map.addLayer(image.clip(polygon), {bands: ['B4', 'B3', 'B2'],min:0, max: 3000}, 'True colour image');

The results are as folows:

enter image description here
enter image description here

As presented, bands 11,12 are placed in first order in the 1st plot.

How can I solve that?

https://code.earthengine.google.com/?scriptPath=users%2FSolmaz%2FBurdur_Sentinel_CloudFree%3ASentinel%20Spectral%20Profile%20New

Best Answer

ui.Chart.image.regions orders X-axis alphabetically.

One possible solution is to add a space before all one digits bands (see here https://code.earthengine.google.com/2a29344cabef33f10d4fb51bd2d9b1c5)

Another way is to construct an input for ui.Chart() yourself. Not easy, because .getDataTable() is not working properly. But if it would, you could tweak all the parts of how the data is ordered and displayed pretty easy (check out that option once the bug is fixed).

Related Question