Announcement

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

  • Use var label on graph legend

    Hi to all
    Im doing a bar graph to show the proportion of observations that have an atribute overal several dummy variables

    My syntax is:

    graph bar plot_tree_sec_10 plot_tree_sec_26 plot_tree_sec_21 plot_tree_sec_500 plot_tree_sec_17 plot_tree_sec_6 plot_tree_sec_46 plot_tree_sec_29 plot_tree_sec_47 plot_tree_sec_31 plot_tree_sec_40 plot_tree_sec_42 plot_tree_sec_39 plot_tree_sec_48 plot_tree_sec_7 , scheme(burd) ytitle(Proportion hh) blabel(bar, format(%12.2f)) legend(pos(6) row(3) ring() size(*.70) symx(*.70) symy(*.70) forcesize ) title(Species in Agrarian Plot, size(medium) position(12))

    The graph works as I intended, however i have a problem with the legend. The legend for each variable is "mean of varname (ie: mean of plot_tree_sec_10").
    Is there is any way of change the labels of the legend to use the varlabel??

    I know that can maually change the label in the legend using: legend(label(1 "Varlabel"....)), but i dont want to do it manually since i may have to change the order of the variables several times

    Thanks in advance

  • #2
    I agree that you should want to cut out that repetition. But surely

    1. Your readers don't want to see ugly text like plot_tree_sec_10 either.

    2. A legend with 15 entries will look horrible too, and is not needed either.

    Note that the graph will show means and presumably that is what you want. So, get a set-up where the means are the data. That is,
    collapse first.

    I don't know what your variables are quite. Species in agrarian plot sounds right, but not Proportion hh. No matter. Here is some technique. First I fake a dataset, absent a data example.


    Code:
    clear
    set obs 10
    set seed 2803
    local j = 1
    foreach v in  10 26 21 500 17 6 46 29 47 31 40 42 39 48 7 {
        gen plot_tree_sec_`v' = `j' * runiformint(1, 10)
        local ++j
    }
    
    * this is where you start
    preserve
    
    collapse plot_tree_sec_*
    
    gen id = 1
    
    reshape long plot_tree_sec_, i(id) j(section)
    
    set scheme s1color
    
    graph bar plot_tree_sec_, over(section, sort(1)) ytitle(Something sensible goes here) title(Use a good overall title)
    Code:
    
    restore 
    Click image for larger version

Name:	somethingsensible.png
Views:	1
Size:	26.2 KB
ID:	1575678



    Naturally, you want different cosmetic details.


    Last edited by Nick Cox; 05 Oct 2020, 14:15.

    Comment


    • #3
      Thanks
      At the end I did something kind of similar to what you mention (use collpase) while keep in the labels

      Code:
      *Keep the varlabels
      foreach     v of varlist plot_tree_sec_10 plot_tree_sec_26 plot_tree_sec_21 plot_tree_sec_500 plot_tree_sec_17 plot_tree_sec_6 plot_tree_sec_46 plot_tree_sec_29 plot_tree_sec_47 plot_tree_sec_31 plot_tree_sec_40 plot_tree_sec_42 plot_tree_sec_39 plot_tree_sec_48 plot_tree_sec_7  {
                  local `v'_label: var label `v'
      }
      
      *Collapse
      collapse    (mean) plot_tree_sec_10 plot_tree_sec_26 plot_tree_sec_21 plot_tree_sec_500 plot_tree_sec_17 plot_tree_sec_6 plot_tree_sec_46 plot_tree_sec_29 plot_tree_sec_47 plot_tree_sec_31 plot_tree_sec_40 plot_tree_sec_42 plot_tree_sec_39 plot_tree_sec_48 plot_tree_sec_7
      
      *Restore labels
      foreach v of varlist plot_tree_sec_10 plot_tree_sec_26 plot_tree_sec_21 plot_tree_sec_500 plot_tree_sec_17 plot_tree_sec_6 plot_tree_sec_46 plot_tree_sec_29 plot_tree_sec_47 plot_tree_sec_31 plot_tree_sec_40 plot_tree_sec_42 plot_tree_sec_39 plot_tree_sec_48 plot_tree_sec_7  {
          label var `v' `"``v'_label'"'
      }
      
      *Graph
      graph         bar (asis) plot_tree_sec_10 plot_tree_sec_26 plot_tree_sec_21 plot_tree_sec_500 plot_tree_sec_17 plot_tree_sec_6 plot_tree_sec_46 plot_tree_sec_29 plot_tree_sec_47 plot_tree_sec_31 plot_tree_sec_40 plot_tree_sec_42 plot_tree_sec_39 plot_tree_sec_48 plot_tree_sec_7    , scheme(burd)  ytitle(Proportion of hh)  blabel(bar,  format(%12.2f)) legend(pos(6) row(3) ring() size(*.70) symx(*.70) symy(*.70) forcesize  )  title(Species in Agrarian plot, size(medium) position(12))


      Click image for larger version

Name:	Example.png
Views:	1
Size:	64.3 KB
ID:	1575811


      *I was ask to keep the 15 labels, and since they are just 1-2 words, i dont think it look horrible
      Thanks for the help

      Comment


      • #4
        graph hbar is a another way to avoid a legend (which I still recommend against);

        Comment


        • #5
          That is, if you have value labels defined they will automatically show up on the categorical axis with graph hbar You can get all the information you are showing in the legend -- shown readably -- without the "back and forth" of pairing up legend items and bars.

          Comment

          Working...
          X