Announcement

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

  • Add additional key to legend

    I'm graphing survey data. On one survey question, no one answered "very often" -- but for consistency I want "very often" to still appear in the key for that question.

    I've been successful adding an additional label ("Very Often") to the legend, but I can't figure out how to make the legend show an additional color.
    Click image for larger version

Name:	Screen Shot 2019-08-07 at 3.25.36 PM.png
Views:	1
Size:	19.6 KB
ID:	1511323


    The graph editor doesn't have an element key[5]:
    Click image for larger version

Name:	Screen Shot 2019-08-07 at 3.25.57 PM.png
Views:	1
Size:	14.3 KB
ID:	1511324


    In the following code, bar(5, color(red))doesn't appear to do anything.
    catplot qid25L, recast(bar) percent asyvars stack over(all) ///
    legend(position(3) cols(1) size(small) order(4 3 2 1 5 "Very Often" )) ///
    b1title("") ///
    bar(1, color(orange)) ///
    bar(2, color(yellow)) ///
    bar(3, color(green)) ///
    bar(4, color(blue)) ///
    bar(5, color(red)) ///
    name(all, replace)


    Is there a way to do this?
    Thanks!
    - Colleen

  • #2
    Please show the results of

    Code:
    tab qid25L
    to give us the basis of a data example and note that I will not be replying further until I wake up.

    Comment


    • #3
      I woke up and no data yet. Still, the answer is to fake a smidgen of data large enough to show up in the legend but small enough not to be a visible bar.

      This is self-contained code that can be run:

      Code:
      clear 
      input whatever freq 
      1  20
      2  19 
      3  17 
      4  12 
      end 
      label define whatever 1 Never 2 Rarely 3 Sometimes 4 Often 5 "Very often" 
      label val whatever whatever 
      expand freq 
      
      catplot whatever, recast(bar) percent asyvars stack ///
      legend(position(3) cols(1) size(small) order(1 2 3 4 5 "Very Often" )) ///
      b1title("") /// 
      bar(1, color(orange)) /// 
      bar(2, color(yellow)) /// 
      bar(3, color(green)) /// 
      bar(4, color(blue)) /// 
      bar(5, color(red))
      
      preserve 
      
      contract whatever 
      expand 2 in L 
      replace whatever = 5 in L 
      replace _freq = 1e-6 in L 
      
      catplot whatever [aw=_freq], recast(bar) percent asyvars stack ///
      legend(position(3) cols(1) size(small)) ///
      b1title("") /// 
      bar(1, color(orange)) /// 
      bar(2, color(yellow)) /// 
      bar(3, color(green)) /// 
      bar(4, color(blue)) /// 
      bar(5, color(red))
      
      restore
      Click image for larger version

Name:	faked.png
Views:	1
Size:	16.8 KB
ID:	1511374


      Comments:

      1. You have an extra variable all that you don't tell us about. It will lead to more complicated code. If you want advice on the complication you'll need to show us data.

      Showing the results of

      Code:
      contract qid25L all 
      dataex 
      would be enough.

      If the command with dataex doesn't work, then you should read

      https://www.statalist.org/forums/help#version
      https://www.statalist.org/forums/help#stata

      as you're not using Stata 16 and should be saying that.

      2. The colour scheme you're using is not a good choice. Red and green are hard to tell apart for many people (informally this is often known as colour blindness) and in any case the colours are not in monotonic sequence despite the use of an ordinal scale.

      3. catplot (from SSC, as you're asked to explain: see the last link above)is a lousy command (I can say that without risk of offending the author, who sympathises with my views on this point) in so far as it makes it easier for people to produce poor graphs. Here the stacked design makes it hard to see small or zero frequencies and to discern small variations that could be interesting or important. I use tabplot (Stata Journal) much more frequently now. For an introduction with discussion of what I see as the major points at issue see https://www.statalist.org/forums/for...updated-on-ssc noting that at the time of writing the most up-to-date public version of the files is on SSC.

      4. If a category never occurs in the data I usually think if that's worth noting it's sufficient to mention it in text.





      Comment


      • #4
        Dear Nick, I comment this post instead of creating a new identical.

        I have the same problem, I plotted some impulse response functions and I had to combine two graphs and their respective legends (different). In order to solve the problem I just include an additional element (line of the second graph) in the legend of the first. However, similar to the original question of this post, I get a neutral symbol (i.e. without the line color, pattern and width of the original line). I highlight in red in the code (i) where I included the additional element in the legend of the first graph, without reference to any line, (ii) the line color, pattern and width of the line in question.

        The code is the following
        Code:
        gen _h = h if h<=8;
        gen _zero = 0;
        
        twoway     (line _zero h if h>=1 & h<=16, lcolor(black) lpattern(solid) ) ///
                (line multexp1  h if h>=1 & h<=16, connect(l) lcolor(navy) lpattern(solid) lwidth(thick)) ///
                (rarea up90fb lo90fb h if h>=1 & h<=16, lcolor(navy%20) fcolor(navy%20) lwidth(none)) ///
                (line up90hb h if h>=1 & h<=16, lcolor(cranberry) lpattern("_###_###") lwidth(medthick)) ///
                (line lo90hb h if h>=1 & h<=16, lcolor(cranberry) lpattern("_###_###") lwidth(medthick)) ///
                (scatter multrec1  h if h>=1 & h<=16, connect(l) lcolor(cranberry) lpattern(solid) lwidth(thick) msymbol(C) msize(medlarge) mcolor(cranberry)), ///
                xscale(range(1 16)) xlabel(2(2)16) xtitle("quarter") ylabel(, format(%03.1f) angle(0)) legend(order(2 "High private debt" 6 "Low private debt" 1 "Difference")  size(small)) graphregion(color(white)) bgcolor(white);
                graph rename full_model, replace;
        
        twoway    (line _zero h if h>=1 & h<=16, lcolor(black) lpattern(solid)) ///
                (rarea up90diff lo90diff h if h>=1 & h<=16, fcolor(eltgreen%50) lcolor(eltgreen%50) lwidth(none)) ///
                (line diff1  h if h>=1 & h<=16, lcolor(green) lpattern("longdash") lwidth(thick)) ///
                ,xscale(range(1 16)) xlabel(2(2)16) xtitle("quarter") ylabel(, format(%03.1f)  angle(0)) legend(order(3 "Difference")) graphregion(color(white)) bgcolor(white);
                graph rename diff_model, replace;
                
        
        grc1leg2 full_model diff_model, title("US `graphname' multiplier",size(medsmall) span) rows(1) c(1) xsize(2.5) legendfrom(full_model)  lcols(3) ltsize(small)  graphregion(color(white)) graphregion(margin(zero));
        I would like to automatize the code so I don't need to manually change the line color, pattern and width in the legend for the added element ("Difference").

        Let me attach also a figure in case I didn't make clear my point.
        Graph.pdf

        Thanks,
        Alessandro
        Attached Files

        Comment

        Working...
        X