2

How can I make a stacked bar chart in Google Earth Engine?

Earth Engine's docs for ui.chart() refers to Google Visualization API for customization options. That API suggests to set isStacked = true in the charts options. I tried, but it still plots the bars individually (doesn't stack them). What am I missing?

Link to code. Code also pasted below for reference.

var dataTable = {
  cols: [{id: 'name', label: 'Classes', type: 'string'},
         {id: 'percentage', label: '(%)', type: 'number'}],

  rows: [{c: [{v: 'A'}, {v: 60}]},
        {c: [{v: 'B'}, {v: 40}]}]
  };
// Define a dictionary of customization options.
var options = {
  legend: {position: 'none'},
  padding:0,
  hAxis: {
    minValue:0,
    maxValue:100
  },
  height:'80px',
  isStacked: true //<=following google's recommendations. 
};
// Make a BarChart from the table and the options.
var chart = new ui.Chart(dataTable, 'BarChart', options);
print(chart)

2
  • 2
    I think it's because GEE is confused about your dataTable. It's expecting a 2-D array as input, while you give it a dictionary. Have tested a 2-D array [['Type', 'A', 'B'], ['Value', 40, 60]] and this works just fine.
    – Kevin
    May 29, 2020 at 9:34
  • You were right. Changing it to var dataTable = [['Type', 'A', 'B'], ['Value', 40, 60]] worked. Thank you.
    – kkrao
    May 30, 2020 at 5:37

1 Answer 1

3

Yes, you can create stacked bar charts with Earth Engine charting functions. I see your example is using a datatable, though the more common method would be to create one from a feature collection. Here is an example for a feature collection where each feature is an ecoregion zone and properties for each feature are 12 months of precipitation.

/**
 * @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',
  })
);

Code Editor link

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.