1

Currently, I am working with the feature group chart – a scatter plot. The legend (series) displayed on the chart is automatically sorted by the value that first appeared on it, so it looks like a random number (attached image). I wanted to customize the legend by sorting the values from small to large but found nothing (case: 1 - 11). Does anyone have a solution?

My code:

Map.addLayer(target_image);
Map.addLayer(world);

function generatesScatter(feature, xProperty, yProperty, series, title, hAxis_title, xAxis_title, hMinMax, vMinMax) {
    var scatter_chart = ui.Chart.feature.groups(feature, xProperty, yProperty, series)
        .setChartType('ScatterChart')
        .setOptions({
            title: title,
            pointSize: 2,
            dataOpacity: 0.4,
            hAxis: {
                viewWindow: {
                    min: hMinMax[0],
                    max: hMinMax[1],
                },
                'title': hAxis_title,
                titleTextStyle: { italic: false, bold: true },
            },
            vAxis: {
                viewWindow: {
                    min: vMinMax[0],
                    max: vMinMax[1],
                },
                'title': xAxis_title,
                titleTextStyle: { italic: false, bold: true }
            },
        });

    return scatter_chart
}

function stratifiedSampling(img_input, numPoints, classBand, classVal, pts, scale, region) {
    // Sampling
    var str_point = img_input.stratifiedSample({
        numPoints: numPoints,
        classBand: classBand,
        // Class to be sampled
        classValues: classVal,
        // Points each class
        classPoints: [pts, pts, pts, pts, pts, pts, pts, pts, pts, pts, pts],
        scale: scale,
        // region: region,
        geometries: false
    })
    return str_point
}

// Class references 
// Define a dictionary that maps the original values to new numeric values
var classDict = ee.Dictionary({
    "Bush / Shrub": 1,
    "Swamp Shrub": 2,
    "Plantation Forest": 3,
    "Primary Swamp Forest": 4,
    "Secondary Swamp Forest": 5,
    "Primary Mangrove Forest": 6,
    "Dryland Agriculture": 7,
    "Secondary Mangrove Forest": 8,
    "Primary Dry Land Forest": 9,
    "Secondary Dry Land Forest": 10,
    "Shrub-Mixed Dryland Farm": 11
});

// List of class
var classList = classDict.keys()

// List of class values
// Using for remapping raster value from 0 to 10 --> 1 to 11
var valueList = classDict.values().sort()

// SEA
var SEA_select = world.select('VV_mean', 'VV_stdDev', 'VH_mean', 'VH_stdDev');

// K-Means Clustering
// Create training dataset.
var training = SEA_select.sample({
    region: aoi,
    scale: 1000,
    numPixels: 10000
});

// Start unsupervised clusterering algorithm and train it.
var kmeans = ee.Clusterer.wekaKMeans(11).train(training);

// Cluster the input using the trained clusterer.
var result = SEA_select.cluster(kmeans);

// Remapping class raster
var cluster_image = result.select("cluster")
    .remap([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], valueList)
    .rename("cluster")

// Add SEA Band to image input
var cluster_concat = cluster_image.addBands(world)

// Cluster - Str sampling
var cluster_sample = stratifiedSampling(
    cluster_concat, 1100, "cluster", valueList, 100, 1000, aoi
)

// Cluster - mean chart
var cluster_mean_chart = generatesScatter(
    cluster_sample, 
    "VV_mean", 
    "VH_mean", 
    "cluster", 
    "VV Mean .vs VH Mean - Cluster Results", 
    "VV Mean", 
    "VH Mean",
    [-22, -1.5],
    [-30, -5]
)

print("Cluster Mean:",cluster_mean_chart)

Link:

https://code.earthengine.google.com/893419f54566b127298e523384106f64

enter image description here

1 Answer 1

1

I have found a reference to this issue, from a person named Ujaval Gandhi. The point is that there is no built-in feature. The solution is to reformat the data, so we can make the columns sequential. The function used is DataTable Charts. This is done via client-side, with asynchronous, making it a bit slower.

Main code:

var rows = class_id.map(function (cluster) {
    var features = feature.filter(ee.Filter.eq(series, cluster))
    return features.toList(features.size()).map(function (f) {
        var f = ee.Feature(f);
        var cluster = f.get(series);
        var vv = f.get(xProperty);
        var vh = f.get(yProperty);
        var empty = ee.List([{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}])
        var row = empty.set(ee.Number(cluster).subtract(1), { v: vh });
        var fullrow = ee.List([{ v: vv }]).cat(row)
        return { c: fullrow }
    })
}).flatten()

rows.evaluate(function (rowsResult) {
    var dataTable = {
        cols: [
            { id: 'vv', label: 'VV', type: 'number' },
            { id: 'vh1', label: '1', type: 'number' },
            { id: 'vh2', label: '2', type: 'number' },
            { id: 'vh3', label: '3', type: 'number' },
            { id: 'vh4', label: '4', type: 'number' },
            { id: 'vh5', label: '5', type: 'number' },
            { id: 'vh6', label: '6', type: 'number' },
            { id: 'vh7', label: '7', type: 'number' },
            { id: 'vh8', label: '8', type: 'number' },
            { id: 'vh9', label: '9', type: 'number' },
            { id: 'vh10', label: '10', type: 'number' },
            { id: 'vh11', label: '11', type: 'number' },
        ],
        rows: rowsResult
    };

Full code: https://code.earthengine.google.com/948d885b2328ec2051216d52dc45eba9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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