Announcement

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

  • #16
    Hi, Nick, I will do that next time.
    HTML Code:
    I found this at <reference> (original in Chinese).
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

    Comment


    • #17
      Philip Ender's histbox command was wrote in 2002 under Stata version 7. Histogram with a boxplot above or beside it, or scatter plot combine with density plot is very popular now, especially in R. If Ender's histbox can be rewrote now, I think it will be quite convenient than codes that provided by Eric in #10 and #14.

      https://r-graph-gallery.com/277-marg...r-ggplot2.html

      https://jtr13.github.io/cc21fall2/tu...tribution.html

      http://domenicovistocco.it/en/dv-blog/ggside/

      https://www.geeksforgeeks.org/r-ggplot2-marginal-plots/

      https://www.r-bloggers.com/2021/05/g...ot2-extension/

      Code:
      *! version 1.1 (14Nov02) pbe
      program define histbox
        version 7.0
        syntax varlist(max=1) [if] [in] [fweight] [, Mean by(string) * ]
        if `"`by'"' != "" {
          window manage forward results
          display as err "by option not allowed"
          exit 198
        }
        tokenize `varlist'
        gph open
        capture graph `1' `if' `in' [`weight' `exp'], hist bbox(600,0,23063,32000,850,390,0) `options'
        if _rc>0 {
          window manage forward results
          display as help "r(198)"
          exit
        } 
        local ay = r(ay)
        local by = r(by)
        local ax = r(ax)
        local bx = r(bx)
        local r1 = 50
        local r2 = 600
        local rc = (`r2'-`r1')/2+`r1'
        quietly summarize `1' `if' `in' [`weight' `exp'], detail
        local q1 = r(p25)
        local q2 = r(p50)
        local q3 = r(p75)
        local qm = r(mean)
        local ql = r(min)
        local qu = r(max)
        local cl = `ax'*`ql'+`bx'
        local c1 = `ax'*`q1'+`bx'
        local c2 = `ax'*`q2'+`bx'
        local c3 = `ax'*`q3'+`bx'
        local cu = `ax'*`qu'+`bx'
        local cm = `ax'*`qm'+`bx'
        gph pen 2
        gph line  `r1' `c1' `r1' `c3' 
        gph line  `r2' `c1' `r2' `c3'
        gph line  `r1' `cl' `r2' `cl'
        gph line  `r1' `c1' `r2' `c1'
        gph line  `r1' `c2' `r2' `c2'
        gph line  `r1' `c3' `r2' `c3'
        gph line  `r1' `cu' `r2' `cu'
        gph line  `rc' `cl' `rc' `c1'
        gph line  `rc' `c3' `rc' `cu'
        if "`mean'"~="" {
          gph pen 3
          gph point  `rc' `cm' 0 6
        }
        gph close 
        window manage forward graph
      end

      Comment


      • #18
        In my view rewriting histbox has no appeal over using existing functionality (Stata 8 on) to produce what you want, which is often already supplied by community-contributed commands in any case. That could well mean using one twoway command and anything else added through an addplot() option or a linked twoway call or calls Adding marginal box plots calls minimally for knowing the five summary points minimum, maximum, median and quartiles.

        https://www.statalist.org/forums/for...ercentile-sets

        is an illustration of this approach and many others could be cited.

        Comment


        • #19
          It is interesting to return to a post to which I earlier contributed in #10 and #14.
          Nick's remark that other approaches have since been proposed is more than appropriate.
          Actually, the community contributed package grinset by Ben Jann is an example that facilitates this particular request very well. First install that package by using:
          Code:
          ssc install grinset, replace
          h grinset
          Now we can work on the required example and the wish for a more concise syntax, that is:
          Code:
          * Load the example dta file:
          sysuse nlsw88, clear
          
          * Create the main plot
          tw scatter wage hours, graphreg(fc(white) lc(white) ilc(white) m(r+10 t+10)) plotreg(m(none) lc(none)) ylab(, nogextend) xlab(, nogextend) ysca(noex)
          
          * Create the first inset (i.e. graph box) & next adjust its location and size as to position it at the right side of the main plot
          grinset t=3.5 r=2: graph box wage, scale(1) plotreg(m(none) lc(none)) ysca(off)
          grinset t=13.5 r=2, size(74 5) scale(1)
          
          * Create the second inset (i.e. graph hbox) & next adjust its location and size as to position it at the top side of the main plot
          grinset t=5 l=13: graph hbox hours, scale(1) plotreg(m(none) lc(none)) ysca(off)
          grinset t=2.7 l=13, size(8.3 84) scale(1)
          Which results in:
          Click image for larger version

Name:	ExampleScatterBoxGrinset_2.png
Views:	1
Size:	109.6 KB
ID:	1771794



          The Stata user is not totally relieved of all effort as it does take some time to adjust each inset as to what is preferred.
          But compared to the 'heavy lifting' documented in #10 and #14 it is certainly an improvement.
          Using the regular options of twoway, graph box & hbox and grinset facilitates full control of the various elements to be plotted and enables reproducible results.

          For more information about grinset I refer to Ben's Github page and about the methodology behind it, I refer to Tibbles and Melse (2023).
          http://publicationslist.org/eric.melse

          Comment


          • #20
            Thank you Nick Cox and ericmelse. Eric's code is great for me to learn grinset. I have noticed Ben's grinset and remembered that he posted an introduction of this command in Statalist. https://www.statalist.org/forums/for...lable-from-ssc And ericmelse seems a strongly advocate for it. https://www.statalist.org/forums/for...hs-using-stata Why I didn't use grinset? I think it is of a little complicated, athough it is worth taking time to learn this powerful command. And as to the question of histbox command here, if it is rewrote to catch new Stata, it will be convenient than resorting to grinset at least in this case. All in all, I decide to use grinset afterwards.
            Last edited by Chen Samulsion; 29 Jan 2025, 19:13.

            Comment


            • #21
              In practice, anyone who knows enough about Stata graphics before Stata 8 to understand the code for histbox would rather write code for the more recent Stata graphics -- or has already done so.

              Comment

              Working...
              X