필터 지우기
필터 지우기

Plotting with categorical data

조회 수: 9 (최근 30일)
Shaun Ramsumar
Shaun Ramsumar 2020년 5월 11일
댓글: Shaun Ramsumar 2020년 5월 11일
Plotting a bar chart with a categorical vector on the x-axis and a double vector on the y-axis.
The resulting bar chart is alphabetically arranged (by default) by the categorical vector on the x-axis.
How does one arrange the bar chart with highest to lowest values from the double vector on the y-axis ?
R, Shaun

채택된 답변

Johannes Hougaard
Johannes Hougaard 2020년 5월 11일
Based on the help for the bar plot function with categoricals the solution is possibly to use the reordercats function.
I found this to be the way to do it
workdays = {'monday','tuesday','wednesday','thursday','friday','saturday','sunday'};
workhours = [8.0 4.5 8.2 7.4 10.2 3.1 0.6];
[~,arr] = sort(workhours,'descend');
X = reordercats(categorical(workdays),workdays(arr));
figure;
bar(X,workhours);
  댓글 수: 1
Shaun Ramsumar
Shaun Ramsumar 2020년 5월 11일
Thank you Johannes,
Works, but I had to convert the second argument of reordercats with cellstr.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 5월 11일
[sy, syidx] = sort(y, 'descend');
cc = categorical(1:length(y), 1:length(y), string(x(syidx)));
plot(cc, sy);

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by