Scattered plot for different groups of data, subplot?

Let say I have a table which contains three columns, Country (4 countries), Variable x and Response y. I would like to know how to create a plot for each country in a single figure?
gscatter (Table.x, Table.y, Table.Country) %This only allows me to plot all four in one graph.

 채택된 답변

Walter Roberson
Walter Roberson 2017년 5월 29일
[u_country, ~, idx] = unique(Table.Country);
for K = 1 : 4
subplot(2,2,K)
mask = idx == K;
gscatter(Table.x(mask), Table.y(mask), Table.Country(mask));
title( u_country ); %you might need to adjust this depending on the class() of your country data
end

댓글 수: 4

It doesn't really work, but I think we are getting there. I have a figure (let say figure 1) before this figure (figure 2) with subplots. When I apply your codes, my figure 1 is affected, and it becomes a blank subplot in the position (2,2,1). And, the second to forth subplots in my figure 2 are okay, but the first subplot is missing (I assume it's somehow being read as figure 1?). Any clues?
% This is Figure 1
figure (01)
f1 = gscatter(Table.y, Table.predictedy, Tablecountry)
hold on
m = 0:10:100; n = m;
LineofEquality = plot (m,n, 'k', 'Linewidth', 1.75);
hold off
legend ([f1])
% This is Figure 2
[u_country, ~, idx] = unique(Table.Country);
for K = 1 : 4
subplot(2,2,K)
mask = idx == K;
f2 = gscatter(Table.x(mask), Table.y(mask), Table.Country(mask));
hold on
m = 20:10:120; n = 0*m;
lineAtX = plot (m,n, 'k', 'Linewidth', 1.75)
hold off
title(u_country); % *Sorry how to adjust this?*
legend ([f2]) %the label in the legend is incorrect, as it displays the true label as well as the labels of unplotted data?
end
% This is Figure 2
figure(2)
[u_country, ~, idx] = unique(Table.Country);
for K = 1 : 4
subplot(2,2,K)
mask = idx == K;
gscatter(Table.x(mask), Table.y(mask), Table.Country(mask));
hold on
m = 20:10:120; n = 0*m;
lineAtX = plot (m,n, 'k', 'Linewidth', 1.75)
hold off
title( num2str(u_country(K)) );
end
gscatter() automatically creates the legend.
I had to assume here that your Table.Country information is numeric. If it is categorical, then use
title( char(u_country(K)) );
wesleynotwise
wesleynotwise 2017년 5월 30일
편집: wesleynotwise 2017년 5월 30일
Thank you! Beautiful plots have been created. I made a minor correction "f2 = gscatter" to my code.
The legend contains the name of lineAtX (this is just a horizontal line I created for the plot to show the neutral axis where y = 0), and I want to exclude it. Can you please show me how this can be done?
Also, is it possible to change the order they appear in the plot? At the moment, the default setting is in alphabetical order.
If you want to change the order that the countries appear, then you can use
display_order = [2 4 3 1]; %for example
and
% This is Figure 2
figure(2)
[u_country, ~, idx] = unique(Table.Country);
for K = 1 : 4
subplot(2,2,K)
this_country = display_style(K);
mask = idx == this_country;
gscatter(Table.x(mask), Table.y(mask), Table.Country(mask));
hold on
m = 20:10:120; n = 0*m;
lineAtX = plot (m,n, 'k', 'Linewidth', 1.75)
hold off
title( char(u_country(this_country)) );
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

질문:

2017년 5월 29일

편집:

2017년 5월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by