Question about bar graph ?

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

Question about bar graph ?

Hi everyone,

I've been trying to plot some bar graphs with the following data :

YearAgeImmigrantsNon-Immigrants
1991< 142096430
19911 - 45410341025
19915 - 913690432140
199110 - 1419275462460
199115 - 1925120424565
199120 - 2980015973205
199130 - 391091201109270
199140 - 49116935870385
199150 - 5989810585945
199160 - 6974815498835
199170 - 7937615288155
199180 - 891686085070
1991>= 9021207635

I want to plot 2 bar graphs (should look likes histograms) in the same figure to show the difference between 2 groups. Here are the codes with PROC SGSCATTER:

proc sgscatter data = population;

  plot (Immigrants Non_immigrants)* Age / rows=2 columns=1 uniscale=all;

  where year = 1991;

  title "Scatter plots of age for Immigrants vs. Non-immigrants";

run;

I'd really appreciate any help/suggestion. Thanks in advance.


Accepted Solutions
Solution
‎11-08-2014 10:23 AM
Super Contributor
Posts: 543

Re: Question about bar graph ?

How about this?

W/o the statistics...

proc template;

define statgraph two_bars_again;

dynamic _panelnumber_;

begingraph / designwidth=1222 designheight=845;

   layout datalattice rowvar=GROUP / cellwidthmin=1 cellheightmin=1 rowgutter=3 columngutter=3 rowdatarange=unionall row2datarange=unionall columndatarange=unionall column2datarange=unionall headerlabeldisplay=value;

      layout prototype / ;

         barchart x=AGE y=POPULATION / name='bar' barlabel=true barwidth=0.85 clusterwidth=0.85;

      endlayout;

   endlayout;

endgraph;

end;

run;

proc sgrender data=WORK.QUEBEC_POP template=two_bars_again;

run;

Smiley Happy I stop here.

View solution in original post


All Replies
SAS Super FREQ
Posts: 3,624

Re: Question about bar graph ?

First transpose the data from wide to long so that there is a new column named "Group" with the values "Immigrants" and "Non-Immigrants." Then use PROC SGPLOT and specify

vbar age / group=Group  groupdisplay=cluster;

Contributor
Posts: 23

Re: Question about bar graph ?

Thanks for your suggestion.

I did transpose the data into long format :

YearAgePopulationGroup
1991< 1420Immigrants
19911 - 45410Immigrants
19915 - 913690Immigrants
199110 - 1419275Immigrants
199115 - 1925120Immigrants
199120 - 2980015Immigrants
199130 - 39109120Immigrants
199140 - 49116935Immigrants
199150 - 5989810Immigrants
199160 - 6974815Immigrants
199170 - 7937615Immigrants
199180 - 8916860Immigrants
1991>= 902120Immigrants
1991< 196430Non-immigrants
19911 - 4341025Non-immigrants
19915 - 9432140Non-immigrants
199110 - 14462460Non-immigrants
199115 - 19424565Non-immigrants
199120 - 29973205Non-immigrants
199130 - 391109270Non-immigrants
199140 - 49870385Non-immigrants
199150 - 59585945Non-immigrants
199160 - 69498835Non-immigrants
199170 - 79288155Non-immigrants
199180 - 8985070Non-immigrants
1991>= 907635Non-immigrants

But when I use your code:

title "Bar graphs of age for Immigrants vs. Non-immigrants";

proc sgplot data = quebec_pop;

  vbar Age / group=Group groupdisplay=cluster;

  where year = 1991;

run;

it didn't really work. Here is the result:

toto.bmp

"<1" should be in the first column and I don't want 2 bar graphs together, I want 2 separated graph as in the example code I provided in OP (rows=2 columns=1).

Thanks.

Super Contributor
Posts: 543

Re: Question about bar graph ?

Hi,

How about a little GTL? I think it may accomplish what you need:

data population;

input Year 1-4    Age $9-16     Immigrants    Non_Immigrants;

datalines;

1991    < 1        420    96430

1991    1 - 4    5410    341025

1991    5 - 9    13690    432140

1991    10 - 14    19275    462460

1991    15 - 19    25120    424565

1991    20 - 29    80015    973205

1991    30 - 39    109120    1109270

1991    40 - 49    116935    870385

1991    50 - 59    89810    585945

1991    60 - 69    74815    498835

1991    70 - 79    37615    288155

1991    80 - 89    16860    85070

1991    >= 90    2120    7635

;

proc template;

define statgraph twoBars;

begingraph / designwidth=1428 designheight=936;

   layout lattice / rowdatarange=data columndatarange=data rowgutter=10 columngutter=10;

      layout overlay / xaxisopts=( discreteopts=( tickvaluefitpolicy=splitrotate)) yaxisopts=( linearopts=( viewmin=0.0 viewmax=1200000.0)) y2axisopts=( linearopts=( viewmin=0.0 viewmax=1200000.0)); *-- to mbe the same, so you ake the two groups comparable;

         barchart category=AGE response=IMMIGRANTS / name='bar' stat=mean barwidth=0.5 groupdisplay=Cluster clusterwidth=1.0 fillattrs=(color=CX8CA6CE );

         barchart category=AGE response=NON_IMMIGRANTS / name='bar2' yaxis=Y2 stat=mean barwidth=0.5 discreteoffset=0.5 groupdisplay=Cluster clusterwidth=1.0 grouporder=data;

         discretelegend 'bar' 'bar2' / opaque=false border=true halign=left valign=top displayclipped=true across=1 order=rowmajor location=inside;

      endlayout;

   endlayout;

endgraph;

end;

run;

proc sgrender data = POPULATION template=twoBars;

run;

Good luck!

Anca.

Message was edited by: Anca tilea I added y-axis range of values to be the same, so you can compare the groups.

Contributor
Posts: 23

Re: Question about bar graph ?

Thansk very much Anca !!!

I tried your code but I have to change a few minor things to make it work :

  • change tickvaluefitpolicy=splitrotate => tickvaluefitpolicy=rotate;
  • change category=AGE response=IMMIGRANTS => x=AGE y=IMMIGRANTS;
  • category=AGE response=NON_IMMIGRANTS   => x=AGE y=NON_IMMIGRANTS.

As I said in previous post, is it possible to divide them into 2 separated bar graphs (2 rows, 1 column) ? This example of histogram below is what I've been trying to accomplish (of course without the descriptive statitstics):

chronicC_age_10.bmp

Another thought, is it possible to display count/frequency on top of each bar as shown in the histogram ? Thanks.

Super Contributor
Posts: 543

Re: Question about bar graph ?

ok.

how about this:

(this is using the long dataset, with the group variable)

(but this is not what you want, either)

(or maybe it is)

proc capability data = quebec_pop noprint;

    comphist Population / class = group  barlabel = count

        nrows = 2

        cfill = greydd

       normal(color = red)

;

        inset min max mean/ format=6.2 height = 2.5 position = NE; 

run;

Solution
‎11-08-2014 10:23 AM
Super Contributor
Posts: 543

Re: Question about bar graph ?

How about this?

W/o the statistics...

proc template;

define statgraph two_bars_again;

dynamic _panelnumber_;

begingraph / designwidth=1222 designheight=845;

   layout datalattice rowvar=GROUP / cellwidthmin=1 cellheightmin=1 rowgutter=3 columngutter=3 rowdatarange=unionall row2datarange=unionall columndatarange=unionall column2datarange=unionall headerlabeldisplay=value;

      layout prototype / ;

         barchart x=AGE y=POPULATION / name='bar' barlabel=true barwidth=0.85 clusterwidth=0.85;

      endlayout;

   endlayout;

endgraph;

end;

run;

proc sgrender data=WORK.QUEBEC_POP template=two_bars_again;

run;

Smiley Happy I stop here.

SAS Super FREQ
Posts: 1,139

Re: Question about bar graph ?

Use SGPLOT for cluster bar chart.  Use SGPANEL for panel.

In my opinion, side-by-side comparisons is easier with cluster bar chart.

data population_grp;

drop Immigrants    Non_Immigrants;

input Year 1-4    Age $9-16     Immigrants    Non_Immigrants;

Group='Immigrants'; Population=Immigrants; output;

Group='Non_Immigrants'; Population=Non_Immigrants;  output;

datalines;

1991    < 1        420    96430

1991    1 - 4    5410    341025

1991    5 - 9    13690    432140

1991    10 - 14    19275    462460

1991    15 - 19    25120    424565

1991    20 - 29    80015    973205

1991    30 - 39    109120    1109270

1991    40 - 49    116935    870385

1991    50 - 59    89810    585945

1991    60 - 69    74815    498835

1991    70 - 79    37615    288155

1991    80 - 89    16860    85070

1991    >= 90    2120    7635

;

run;

title "Bar graphs of age for Immigrants vs. Non-immigrants";

ods graphics / reset width=6in height=4in;

proc sgplot data = population_grp;

  vbar Age / group=group response=Population groupdisplay=cluster nostatlabel;

  xaxis fitpolicy=none discreteorder=data valueattrs=(size=8);

run;

ods graphics / reset width=6in height=4in;

title "Bar graphs of age for Immigrants vs. Non-immigrants";

proc sgpanel data = population_grp;

  panelby group / layout=rowlattice rows=2 onepanel novarname;

  vbar Age / response=Population groupdisplay=cluster nostatlabel;

  colaxis fitpolicy=none discreteorder=data;

  rowaxis offsetmin=0;

run;

Contributor
Posts: 23

Re: Question about bar graph ?

Sorry for the late post. I've been busy these days.

Thanks Anca and Sanjay for your answers. For me, Sanjay's suggestion is easier to understand and execute. But in another hand, proc template + proc sgrender by Anca is more useful if we want to mix our graph with different types of panel.

Many thanks.

Super Contributor
Posts: 543

Re: Question about bar graph ?

One more thought,

To make suggestion work, you need to add a response variable.

proc sgplot data = quebec_pop;

  vbar Age / group=Group groupdisplay=cluster  response = population;

  where year = 1991;

run;

Anca.

SAS Employee
Posts: 980

Re: Question about bar graph ?

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 491 views
  • 6 likes
  • 5 in conversation