I’m trying to make two charts in Google Earth Engine. One of the mean temperature across 20 years, and one of days below 5 C. I’m struggling to resolve the error code “Error generating chart: Data column(s) for axis #0 cannot be of type string”. I’m fairly new to GIS, so I apologize if this is obvious. I’ll include my code below.
I’ve tried rewriting my code from the beginning a few times and I’m exhausted with it.
var states = ee.FeatureCollection("TIGER/2018/States");
var arkansas = states.filter(ee.Filter.eq('NAME', 'Arkansas'));
var temp = ee.ImageCollection("MODIS/061/MOD11A1").select('LST_Day_1km')
.filterDate('2001-01-01','2021-12-31');
var KtoC = temp.map(function(image) {
return image.multiply(0.02).subtract(273.15);
});
var arkTemp = KtoC.mean().clip(arkansas);
var calculateMonthlyStats = function(imageCollection) {
var months = ee.List.sequence(1, 12);
var monthlyStats = months.map(function(month) {
var start = ee.Date.fromYMD(2001, month, 1);
var end = start.advance(1, 'month');
var monthlyData = imageCollection.filterDate(start, end);
// Calculate mean and minimum temperatures
var meanTemp = monthlyData.mean();
var minTemp = monthlyData.reduce(ee.Reducer.min());
return ee.Feature(null, {
'month': ee.Number(month), // Cast month to number
'meanTemp': meanTemp,
'minTemp': minTemp
});
});
return ee.FeatureCollection(monthlyStats);
};
var monthlyStats = calculateMonthlyStats(temp);
var meanTempChart = ui.Chart.feature.byFeature(monthlyStats, 'month', 'meanTemp')
.setChartType('ColumnChart')
.setOptions({
title: 'Monthly Mean Temperature in Arkansas (2001-2022)',
hAxis: {title: 'Month'},
vAxis: {title: 'Temperature (°C)'},
colors: ['orange']
});
print(meanTempChart);
var calculateAnnualDaysBelow5C = function(imageCollection) {
var years = ee.List.sequence(2001, 2022);
var annualStats = years.map(function(year) {
var start = ee.Date.fromYMD(year, 1, 1);
var end = start.advance(1, 'year');
var yearlyData = imageCollection.filterDate(start, end);
// Calculate number of days below 5°C
var daysBelow5C = yearlyData.map(function(image) {
return image.lt(5).rename('below_5C');
}).sum();
return ee.Feature(null, {
'year': ee.Number(year).format('%d'),
'daysBelow5C': daysBelow5C
});
});
return ee.FeatureCollection(annualStats);
};
var annualDaysBelow5C = calculateAnnualDaysBelow5C(KtoC);
var daysBelow5CChart = ui.Chart.feature.byFeature(annualDaysBelow5C, 'year', 'daysBelow5C')
.setChartType('ColumnChart')
.setOptions({
title: 'Annual Count of Days Below 5°C in Arkansas (2001-2022)',
hAxis: {title: 'Year'},
vAxis: {title: 'Count of Days Below 5°C'},
colors: ['blue']
});
print(daysBelow5CChart);