Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding total obs. count for a variable in Box-Plot vertical axis

    I'm graphing the following
    Code:
    graph hbox mean_stay, over(geo)
    This produces 7 different box-plots, one for each of 7 geo-regions. How can I add the count of observations for a given variable for each region to the vertical axis?

    Ideally, my vertical axis would have, for each plot, the geo-region name (because I'm using 'over(geo)') and next to it, or slightly underneath, the total number of observations of 'mean_stay' for that given location. This total number is essentially the total number of observations used to generate each box plot.

    I did try
    Code:
    graph hbox mean_stay, over(obs_count) over(geo)
    (as well as putting over(geo) before) and the result is a mess and everything's illegible. I also played around by adding a
    Code:
    marker()
    to denote the obs_count, but this counts the total number of obs. in each region with a specific "mean_stay", which is not what I want.

    I know I could add a legend and place the geolocations there, while keeping the counts in the vertical axis; but I want to avoid using legends since I need each graph to be compact as I'm generating this same graph over many other by()'s , to ultimately combine them in one huge graph.


  • #2
    I can think of four ways forward and you may not like any of them.

    0. Program the value labels of your categorical variable to include their frequency.

    1. Use text() options with graph hbox to add text.

    2. Create your own box plot program. Hints within

    SJ-13-2 gr0039_1 . Speaking Stata: Creating and varying box plots: Correction
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q2/13 SJ 13(2):398--400 (no commands)
    corrects error in code given

    SJ-9-3 gr0039 . . . . . . . . Speaking Stata: Creating and varying box plots
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q3/09 SJ 9(3):478--496 (no commands)
    explains how to use egen to calculate the statistical
    ingredients needed for box plots and variations of box
    plots; shows the use of twoway to then create the plots

    3. Use stripplot (SSC). This demonstration draws boxplots one way; there are others.

    Code:
    sysuse auto, clear
    bysort rep78 : gen count = _N
    gen where = 43
    stripplot mpg, over(rep78) box pctile(5) xla(10(10)40) xsc(r(. 45)) mcolor(magenta) ///
    addplot(scatter rep78 where, ms(none) mla(count) mlabsize(*1.5))
    Click image for larger version

Name:	yetanotherboxplot.png
Views:	1
Size:	14.9 KB
ID:	1373618

    Last edited by Nick Cox; 09 Feb 2017, 11:22.

    Comment


    • #3
      I didn't know about stripplot. Thanks!

      One question on your first step "Program the value labels of categorical variables".

      I'm working with the loop you shared in another thread:
      Code:
      egen group = group(location), label su group, meanonly forval j = 1/`r(max)' { preserve local label : label (group) `j' keep if group == `j' graph hbox mean_stay, over(geo) text(frequency) subtitle("`label'") name(g`j') local names `names' g`j' restore }
      graph combine `names'
      How can I calculate the frequency of each `j' (i.e. group) in the above, so that it counts the no. of observations by "geo" but just for that `j' (i.e. group). My aim is, as you suggest, add it as text.
      Last edited by Ernestina delPiero; 09 Feb 2017, 12:39.

      Comment


      • #4
        Some technique:

        Code:
        sysuse auto , clear 
        
        clonevar foreign_2 = foreign 
        levelsof foreign_2, local(values) 
        
        quietly foreach v of local values { 
              count if foreign_2 == `v'
              local label : label (foreign_2) `v' 
              label def foreign_2 `v' "`label' (`r(N)')" , modify 
        } 
        
        label val foreign_2 foreign_2 
        
        graph hbox mpg, over(foreign_2)

        Comment

        Working...
        X