Hello -
I'm using Stata 14.2 on Windows 10, working with household production data collected in 5 different years. I have an interest in showing on the same figure: 1) participation in a particular production activity by geographical region over time (e.g., the number of people in Region A who fish each year); and 2) the average production of a product by region over time (e.g., average income from fish by region and by year).
We'll use fishing as an example figure. In my bar graph, I would like to have two sets of bars, which are graphed over(year) over(Region). The first, should show the relative shares of fishermen vs. non-fishermen. The second should show average income from fish production. I can easily create these two bar graphs separately, but would like to show them on the same figure.
For easy replication, I've adapted one of Stata's built-in datasets to replicate the structure of my dataset. In this example, I'm trying to graph the share of interviewees who were married on one bar and the average age of interviewees on another. Not a relevant example, but the structure of the problem is the same.
The code has been annotated to describe the adaptations I made to the example dataset and to highlight the graphing issues I'm facing. Included are the three ways I could figure to try and produce this graph, none of which have been successful, thus far.
I believe my issue stems from mixing available options between graph twoway bar and graph bar. It seems that "add plot" and "|| bar" are twoway options, and therefore don't work with graph bar, however graph twoway bar doesn't allow for the stack option, which is necessary to produce the desired figure.
Does anyone know of a way around this? Or a way to finaggle Stata into allowing for more sophisticated multi-plotting with graph bar? Any guidance you can give is much appreciated.
I'm using Stata 14.2 on Windows 10, working with household production data collected in 5 different years. I have an interest in showing on the same figure: 1) participation in a particular production activity by geographical region over time (e.g., the number of people in Region A who fish each year); and 2) the average production of a product by region over time (e.g., average income from fish by region and by year).
We'll use fishing as an example figure. In my bar graph, I would like to have two sets of bars, which are graphed over(year) over(Region). The first, should show the relative shares of fishermen vs. non-fishermen. The second should show average income from fish production. I can easily create these two bar graphs separately, but would like to show them on the same figure.
For easy replication, I've adapted one of Stata's built-in datasets to replicate the structure of my dataset. In this example, I'm trying to graph the share of interviewees who were married on one bar and the average age of interviewees on another. Not a relevant example, but the structure of the problem is the same.
The code has been annotated to describe the adaptations I made to the example dataset and to highlight the graphing issues I'm facing. Included are the three ways I could figure to try and produce this graph, none of which have been successful, thus far.
Code:
use http://www.stata-press.com/data/r14/nlswork4.dta //Drop observations to limit dataset to 5 years drop if year>72 //Reshape to wide and back to long to make sure every id is listed for every year reshape wide age msp ttl_exp ln_wage, i(idcode) j(year 68 69 70 71 72) reshape long age msp ttl_exp ln_wage, i(idcode) j(year) //Generate dummy variable for whether they were interviewed or not gen interview=0 replace interview=1 if age!=. order interview, after(year) //Create variable for # of people interviewed by year bys year: egen NumInterviewed=count(interview) //Create variable for # of people married, spouse present by year bys year: egen NumMSP=count(msp) //Create variable for share of interviewees who were married, spouse present by year bys year: gen msp_share=(NumMSP/NumInterviewed) //And vice-versa bys year: gen nonMsp_share=(1-(NumMSP/NumInterviewed)) //Stacked bar graph to show share who were married, spouse present graph bar msp_share nonMsp_share, stack over(year) //Basic bar graph to show average age graph bar age, over(year) //Graph share of MSP and average age on same graph //Attempt 1 graph bar msp_share nonMsp_share, stack over(year) /// || bar age, over(year) **Says 'invalid 'over', so try getting rid of "over" option to see if it works //Attempt 2 graph bar msp_share nonMsp_share, stack /// || bar age **Says 'option | not allowed' //Attempt 3 graph bar msp_share nonMsp_share, stack over(year) /// addplot(graph bar age, over(year))
Does anyone know of a way around this? Or a way to finaggle Stata into allowing for more sophisticated multi-plotting with graph bar? Any guidance you can give is much appreciated.
Comment