Announcement

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

  • Legend on histogram

    I made this command as a test

    histogram x, normal kdensity freq xaxis(1 2) title("Prueba") ///
    note("Fuente: INEI") ///
    subtitle("Prueba de código") ///
    xtitle("sss", axis(2)) ///
    legend(label(1 "a" 2 "q" 3 "r" )

    everything is ok but it does not show the legend.

  • #2
    We can't see the graph or any of your data.

    Certainly, you need an extra parenthesis for that to run.

    Here is a reproducible fake

    Code:
    sysuse auto, clear
    clonevar x = mpg 
    
    histogram x, normal kdensity freq xaxis(1 2) title("Prueba") ///
    note("Fuente: INEI") ///
    subtitle("Prueba de código") ///
    xtitle("sss", axis(2)) ///
    legend(label(1 "a" 2 "q" 3 "r" ))
    So far as twoway is concerned you are showing one variable, the frequency distribution, so there is no need for a legend.

    What do you think a q r correspond to?

    Comment


    • #3
      hi Mr. Cox, my code is this

      set obs 500
      generate x = rnormal(4,2)
      generate y = 1.25 + 5*x + rnormal(0,1)
      histogram x, xaxis(1 2) freq normal kdensity title("Prueba") ///
      note("Fuente: INEI") ///
      subtitle("Prueba de código") ///
      xtitle("sss", axis(2)) ///
      legend(label(1 "x" 2 "normal" 3 "kdensity") position(6))

      this is the result, as you see, inside are the histogram of x, the normal curve and kdensity, I wanted a legend with those elements inside.

      Click image for larger version

Name:	Graph.png
Views:	2
Size:	39.5 KB
ID:	1688384


      Attached Files

      Comment


      • #4
        I was wrong: histogram switches the legend off and you need to reverse that.

        Code:
        clear 
        set obs 500
        set scheme s1color 
        generate x = rnormal(4,2)
        generate y = 1.25 + 5*x + rnormal(0,1)
        
        
        histogram x, xaxis(1 2) freq normal kdensity title("Prueba") ///
        note("Fuente: INEI") ///
        subtitle("Prueba de código") ///
        xtitle("sss", axis(2)) ///
        normopts(lc(red)) kdenopts(lc(blue)) /// 
        legend(on order(1 2 "kdensity" 3 "normal") row(1) position(6))

        Comment


        • #5
          thank you Mr. Cox

          Comment


          • #6
            Dear members a similar query on keeping common legend and changing position:

            Code:
            twoway (kdensity zwfa if inrange(zwfa,-6, 5) & (round==1 & yc==1), lcolor(blue)    ///
            lpattern(solid)) (hist zwfa if inrange(zhfa,-6, 5) & (round==1 & yc==1),        ///
             color(gs15) lcolor(gs10)), yti("Density", size(small)) ylab(, nogrid)            ///
             xti("Weight-for-age z-score", size(small)) xlab(-6(1)5, nogrid)                ///
             legend(label(1 "Kernel Density") label(2 "Histogram") cols(2) pos(12))                    ///
             by(ctryid, note("")) subtitle(, nobox) name(ap01_uw_r01, replace)
            Why pos(12) not changing the position?

            Stata 18/SE
            Last edited by Mukesh Punia; 31 Jul 2025, 04:51. Reason: added Stata version
            Best regards,
            Mukesh

            Comment


            • #7
              Because you need to specify legend(pos(12)) as a sub-option of the by() option, while the rest of the legend() option can remain as-is. See
              Code:
              help by_option##remarks7
              Here is the relevant extract:
              The issue becomes a little more complicated when, rather than suppressing the legend, we wish to modify the legend's contents or position. Then the legend() option to modify the contents is specified outside the by() and the legend() option to modify the location is specified inside.
              Last edited by Hemanshu Kumar; 31 Jul 2025, 04:59.

              Comment


              • #8
                Thank you Hemanshu Kumar for your prompt response! It worked.
                Best regards,
                Mukesh

                Comment

                Working...
                X