Adding vertical trendline for mutiple y variables in one scatterplot
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Hi,
I have a scatterplot with mutiple y variables from different trials. I want to make a trendline for all the trials together, not separate trendlines for each trial. I was reccomended to "draw" a vertical line on the x axis, then all of the trial points that touch that line should be averaged and a single point drawn on that x plane for the accumulative trendline. Then, to make the trendline, I would have to connect all these new dots together from each point of the x plane. Does this make sense? I'm not sure how to do it and would appreciate help!
Thanks!
Accepted Answer
Star Strider
on 3 Jul 2023
One option is to simply reshape all of the different ‘x’ and ‘y’ data vectors (combined into matrices here for convenience) into a single column using either reshape or the ‘(:)’ operator, and then perform the regression —
x = sort(randn(20, 5));
y = 5*(1:size(x,2)) + randn(size(x)) + 0.5*(1:size(x,1)).';
Line = [x(:) ones(size(x(:)))] * ([x(:) ones(size(x(:)))] \ y(:)); % Calculate Single Linear Regression Line For All Data
figure
hs = scatter(x, y, 'filled', 'DisplayName','Data');
hold on
hp = plot(x(:), Line, 'DisplayName','Linear Regression');
hold off
grid
xlabel('X')
ylabel('Y')
legend([hs hp], 'Location','best')

That is how I would approach it, anyway.
.
10 Comments
Sia Sharma
on 6 Jul 2023
I'm getting an error on this section:
Line = [x(:) ones(size(x(:)))] * ([x(:) ones(size(x(:)))] \ y(:)); % Calculate Single Linear Regression Line For All Data
- it says "Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for one variable t.(i). To select rows, use t(i,:)"
This is what i set my x and y as:
x = data(:,1)
y = data(:,2:9)
Star Strider
on 6 Jul 2023
That is certainly true for table arrays.
There are two options, the first is to use the table2array function to get ‘data’ as an array rather than a table. You can then use the addressing in the code you quoted.
The second is to change the addressing to use curly brackets {} instead, for example:
x = data{:,1}
y = data{:,2:9}
since that will also extract the data from the table, creating arrays.
It would likely be easier to use the second (curly brackets) option.
NOTE — In my original code, both ‘x’ and ‘y’ are matrices, so reshaping them into vectors produces vectors of equal lengths. Your ‘x’ is a vector (with I assume the same number of rows as ‘y’), so you will need to do:
x = repmat(x, size(y,2), 1)
This will make their row lengths equal, and the rest of my code should run without error.
.
Sia Sharma
on 6 Jul 2023
Hi,
This almost worked but I think the statement "x = repmat(x, size(y,2), 1)" made the x rows increase much more than my y rows.
x in workspace is now 2464x1 double
y in workspace is 308x8 double
Both should be 308 rows long. Do you know how to fix this? I think the statement made my x rows increase, not my x columns.
Star Strider
on 6 Jul 2023
That should work, since my ‘Line’ variable reshapes ‘y’ to create it as a column vector that will be the same size as ‘x’. It will work with the ‘x’ I calculated earlier using repmat.
Try it!
Sia Sharma
on 11 Jul 2023
Hi,
It gives me an error that says :
Error using scatter
X and Y must be vectors of the same length, matrices of the same size, or a combination of a vector and a matrix
where the length of the vector matches either the number of rows or columns of the matrix.
Please help!!
Star Strider
on 11 Jul 2023
I do not understand the problem. My code should work if your data matches my assumptions of it.
I need your data and your code to see what the problem is.
Sia Sharma
on 11 Jul 2023
My data is a excel file with x variable seconds, y variables mmHg values for different trials. All trials go up to 301 seconds, and I have 8 trials.
My code is below. Thanks for your help!
x = data{:,1}
y = data{:,2:9}
x = repmat(x, size(y,2), 1)
Line = [x(:) ones(size(x(:)))] * ([x(:) ones(size(x(:)))] \ y(:)); % Calculate Single Linear Regression Line For All Data
figure
hs = scatter(x, y, 'filled', 'DisplayName','Data');
hold on
hp = plot(x(:), Line, 'DisplayName','Linear Regression');
hold off
grid
xlabel('X')
ylabel('Y')
legend([hs hp], 'Location','best')
My pleasure!
I now see the problem in adapting my code to your data.
Try this —
data = array2table(randn(308,9).*(1:9)+(0:8)*10); % Create 'data'
xv = data{:,1}; % Change This To 'xv'
y = data{:,2:9};
x = repmat(xv, size(y,2), 1); % Change Reference To 'xv' Here
Line = [x(:) ones(size(x(:)))] * ([x(:) ones(size(x(:)))] \ y(:)); % Calculate Single Linear Regression Line For All Data
figure
hs = scatter(xv, y, 'filled', 'DisplayName','Data');
hold on
hp = plot(x(:), Line, '-k', 'LineWidth',2, 'DisplayName','Linear Regression');
hold off
grid
xlabel('X')
ylabel('Y')
legend([hs hp], 'Location','best')

.
Sia Sharma
on 11 Jul 2023
It worked! Thank you so much 😊
Star Strider
on 11 Jul 2023
As always, my pleasure!
More Answers (0)
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)