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

          Working...
          X