Bookmark and Share

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]

Re: st: RE: Any way to suppress bar value labels in a graph if less than a specified value?


From   Friedrich Huebler <[email protected]>
To   [email protected]
Subject   Re: st: RE: Any way to suppress bar value labels in a graph if less than a specified value?
Date   Fri, 20 Sep 2013 13:25:55 -0400

Matthew,

There is an easier solution. You can loop over the variables, identify
those with a mean<7 and delete the bar labels without the Graph Editor
by using the gr_edit command. The commands below work with your
example data.

Friedrich


sysuse auto, clear
gen pct_standfail = .
gen pct_standpass = .
gen pct_standexcel = .
replace pct_standfail = 4.87 if foreign == 1
replace pct_standpass = 80.88  if foreign == 1
replace pct_standexcel = 14.25 if foreign == 1
replace pct_standfail = 10.22 if foreign == 0
replace pct_standpass = 83.06 if foreign == 0
replace pct_standexcel = 6.72 if foreign == 0

graph bar pct_standfail pct_standpass pct_standexcel,  ///
stack over(foreign, label(labsize(4.5) axis(noline) ) ///
gap(*.5)) outergap(*.75) ///
blabel(bar, position(center) color(white) size(5) format(%9.1f)) ///
bar(1, fcolor(128 0 0) lcolor(black)) ///
bar(2, fcolor(180 180 180) lcolor(black)) ///
bar(3, fcolor(0 0 0) lcolor(black)) ///
title("Performance Standard Y", size(8) margin(0 0 3 0) color (black)) ///
legend(off) ///
yscale(range(0(20)100)) ylabel(0 "" 20 "" 40 "" 60 "" 80 "" 100 "", ///
format(%9.0f) angle(0) labsize(1) labcolor(white) tstyle(major) nogrid) ///
graphregion(color(white) fcolor(white) lcolor(white)) ///
plotregion(color(black) fcolor(white) lcolor(white)) ///
xsize(1.0) ysize(1.7)

levelsof(foreign), local(levels)
local i = 0
foreach l of local levels {
  foreach var of varlist pct_standfail pct_standpass pct_standexcel {
    local i = `i' + 1
    sum `var' if foreign == `l'
    if r(mean)<7 {
      gr_edit .plotregion1.barlabels[`i'].Delete barlabels[`i'] edits
    }
  }
}




On Thu, Sep 19, 2013 at 1:09 PM, Radwin, David <[email protected]> wrote:
> Sorry, I don't think there is a simple solution to your problem, and even this complicated idea is far from a satisfactory solution:
>
> * Reshape data
> drop make-gear
> gen id=12345
> reshape wide pct_*, i(id) j(foreign)
>
> * Label only variables with values greater than 7
> foreach v of varlist pct_* {
>         if `v'[1] > 7 {
>         local `v'lab : display %9.1f `v'[1]
>                 label variable `v' "``v'lab'"
>                 }
>         else {
>                 label variable `v' " "
>                 }
>         }
>
> * Create asis bar graphs using variable labels with -blabel(name)-
> graph bar (asis) pct_standfail0 pct_standpass0 pct_standexcel0,  ///
>         stack  ///
>         outergap(*.75) ///
>         blabel(name, position(center) color(white) size(5) format(%9.1f)) ///
>                 bar(1, fcolor(128 0 0) lcolor(black)) ///
>                 bar(2, fcolor(180 180 180) lcolor(black)) ///
>                 bar(3, fcolor(0 0 0) lcolor(black)) ///
>         title("Performance Standard Y", size(8) margin(0 0 3 0) color(black)) ///
>         legend(off) ///
>         yscale(range(0(20)100)) ylabel(0 "" 20 "" 40 "" 60 "" 80 "" 100"", ///
>         format(%9.0f) angle(0) labsize(1) labcolor(white) tstyle(major) nogrid) ///
>         graphregion(color(white) fcolor(white) lcolor(white)) ///
>         plotregion(color(black) fcolor(white) lcolor(white)) ///
>         xsize(1.0) ysize(1.7) name(graph0, replace) legend(off)
>
> graph bar (asis) pct_standfail1 pct_standpass1 pct_standexcel1,  ///
>         stack  ///
>         outergap(*.75) ///
>         blabel(name, position(center) color(white) size(5) format(%9.1f)) ///
>                 bar(1, fcolor(128 0 0) lcolor(black)) ///
>                 bar(2, fcolor(180 180 180) lcolor(black)) ///
>                 bar(3, fcolor(0 0 0) lcolor(black)) ///
>         title("Performance Standard Y", size(8) margin(0 0 3 0) color(black)) ///
>         legend(off) ///
>         yscale(range(0(20)100)) ylabel(0 "" 20 "" 40 "" 60 "" 80 "" 100"", ///
>         format(%9.0f) angle(0) labsize(1) labcolor(white) tstyle(major) nogrid) ///
>         graphregion(color(white) fcolor(white) lcolor(white)) ///
>         plotregion(color(black) fcolor(white) lcolor(white)) ///
>         xsize(1.0) ysize(1.7) name(graph1, replace) legend(off)
>
> graph combine graph0 graph1
>
>
> P.S. If Colin M. is still around Gwinnett, please say hello for me.
>
> David
> --
> David Radwin
> Senior Research Associate
> Education Studies Division
> RTI International
> 2150 Shattuck Ave., Suite 800
> Berkeley, CA 94704
> Phone: 510-665-8274
>
> www.rti.org
>
>
>> -----Original Message-----
>> From: [email protected] [mailto:owner-
>> [email protected]] On Behalf Of
>> [email protected]
>> Sent: Wednesday, September 18, 2013 12:03 PM
>> To: [email protected]
>> Subject: st: Any way to suppress bar value labels in a graph if less than
>> a specified value?
>>
>> I work in a school district with over 100 schools, and a common task I
>> have
>> is making graphs for annual reports on each school. I use Stata to loop
>> over schools to automate these kinds of repetitive and otherwise extremely
>> time-consuming tasks. A problem I commonly run into, though, is needing to
>> suppress value labels conditional on some rule (e.g., if smaller than x).
>> For instance, I'm currently making a stacked bar chart based on data
>> available in aggregate form only, and I have to suppress value labels in
>> the graph that are below 7.0%. I'm hoping there's a simple solution out
>> there. :) Below is an example of this issue, using the auto dataset.
>>
>> (I'm using Stata 13 MP)
>>
>> sysuse auto, clear
>>
>> * Here, I'm just making up some aggregate data so we end up with a dataset
>> similar to the one I'm working with
>> gen pct_standfail = .
>> gen pct_standpass = .
>> gen pct_standexcel = .
>>
>> replace pct_standfail = 4.87 if foreign == 1
>> replace pct_standpass = 80.88  if foreign == 1
>> replace pct_standexcel = 14.25 if foreign == 1
>>
>> replace pct_standfail = 10.22 if foreign == 0
>> replace pct_standpass = 83.06 if foreign == 0
>> replace pct_standexcel = 6.72 if foreign == 0
>>
>> duplicates drop foreign, force
>>
>> /*
>> Ok, so now we have a wide dataset with each case representing a group
>> (domestic or foreign).
>> And we have 3 variables with values representing percentage of cases in
>> that group that
>> failed/passed/exceeded some standard of performance.
>>
>> Below is my code for a stacked bar chart adapted to this data. I'm using
>> the blabel option to display values
>> in the center of each bar. The challenge I'm having is in figuring out how
>> to suppress the bar label when the value is below 7%
>> I can't seem to figure out a way to do this other than opening the graph
>> and deleting the smaller values
>> by hand. With over 100 schools and many different variations on this kind
>> of graph to produce,
>> I would love a code-based conditional solution that will save me tons of
>> tedious work. :)
>>
>> Any ideas?
>> */
>>
>> graph bar pct_standfail pct_standpass pct_standexcel,  ///
>>       stack over(foreign, label(labsize(4.5) axis(noline) ) ///
>>       gap(*.5)) outergap(*.75) ///
>>       blabel(bar, position(center) color(white) size(5) format(%9.1f)) ///
>>               bar(1, fcolor(128 0 0) lcolor(black)) ///
>>               bar(2, fcolor(180 180 180) lcolor(black)) ///
>>               bar(3, fcolor(0 0 0) lcolor(black)) ///
>>       title("Performance Standard Y", size(8) margin(0 0 3 0) color
>> (black)) ///
>>       legend(off) ///
>>       yscale(range(0(20)100)) ylabel(0 "" 20 "" 40 "" 60 "" 80 "" 100
>> "", ///
>>       format(%9.0f) angle(0) labsize(1) labcolor(white) tstyle(major)
>> nogrid) ///
>>       graphregion(color(white) fcolor(white) lcolor(white)) ///
>>       plotregion(color(black) fcolor(white) lcolor(white)) ///
>>       xsize(1.0) ysize(1.7)
>>
>>
>>
>>
>>
>> Matthew D. Lovelace, Ph.D.
>> Research and Evaluation
>> Gwinnett County Public Schools
>> Instructional Support Center, Suite 1.520
>> 437 Old Peachtree Road NW
>> Suwanee, GA 30024-2978
>> Phone 678.301.7365 | Fax 678.301.7088
>> [email protected]
>>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index