Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: How to overlay multiple twoway bar graphs?
From
"McDermaid, Cameron" <[email protected]>
To
<[email protected]>
Subject
st: RE: How to overlay multiple twoway bar graphs?
Date
Wed, 5 Oct 2011 09:19:40 -0400
Stata/SE 12.0 for Windows (32-bit)
Revision 15 Sep 2011
The problem: How to generate a bar graph by values in a categorical variable to get a product similar to --twoway bar count date, by(category)-- but with all plots in a single graph rather than a graph per categorical value? The x axis data are dates, y axis counts by day.
The solution: This solution uses macros to pass graph rbar statements by category value. It works as expected in the scenarios I've tried so I hope it's helpful to others.
Limitations: You need to know the maximum number of possible category values
The script also makes use of Nick Cox's floor/ceiling script The Stata Journal (2003) 3, Number 4, pp. 446–447
Data format is agent of disease(category); date; count of cases by agent by day. There are no multiples of the same category by day.
e.g
category day count
1 04sep2011 3
2 04sep2011 4
3 04sep2011 1
2 05sep2011 2
3 05sep2011 5
1 07sep2011 3
4 07sept2011 1
---Script-----
label define agent 1 "Unknown" 2 "Influenza A" 3 "Influenza B" 4 "Influenza A & B"
label values category agent
* Predefine bar colors - maintains colour by agent for comparison between graphs
local color1 fcolor(yellow) /* Unknown */
local color2 fcolor(blue) /* Influenza A */
local color3 fcolor(green) /* Influenza B */
local color4 fcolor(red) /* Influenza A & B */
gen start=. /* Generate start & end points for rbar */
gen end=.
levelsof date, local(date)
foreach day of loc date {
local start=0
levelsof category if date==`day', local(agents)
foreach lev of loc agents {
replace start=`start' if date==`day' & category==`lev'
replace end=`start'+count if date==`day' & category==`lev'
sum end if date==`day' & category==`lev'
local start=`r(max)'
}
}
bysort date: gen total=_N /* Generate yaxis scale using Nick Cox's ceiling script */
sum total
local ymax=10*ceil(`r(max)'/10)
drop total
local i=0 /* Generate plot macros by agent */
levelsof category, local(agents)
foreach lev of loc agents {
local i=`i'+1
local name: label agent `lev'
local agent`i' rbar start end date if category==`lev', barwidth(0.8) `color`lev'' legend(lab (`i' `"`name'"'))||
}
* Graph using macro statements: use as many macro statements as you have potential agents
twoway `agent1' `agent2' `agent3' `agent4' , title("Daily case counts by day") ylabel(0(5)`ymax') ytitle("Count") xtitle("Date")
drop start end
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of McDermaid, Cameron
> Sent: September 15, 2011 8:49 AM
> To: [email protected]
> Subject: st: How to overlay multiple twoway bar graphs?
>
> I want to graph duration of disease outbreaks against the date of
> occurrence by aetiological agent for multiple sites so each site will
> have its own summary graph. I've been trying to generate bar graphs
> such that duration is on the y-axis, date of onset along the x-axis and
> each aetiological agent represented by it's own bar colour.
>
> I can get the result I want by manually using || to overlay bar graphs
> where each additional plot represents the duration and onset date of a
> particular aetiological agent.
> e.g.
> twoway bar duration index_date if site==2 & aetiologicagent=1 ||
> duration index_date if site==2 & aetiologicagent=4
>
> However, I’d like to script this. I have 24 sites and not all sites had
> outbreaks with the same aetiological agents and, over time, the within-
> site agents may change depending on the time period. That’s a lot of
> recoding if I take a manual approach to write disease specific graphs.
> What I’d like to do is essentially overlays the graphs generated by:
>
> .twoway bar duration index_date if site==2, by(aetiologicagent)
>
> into one graph. I would loop through the instances of the sites to get
> summary graphs for each.
>
> I’ve tried a number of things without achieving the results I wanted.
> The only thing I haven’t tried is to generate binary dummy variables for
> each aetiologic agent and include them all in a graph statement as
> overlays using ||. I’d expect the instances of aetiological agents that
> don’t exist for that institution would not be plotted.
>
> Any suggestions of alternative solutions?
>
>
> - cam
> --
> Cameron McDermaid MHSc
> Epidemiologist
> Ottawa Public Health
> 100 Constellation Cr, Ottawa ON K2G 6J8
> vox: 613-580-6744x15321
> fax: 613-580-9601
>
> The beginning of wisdom is the definition of terms - Socrates
>
>
> This e-mail originates from the City of Ottawa e-mail system. Any
> distribution, use or copying of this e-mail or the information it
> contains by other than the intended recipient(s) is unauthorized.
> If you are not the intended recipient, please notify me at the
> telephone number shown above or by return e-mail and delete
> this communication and any copy immediately. Thank you.
>
> Le présent courriel a été expédié par le système de courriels de
> la Ville d'Ottawa. Toute distribution, utilisation ou
> reproduction du courriel ou des renseignements qui s'y trouvent
> par une personne autre que son destinataire prévu est interdite.
> Si vous avez reçu le message par erreur, veuillez m'en aviser par
> téléphone (au numéro précité) ou par courriel, puis supprimer
> sans délai la version originale de la communication ainsi que
> toutes ses copies. Je vous remercie de votre collaboration.
>
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
This e-mail originates from the City of Ottawa e-mail system. Any
distribution, use or copying of this e-mail or the information it
contains by other than the intended recipient(s) is unauthorized.
If you are not the intended recipient, please notify me at the
telephone number shown above or by return e-mail and delete
this communication and any copy immediately. Thank you.
Le présent courriel a été expédié par le système de courriels de
la Ville d'Ottawa. Toute distribution, utilisation ou
reproduction du courriel ou des renseignements qui s'y trouvent
par une personne autre que son destinataire prévu est interdite.
Si vous avez reçu le message par erreur, veuillez m'en aviser par
téléphone (au numéro précité) ou par courriel, puis supprimer
sans délai la version originale de la communication ainsi que
toutes ses copies. Je vous remercie de votre collaboration.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/