Data visualization with SAS programming

How to create a graph with 2 variables in 1 bar per unit of time

Reply
Occasional Contributor
Posts: 11

How to create a graph with 2 variables in 1 bar per unit of time

Hi!

 

I want to show the distribution of food intake that is consumed over the day, and more specifically... I want to show the distribution of both protein and energy intake (both on the y-axis) that is consumed over the day, per food consumption occasion (fco) (e.g. breakfast, lunch) and per unit of time (both on the x-axis) in a graphical way.

 

I would like to project energy and protein as a vertical bar, and if possible I want both variables to be in the same bar (protein as part of energy per food consumption occasion). Also, I would like to present the food consumption occasion (fco) in different colours, so it is easier to observe differences per fco.

 

I've already tried some gplot syntaxes, but these did not produce the desired outcome.

PROC GPLOT data=test_1;

PLOT sum_prot*hour=fco

PLOT sum_enkcal*hour=fco;

RUN;

 

This syntax created two separate plots for protein and energy intake (see attachment for plot energy intake) instead of one in which protein and energy are combined. Also, no bars were created (which is logical when using gplot), and therefore protein and energy intake could not be presented in the same bar.

 

Can someone maybe help me with this? Is it even possible to present protein and energy intake in the same bar, and if, what kind of syntax do I need for this?

 

Many thanks in advance.

 

Best Regards,

Teuni

SAS Super FREQ
Posts: 1,139

Re: How to create a graph with 2 variables in 1 bar per unit of time

It is not clear what you mean by "in the same bar".  Do you mean the two values should be summed, or shown side by side in the same x-axis tick value?  It would help if you could attach a sketch of what you want, and your data.  Also, it is important to know which release of SAS are you using.

Occasional Contributor
Posts: 11

Re: How to create a graph with 2 variables in 1 bar per unit of time

Thank you for your comment and sorry for my unclarity. What I mean is that both protein and energy intake are measured in kcal. Therefore, I think it would be easiest to have one bar for each food consumption occasion which shows both the energy intake per food consumption occasion and the part of the energy intake which is derived from protein.

 

Hopefully the picture will clarify it a bit more (see attachment). The picture shows a bar for each food consumption occasion, and part of the bar is marked. So, for instance, if someone eats 100kcal of energy at breakfast and 35kcal of these 100kcal is derived from protein, then 35% of the bar will be dark-coloured whereas the rest of the bar (which represents energy intake that is not derived from protein but from other nutrients) will be light-coloured.

 

For simplity I made all bars black/white and grey (protein), but what I would like to have, is using a different colour for each food consumption occasion. So, for instance, foods eaten at breakfast are coloured purple (and then the protein part will be dark purple and the rest of the bar will be lighter purple).

 

I hope this will help.

 

Furthermore, I am making use of SAS version 9.4.

SAS Super FREQ
Posts: 3,620

Re: How to create a graph with 2 variables in 1 bar per unit of time

Because Energy > Protein, you can create to bar charts: Energy behind and Protein in front.

This snippet should get you started:

data Food;
informat Group $10.;
input Group Time Energy Protein;
datalines;
Breakfast 7     300 100
Breakfast 7.5   200  80
Lunch     11.5  400 200
Lunch     12.5  200 130
Dinner    17    500 220
Dinner    17.5   300 180
;

proc sgplot data=Food nocycleattrs;
styleattrs datacolors=(purple blue green);
vbar time / response=Energy group=Group transparency=0.5;
vbar time / response=Protein group=Group;
xaxis type=linear label="Time (h)";
yaxis label="Energy Type";
run;

 

You could also create a "stacked" bar chart. To do that, you would have to create a variable

Diff = Energy - Protein 

and then stack Diff on top of protein.

 

 

Occasional Contributor
Posts: 11

Re: How to create a graph with 2 variables in 1 bar per unit of time

[ Edited ]

Thank you for your suggestion Rick. This could have been a good solution if the different food consumption occasions were eaten on different times of the day. However, in my dataset some participants ate breakfast at 11am, whereas others ate lunch at 11am. Consequently, the colours of the different food consumption occassions will mix (and the graph becomes hard to interpretet), see attachment page 1 (please do not pay attention to the axis).

 

I also tried your stacked bar chart suggestion.

 

What I have in mind is that SAS will stack energy on top of protein per hour of the day, and, in order to get an easy-to-interpret graph, cluster this data per food consumption occasion. So, for instance, at 8am, there is 1 stacked (protein and energy) bar called breakfast, and at 11am there are 2 stacked (protein and energy) bars, one called breakfast and one called lunch.

 

I have tried the following syntax, and this will already led me into the right direction (see attachment page 2):

proc sgplot data=test_1 nocycleattrs;

styleattrs datacolors=(purple blue green yellow orange red pink);

vbar hour / response = sum_prot_kcal group=fco groupdisplay=cluster transparency=0.0;

vbar hour / response=diff_en group=fco groupdisplay=cluster transparency=0.5;

run;

 

What I would like to add to this graph is a line that shows the curve of intake per food consumption occasion (see attachement page 3). Is it possible to add? It will probably give a better overview of the intake distribution over the day.

 

Many thanks

 

Occasional Contributor
Posts: 11

Re: How to create a graph with 2 variables in 1 bar per unit of time

Is there someone out here who has experience with this?

 

Many thanks in advance!

SAS Super FREQ
Posts: 925

Re: How to create a graph with 2 variables in 1 bar per unit of time

Does this give you what you want?

 

proc sgplot data=test_1 nocycleattrs;

styleattrs datacolors=(purple blue green yellow orange red pink);

vbar hour / response = sum_prot_kcal group=fco groupdisplay=cluster transparency=0.0;

vbar hour / response=diff_en group=fco groupdisplay=cluster transparency=0.5;

vline hour / response=rate group=fco groupdisplay=cluster y2axis;

run;

Occasional Contributor
Posts: 11

Re: How to create a graph with 2 variables in 1 bar per unit of time

Does comes close to what I would like to have. I prefer using the 'density-statement'. However, I do not think it is 'easy' to overlay a histogram on a box plot, since this type of overlay is incompatable with the SGPLOT procedure.  

 

Ask a Question
Discussion stats
  • 7 replies
  • 704 views
  • 0 likes
  • 4 in conversation