Search code examples
scatter-plotgoogle-earth-engine

Scatter Plot between 2 images Google Earth Engine


I have 2 high-resolution images in Google Earth Engine. I want to plot a scatter between these 2 images. But I am getting some error Missing required arguments to function seriesByRegion(): reducer. I don't know how to correct this (or if this is due to the images).

First I imported geometry and my Image:

var RZSC = ee.Image("users/chandrakant/Max_RZSC_Layer_Trail_3"),
    geometry = /* color: #d6cbbb */ee.Geometry.Polygon(
        [[[-81.375, -56.125],
          [-34.625, -56.125],
          [-34.625, 12.625],
          [-81.375, 12.625]]]);

Then I visulatized the Image

var vizParams = {
  bands: ['b1'],
  min: 0.0,
  max: 1500.0,
  palette: ['blue', 'green', 'red']
};
Map.setCenter(6.746, 46.529, 10);
Map.addLayer(RZSC.clip(geometry), vizParams, 'Rootzone Storage Capacity');
Map.centerObject(RZSC);
print('RZSC Projection, crs, and crs_transform:', RZSC.projection());

Visualized my MODIS image

var MODIStc = ee.ImageCollection('MODIS/051/MOD44B')
                  .filter(ee.Filter.date('2000-01-01', '2017-12-01')).mean();
var percentTreeCover = MODIStc.select('Percent_Tree_Cover');
var percentTreeCoverVis = {
  min: 0.0,
  max: 100.0,
  palette: ['bbe029', '0a9501', '074b03'],
};
Map.setCenter(6.746, 46.529, 2);
Map.addLayer(percentTreeCover.clip(geometry), percentTreeCoverVis, 'Percent Tree Cover');
print('MOD44B Projection, crs, and crs_transform:', percentTreeCover.projection());
\\Here I added Water Mask
var waterOcc = ee.Image("JRC/GSW1_0/GlobalSurfaceWater").select('occurrence'),
    jrc_data0 = ee.Image("JRC/GSW1_0/Metadata").select('total_obs').lte(0),
    waterOccFilled = waterOcc.unmask(0).max(jrc_data0),
    waterMask = waterOccFilled.lt(50);

Here I reprojected the image (RZSC) at 250 m

\\Here I tried to increase the resolution from 25km to 250m    
var RZSC_250m = RZSC.resample('bilinear').reproject({
  crs: RZSC.projection().crs(),
  scale: 250
});
Map.addLayer(RZSC_250m.clip(geometry).updateMask(waterMask), vizParams, 'RZSC_250m');
print('RZSC_250m Projection, crs, and crs_transform:', RZSC_250m.projection());

enter image description here

Tried a scatterplot here

\\Tried to plot scatterplot here (I want just the basic scatter plot between 2 variables)
var chart = ui.Chart.image.seriesByRegion(RZSC, MODIStc, 
                ee.Reducer.mean(),250, 'nd').setChartType('ScatterChart');
print(chart);

The error I get: Error generating chart: Collection.map, argument 'collection': Invalid type. Expected: FeatureCollection. Actual: Image<[Percent_Tree_Cover]>. and Collection.map, argument 'collection': Invalid type. Expected: FeatureCollection. Actual: Image<[Percent_Tree_Cover]>.

Additional information: Also, I don't know why, but the dimensions of MODIStc and RZSC_250m are different. For MODIStc:

bands: List (1 element)
0: "Percent_Tree_Cover", double ∈ [0, 255], EPSG:4326, 48x73 px
id: Percent_Tree_Cover
crs: EPSG:4326
crs_transform: [1,0,0,0,1,0]
data_type: double ∈ [0, 255]
dimensions: [48,73]
origin: [-82,-59]

For RZSC_250m:

bands: List (1 element)
0: "b1", float, EPSG:4326, 20818x28371 px
id: b1
crs: EPSG:4326
crs_transform: List (6 elements)
data_type: float
dimensions: [20818,28371]
origin: [-36235,-6107]

Also, when I try to use reproject on MODIStc, the data looks smoothed and weird (a lot of details are lost). enter image description here


Solution

  • If I understand correctly, what you want is a scatter plot where one axis represents value in RZSC and the other axis represents value in MODIStc. Also, each dot in the chart should hold a pair of values at the same pixel.

    To achieve this, you will need ui.Chart.feature.byFeature, not ui.Chart.image.seriesByRegion as you're using.

    To use ui.Chart.feature.byFeature, you will need to create a FeatureCollection that has 2 properties, one holds values from all pixels in RZSC, and the other holds values from all corresponding pixels in MODIStc. Below is the sample code to get this:

    var combined_image = RZSC.addBands(MODIStc)
    var region = ee.Geometry.Rectangle(-83, -20, -31, 13)
    var sample = combined_image.sampleRegions(region, null, 250)
    var chart = ui.Chart.feature.byFeature(sample, 'b1', 'Percent_Tree_Cover')
        .setChartType('ScatterChart')
    print(chart)
    

    In the code above, I made up region variable that represents the area of interest (South America) based on your sample picture. You may want to change this to whatever you want (probably the whole world, I guess).

    Also, you may have to increase the scale value (currently 250 in the above code) or make your area of interest smaller if GEE throws up any memory error (e.g. 'Collection query aborted after accumulating over 5000 elements' or 'User memory limit exceeded')

    Hope this helps.