简体   繁体   English

使用 Proc sgplot 条形图中每个条形的不同 label

[英]Different label for each bar in a bar chart using Proc sgplot

I'm trying to add the number of each group into the labels of groups on xaxis using Proc sgplot in SAS.我正在尝试使用 SAS 中的 Proc sgplot 将每个组的数量添加到 xaxis 上的组标签中。 Here is the data and the graph I'd like to have.这是我想要的数据和图表。 I want to have sample of each bar on xaxis (hand writen parts).我想在 xaxis 上获取每个条的样本(手写部分)。 Your help is very much appreciated!非常感激你的帮助!

Data have ;
input type  sex $  n n_total percent ;
datalines;
0  F  6    29  20.7 
1  F  387  496 78.2  
0  M  4    15  26.6
1  M  264  305 86.5
;
Run; 

proc sgplot data=have ;
vbarparm category= type  response=percent /group=sex groupdisplay=cluster datalabel;
run; 

Graph that I want to create:我要创建的图表: 在此处输入图像描述

You can compute a bar data label that shows the percent value and the text n=<N>您可以计算显示百分比值和文本n=<N>的条形数据 label

Example:例子:

Data have ;
input type  sex $  n n_total percent ;
datalines;
0  F  6    29  20.7 
1  F  387  496 78.2  
0  M  4    15  26.6
1  M  264  305 86.5
;
Run; 

data plot;
  set have;
  barlabel = cats(percent) || ' %N/(n=' || cats(n_total, ')');
run;

proc sgplot data=plot;
  vbarparm 
    category=type  
    response=percent 
  / group=sex 
    groupdisplay=cluster 
    datalabel=barlabel datalabelfitpolicy=splitalways splitchar='/'
  ;
  label percent = 'Percent N having some attribute';
run; 

在此处输入图像描述

While I agree with Richard that you might be better off putting this in the bar label, it's easy to put it in an axis table as well.虽然我同意 Richard 的观点,您最好将它放在 label 栏中,但也很容易将它放在轴表中。

Data have ;
input type  sex $  n n_total percent ;
datalines;
0  F  6    29  20.7 
1  F  387  496 78.2  
0  M  4    15  26.6
1  M  264  305 86.5
;
Run; 

proc sgplot data=have ;
vbarparm category= type  response=percent /group=sex groupdisplay=cluster datalabel;
xaxistable n_total/class=sex classdisplay=cluster position=bottom location=inside colorgroup=sex;
run; 

classdisplay=cluster makes the values spread out like the bars do, location=inside puts it right below the bar, and colorgroup=sex makes it colored like the bars (instead of black). classdisplay=cluster使值像条一样散开, location=inside将其放在条的正下方,而colorgroup=sex使其像条一样着色(而不是黑色)。 position=bottom is the default, just highlighting that option. position=bottom是默认值,只是突出显示该选项。

You could further customize what shows up if you wanted to in the same manner Richard did - by creating a text variable that contains exactly what you want to display for each, and using that as your variable (the first argument to xaxistable ).如果您想以与 Richard 相同的方式进一步自定义显示的内容 - 通过创建一个包含您想要为每个显示的内容的文本变量,并将其用作您的variablexaxistable的第一个参数)。 That variable can be numeric or charcter.该变量可以是数字或字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM