Add correlation coefficient (r) and r^2 to plot
33 views (last 30 days)
Show older comments
I am having difficulty adding my value of correlation coefficient and r^2 to my scatter plot. I am able to create a scatter plot and add trendline to the plot as well as the trendline equation. I would like to have the correlation coefficient (r ) and r^2 underneath the trendline equation (see scatterplot figure). I can't seem to figure this part out. Attached is my scatterplot (jpeg) and data (csv).
T=csvread('merged_hrrr_camera_arm_DJF2018_19.csv',1,1);
x=T(:,2); % hrrr data
y=T(:,9); % camera data
scatter(x,y) %scatter plot hrrr vs camera
title('DJF 2018-19 HRRR f00 vs Camera')
xlabel('HRRR')
ylabel('Camera')
c=polyfit(x,y,1); %fit trendline
disp([' y = ' num2str(c(1)) '*x + ' num2str(c(2))]);
R =corrcoef(x,y);
r=['',num2str(R(1,2))]
R2 = R(2)^2;
r2=['',num2str(R2)]
eq = sprintf('y = %.3f x + %.3f', c(1), c(2));
text(70, 70, eq)
y_est=polyval(c,x); %evaluate fit equation using polyval
hold on
plot(x,y_est)
hold off
0 Comments
Answers (1)
Star Strider
on 19 Jul 2020
I would just use another text object. Alternatively, you can add those to ‘eq’ easily enough.
Try this:
eq = sprintf('y = %.3f x + %.3f \n R = %.3f \n R^2 = %.3f', c(1), c(2), R, R2);
.
0 Comments
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!