TransWikia.com

How can the order of labels in a Google Earth Engine chart be edited?

Geographic Information Systems Asked by Lilifri on April 30, 2021

This is a follow-up question to the post "Stacked bar chart in Google Earth Engine" concerning the there-referenced code in the answer.

Looking at the labels of the plot in the console, the order is as expected (Jan to Dec). As soon as I open the plot in full size though, the order of the labels is reversed (Dec to Jan). This only happens if the option isStacked is NOT set to false.

Why does this happen and how can it be avoided? I would like the months to be in ordered Jan to Dec with a stacked graph. The code of the above-referenced answer is pasted below for reference.

/**
 * @license
 * Copyright 2020 Google LLC.
 * SPDX-License-Identifier: Apache-2.0
 */

// Define some ecoregion features.
var desert = ee.Feature(
    ee.Geometry.Rectangle(-109.21, 31.42, -108.3, 32.03),
        {label: 'Desert', value: 0});

var forest = ee.Feature(
    ee.Geometry.Rectangle(-122.73, 43.45, -122.28, 43.91),
        {label: 'Forest', value: 1});
    
var grassland = ee.Feature(
    ee.Geometry.Rectangle(-101.81, 41.7, -100.53, 42.51),
        {label: 'Grassland', value: 2});

// Combine features into a feature collection.
var ecoregions = ee.FeatureCollection([desert, forest, grassland]);

// Load PRISM climate normals image collection; convert images to bands.
var normClim = ee.ImageCollection('OREGONSTATE/PRISM/Norm81m')
  .toBands();
  
// Add mean climate properties to each ecorgeion.
ecoregions = normClim.reduceRegions({
  collection: ecoregions,
  reducer: ee.Reducer.mean(),
  scale: 5e4});

// Make an absolute stacked bar chart.
print(
  ui.Chart.image.byRegion({
  image: normClim.select('[0-9][0-9]_ppt|label'),
  regions: ecoregions,
  reducer: ee.Reducer.mean(),
  scale: 500,
  xProperty: 'label'})
  .setSeriesNames(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                     'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
  .setChartType('ColumnChart')
  .setOptions({
    title: 'Average Monthly Precipitation by Ecoregion',
    hAxis: {title: 'Ecoregion'},
    vAxis: {title: 'Precipitation (mm)'},
    colors: ['604791', '1d6b99', '39a8a7', '0f8755', '76b349', 'f0af07',
            'e37d05', 'cf513e', '96356f', '724173', '9c4f97', '696969'],
    isStacked: 'absolute',
  })
);

// Make relative stacked bar chart.
print(
  ui.Chart.image.byRegion({
  image: normClim.select('[0-9][0-9]_ppt|label'),
  regions: ecoregions,
  reducer: ee.Reducer.mean(),
  scale: 500,
  xProperty: 'label'})
  .setSeriesNames(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                     'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
  .setChartType('ColumnChart')
  .setOptions({
    title: 'Average Monthly Precipitation by Ecoregion',
    hAxis: {title: 'Ecoregion'},
    vAxis: {title: 'Precipitation (mm)'},
    colors: ['604791', '1d6b99', '39a8a7', '0f8755', '76b349', 'f0af07',
            'e37d05', 'cf513e', '96356f', '724173', '9c4f97', '696969'],
    isStacked: 'relative', // if this is set to false, the order of the labels is Jan to Dec
  })
);

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP