Hello,
I am creating stacked bar graphs in STATA to display the results of testing samples for several salmonella serotypes, across 6 years. My current code creates a bar graph which is close to what I want but needs a few alterations – the main one being we are trying to create a code that is universal and can be applied to different datasets and still produce the same graph? The issue is that the number and type of variables change (some datasets may have more salmonella serotypes than others, other datasets have slightly different serotypes).
The example here is a subset of data and sometimes we can have 30+ different serotypes to graph. One short cut I found was to list the variables as <negative-hadar> instead of listing all variables (<negative typhimurium heidelburg enteritidis hadar). But wondering if here is a better way I can tell STATA to graph all variables in the dataset, without having to change the code each time to reflect different variables?
An example of how my data looks:
* Example generated by -dataex-. For more info, type help dataex
My code to generate the bar graph:
My graph looks like this:

I was also wondering:
- Is there a way to label only those samples that tested negative? The blabel option labels every bar and I cannot figure out a way to label only the bars that represent negative samples. Is there there a way to label only a portion of bars on the graph?
- How do I get the bar graph to not graph salmonella serotypes that have a value of 0? One way I see to do this is to change all zero values to missing values with the following code: <mvdecode _all, mv(0)> before inputting my graph code. Since bar graph does not graph missing data this works. But I was wondering if there is a better or different way to achieve this?
Thanks.
I am creating stacked bar graphs in STATA to display the results of testing samples for several salmonella serotypes, across 6 years. My current code creates a bar graph which is close to what I want but needs a few alterations – the main one being we are trying to create a code that is universal and can be applied to different datasets and still produce the same graph? The issue is that the number and type of variables change (some datasets may have more salmonella serotypes than others, other datasets have slightly different serotypes).
The example here is a subset of data and sometimes we can have 30+ different serotypes to graph. One short cut I found was to list the variables as <negative-hadar> instead of listing all variables (<negative typhimurium heidelburg enteritidis hadar). But wondering if here is a better way I can tell STATA to graph all variables in the dataset, without having to change the code each time to reflect different variables?
An example of how my data looks:
* Example generated by -dataex-. For more info, type help dataex
Code:
clear input int year byte(negative typhimurium anatum enteritidis hadar) 2010 20 1 1 2 0 2011 18 2 1 2 3 2012 36 4 0 2 3 2013 29 0 1 0 4 2014 23 2 0 1 2 end
My code to generate the bar graph:
Code:
graph bar (asis) negative-hadar, over(year, label(labsize(vsmall))) stack /// legend (size(small)) legend(cols(4)) ylabel(,angle(horizontal) labsize(vsmall)) /// graphregion(color(white)) ytitle("Number of Samples", margin(medium)) /// blabel(bar,position(center) color(white))
My graph looks like this:
I was also wondering:
- Is there a way to label only those samples that tested negative? The blabel option labels every bar and I cannot figure out a way to label only the bars that represent negative samples. Is there there a way to label only a portion of bars on the graph?
- How do I get the bar graph to not graph salmonella serotypes that have a value of 0? One way I see to do this is to change all zero values to missing values with the following code: <mvdecode _all, mv(0)> before inputting my graph code. Since bar graph does not graph missing data this works. But I was wondering if there is a better or different way to achieve this?
Thanks.
Comment