Scatter plots for a large dataset with 2 grouping variables

7 次查看(过去 30 天)
I have a large dataset with 4 variables. I want to make scatter plots for using two of the variables (x = date, y = d18O), GROUPED by the other two variables (site and depth). Specifically, I want there to be a single plot for each SITE, with different colored dataseries (x, y) based on DEPTH.
My code so far:
tT=readtable("data1.csv", "VariableNamingRule","preserve");
tT.Properties.VariableNames(2)={'Site'}; % shorten to be more convenient to use
G=grpstats(tT,{'Site','Depth'},{'mean','median','std'},'DataVars',{'Date','d18O'});
hSc=rowfun(@doit,tT,'GroupingVariables',{'Site'},'InputVariables',{'Date','d18O','Site','Depth'},'OutputFormat','uniform');
function h=doit(x,y,s,d)
figure
h=gscatter(x,y,d);
xlabel('Date'),ylabel('d18O')
end
This code produces one graph that looks like what I want, but then I get an error :
Error using tabular/rowfun
The function 'doit' returned a non-scalar value when applied to the 1st group of rows.
Error in lysimeter_processing_code_v2 (line 6)
hSc=rowfun(@doit,tT,'GroupingVariables',{'Site'},'InputVariables',{'Date','d18O','Site','Depth'},'OutputFormat','uniform');
Any help appreciated!
*note attached dataset is only a tiny fraction of actual data, this code will be used for a very large dataset when I am done. (hence why I need to automate making the figures)
*in final dataset, each SITE will contain data for up to 4 DEPTHS

采纳的回答

Voss
Voss 2023-9-26
编辑:Voss 2023-9-26
The function doit is written to return what gscatter returns, but gscatter returns an array of line handles - one line handle per group plotted - so doit returns a non-scalar array whenever there is more than one group plotted.
The solution is to make doit return a scalar no matter what. For instance, doit can return a scalar cell array containing the line handles returned from gscatter. (Or, if you don't need those line handles once the lines are created, you can just have doit return a scalar number, say 0 or 1 or NaN or anything else, or have doit return a status, say, logical true indicating it completed successfully.)
tT=readtable("data1.csv", "VariableNamingRule","preserve");
tT.Properties.VariableNames(2)={'Site'}; % shorten to be more convenient to use
G=grpstats(tT,{'Site','Depth'},{'mean','median','std'},'DataVars',{'Date','d18O'});
hSc=rowfun(@doit,tT,'GroupingVariables',{'Site'},'InputVariables',{'Date','d18O','Site','Depth'},'OutputFormat','uniform');
disp(hSc)
{2×1 Line} {1×1 Line} {1×1 Line} {1×1 Line} {1×1 Line}
function out=doit(x,y,s,d)
figure
h=gscatter(x,y,d);
out = {h}; % return a scalar cell array containing the lines
xlabel('Date'),ylabel('d18O')
title(sprintf('Site: %d',s(1))) % title the plot with the site value
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by