极坐标直方图可以非常有用地绘制具有多个条目的堆积条形图。下面的图像提供了一个示例,显示了图形目标。使用ggplot2在R中可以相对容易地实现这一点。类似于matlab中的“rose”函数似乎不允许这样的结果。
作为起点,这是我所拥有的内容:
结果仍然远远落后于
- 脚本
% inputs
l = [1 1.4 2 5 1 5 10;
10 5 1 5 2 1.4 1;
5 6 3 1 3 2 4];
alpha = [10 20 50 30 25 60 50]; % in degrees
label = 1:length(alpha);
% setings
offset = 1;
alpha_gap = 2;
polarHist(l,alpha,label)
函数
polarHist
function polarHist(data,alpha,theta_label,offset,alpha_gap,ticks)
if nargin 360-alpha_gap*length(alpha)
error('Covers more than 360°')
end
% code
theta_right = 90 - alpha_gap + cumsum(-alpha) - alpha_gap*[0:length(alpha)-1];
theta_left = theta_right + alpha;
col = get(gca,'colororder');
for j = 1:size(data,1)
hold all
if j == 1
rho_in = kron(offset*ones(1,length(alpha)),[1 1]);
else
rho_in = rho_ext;
end
rho_ext = rho_in + kron(data(j,:),[1 1]);
for k = 1:size(data,2)
h = makewedge(rho_in(k),rho_ext(k),theta_left(k),theta_right(k),col(j,:));
if j == size(data,1) && ~isempty(theta_label)
theta = theta_right(k) + (theta_left(k) - theta_right(k))/2;
rho = rho_ext(k)+1;
[x,y] = pol2cart(theta/180*pi,rho);
lab = text(x,y,num2str(theta_label(k),'%0.f'),'HorizontalAlignment','center','VerticalAlignment','bottom');
set(lab, 'rotation', theta-90)
end
end
end
axis equal
theta = linspace(pi/2,min(theta_right)/180*pi);
%ticks = [0 5 10 15 20];
rho_ticks = offset + ticks;
ax = polar([ones(length(ticks(2:end)),1)*theta]',[rho_ticks(2:end)'*ones(1,length(theta))]');
set(ax,'color','w','linewidth',1.5)
axis off
for i=1:length(ticks)
[x,y] = pol2cart((90)/180*pi,rho_ticks(i));
text(x,y,num2str(ticks(i)),'HorizontalAlignment','right');
end
- 函数
makewedge
function hOut = makewedge(rho1, rho2, theta1, theta2, color)
%MAKEWEDGE Plot a wedge.
% MAKEWEDGE(rho1, rho2, theta1, theta2, color) plots a polar
% wedge bounded by the given inputs. The angles are in degrees.
%
% h = MAKEWEDGE(...) returns the patch handle.
ang = linspace(theta1/180*pi, theta2/180*pi);
[arc1.x, arc1.y] = pol2cart(ang, rho1);
[arc2.x, arc2.y] = pol2cart(ang, rho2);
x = [arc1.x arc2.x(end:-1:1)];
y = [arc1.y arc2.y(end:-1:1)];
newplot;
h = patch(x, y, color);
if ~ishold
axis equal tight;
end
if nargout > 0
hOut = h;
end
结果仍然远远落后于
ggplot2
的输出,但我认为这是一个开始。我正在努力添加图例(l
的行)...