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