简体   繁体   English

SAS 中的配对条形图

[英]Paired bar charts in SAS

I need help creating a single bar chart, that pairs bars together (by two levels of Group), for four time periods.我需要帮助创建一个条形图,将条形图配对在一起(按两个级别的组),用于四个时间段。 Here's what my table of data look like, sorted by 'Group':这是我的数据表的样子,按“组”排序:

在此处输入图像描述

I've figured out how to plot the means for both groups, but only for one time period at a time:我已经弄清楚如何 plot 两组的手段,但一次只有一个时间段:

proc sgplot data=Testdata;
  vbar Group /
    response=Baseline
    stat=mean
    GROUPDISPLAY = CLUSTER;
run;

Which gets me this:这让我明白了:

在此处输入图像描述

However, I'd like to "smoosh" these two bars together, so that they're touching, and then add the means, for each level of group, for the other three time periods, all in one plot.但是,我想将这两个条“平滑”在一起,使它们相互接触,然后为每个级别的组添加平均值,对于其他三个时间段,都在一个 plot 中。 I've tried just adding the other time periods to the 'response=' line (both with, and without commas) but that doesn't work.我尝试将其他时间段添加到“response=”行(包括逗号和不带逗号),但这不起作用。

Please help!请帮忙!

(And I know this is kind of greedy, but it would be great if anyone could tell me how to change the bar color based on Group level) (我知道这有点贪心,但如果有人能告诉我如何根据组级别更改条形颜色,那就太好了)

TIA for any help. TIA 寻求帮助。

You will want to transpose the data so you have columns id , group , period , result .您将需要转置数据,以便拥有列idgroupperiodresult

The VBAR satement would change from VBAR 状态将从

  • VBAR GROUP to VBAR GROUP
  • VBAR PERIOD

and you can use the VBAR features并且您可以使用VBAR功能

  • group = GROUP
  • datalabel = result
  • statlabel

Example:例子:

data have;
  call streaminit(123);
  do group = 'A' , 'B';
    do _n_ = 1 to ifn(group='A',6,11);
      id + 1;
      baseline = ifn(group='A', 2125, 4400) + rand('integer', 400) - 200;
      period1 =  ifn(group='A', 2425, 4100) + rand('integer', 600) - 300;
      period2 =  ifn(group='A', 1800, 3600) + rand('integer', 500) - 250;
      period3 =  ifn(group='A', 1600, 2800) + rand('integer', 500) - 250;
      output;
    end;
  end;

  label 
    baseline = 'Basline'
    period1  = '14 Day Average'
    period2  = '30 Day Average'
    period3  = '60 Day Average'
  ;
run;

proc transpose data=have 
  out=plotdata ( 
    rename=(
      _name_  = period
      _label_ = period_label
      col1    = result
    ))
;
  by id group notsorted;
  var baseline period1-period3;
  label period = ' ';
  label period_label = ' ';
run; 

ods html file='plot.html' style=plateau;

proc sgplot data=plotdata;
  vbar period_label /
    response = result
    stat = mean
    groupdisplay = cluster
    group = group
    datalabel = result statlabel
  ;

  xaxis display=(nolabel);
run;

ods html close;

Image图片

在此处输入图像描述

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

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