Announcement

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

  • Twoway Histogram

    Dear All,
    I would like to create a two-way histogram for two variables. However, I would like a variable to be in a panel and the other variables being below it. Is it possible? In other words, I would like to obtain the attached graph using two-
    Click image for larger version

Name:	Screenshot 2023-10-22 at 12.50.10 PM.png
Views:	1
Size:	90.7 KB
ID:	1731183
    way histogram. The code I use is

    twoway (histogram pscore if distance>=0, color(green%30)) (histogram pscore if distance<0, color(red%30))

    which of course overlaps the two distributions.

    Thank you very much for considering my request!



  • #2
    Here are some small tricks

    Code:
    sysuse auto, clear
    
    twoway__histogram_gen mpg if foreign, start(10) width(2) freq gen(f1 v1)
    twoway__histogram_gen mpg if !foreign, start(10) width(2) freq gen(f2 v2)
    
    replace f1 = -f1 
    
    twoway bar f1 v1, barw(2) fcolor(stc2*0.2) lcolor(stc2) ///
    || bar f2 v2, barw(2) fcolor(stc1*0.2) lcolor(stc1) legend(order(2 "Domestic" 1 "Foreign")) /// 
    xtitle("`: var label mpg'") yla(-5 "5" 0(5)15)
    Click image for larger version

Name:	upanddownhistogram.png
Views:	1
Size:	35.4 KB
ID:	1731189

    Comment


    • #3
      This works perfectly, thank you so much!

      Comment


      • #4
        Consider also superimposed histograms:

        Code:
        sysuse auto, clear
        
        twoway histogram mpg if foreign, start(10) width(2) freq lcolor(stc2) fcolor(stc2%20) ///
        || histogram mpg if !foreign, start(10) width(2) freq lcolor(stc1) fcolor(stc1%20) ///
        legend(order(2 "Domestic" 1 "Foreign")) xtitle("`: var label mpg'")  
        
        twoway histogram mpg if foreign, start(10) width(2) lcolor(stc2) fcolor(stc2%20) ///
        || histogram mpg if !foreign, start(10) width(2) lcolor(stc1) fcolor(stc1%20) ///
        legend(order(2 "Domestic" 1 "Foreign")) xtitle("`: var label mpg'")
        and indeed quantile plots e.g. https://www.statalist.org/forums/for...lable-from-ssc

        Comment


        • #5
          The use of
          Code:
          twoway__histogram_gen
          in #2 is more evidence why most or all undocumented commands should not be undocumented. See
          Code:
          help undocumented

          Comment


          • #6
            A serious detail about #1 is that red and green together are a big challenge for many people. See e.g. https://www.nei.nih.gov/learn-about-...olor-blindness

            Comment

            Working...
            X