Announcement

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

  • graph combine

    Hello,

    I have 5 graphs that I am combining into one as in the picture below. I would like to have the x-axis visually aligned, but I haven’t figured out a way to do so. Does anyone have a suggestion for code to do this? I am using Stata 18.

    This is a sample of my code for each of the 5 graphs (the only things that change from one graph to another are the title and xlabels; all other options are the same):

    Code:
    marginsplot, recast(bar) ylabel(0(0.2)0.6) title("") ylabel(, nogrid) xlab(, nogrid) ytitle(Pr(needs)) plotopts(barw(1.0) fcolor("gray") lcolor("black")) ysize(7) xsize(12) aspect(2) ciopts(lcolor(black)) xtitle("", color(black)) xlabel(1 "White" 2 "Black" 3 "Hispanic" 4 "Other", valuelabel angle(45) labsize(vsmall)) title("Race and Ethnicity", nospan size(medsmall) color(black))
    This is my code used to create the combined one:

    Code:
    graph combine 1.gph 2.gph 3.gph 4.gph 5.gph, ycommon rows(1) ysize(7) xsize(12)


    Click image for larger version

Name:	sample.png
Views:	1
Size:	75.4 KB
ID:	1777234




  • #2
    Tinkering with graph combine details will not, I fear, improve the several problems you have here very much.

    If this were my task I would assemble a new dataset and produce a single display. The code gives some flavour. You need 16 observations, but should add gaps to the axis numbering.

    Here, as almost always, or at least commonly:

    * axis labels on a slant solve one problem but create another

    * comparing different estimates is frustrated mightily without a shared scale.

    * bars starting at zero over-emphasise comparisons with zero; usually comparison of estimates with each other is the central concern

    * bars touching is not much of a distraction, but not needed either

    I made the numbers up, and didn't attempt to read them off your graphs. Naturally you need to add more data, as well as the correct values.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(which variable) str9 category float(estimate upper lower)
    1 1 "white"    .55       .75 .45
    2 1 "black"     .4        .6  .3
    3 1 "Hispanic"  .2        .4  .1
    4 1 "other"     .1        .3 .05
    6 2 "straight" .55       .75 .45
    7 2 "gay"       .3        .5  .2
    8 2 "bisexual"  .5        .7  .4
    9 2 "other"    .85 .95  .75
    end
    
    clonevar WHICH = which 
    
    forval i = 1/8 { 
        local this = which[`i']
        local lbl = category[`i']
        label define WHICH `this' "`lbl'", add
    }
    
    label val WHICH WHICH
    
    levelsof WHICH, local(yla)
    
    capture set scheme stcolor 
    
    twoway scatter WHICH estimate, ysc(reverse) yla(`yla', ang(h) valuelabel noticks) ytitle("") || rcap lower upper WHICH, horizontal legend(off) xsc(alt) xtitle(Probability)
    Click image for larger version

Name:	onegraph.png
Views:	1
Size:	32.5 KB
ID:	1777238

    Comment


    • #3
      Thank you very much for the suggestion! I will attempt to replicate what you have done here with my data.

      Comment


      • #4
        Hello! I was able to apply your code to my data using the following code:

        Code:
        clear
        input float(which variable) str20 category float(estimate upper lower)
        1 1 "White"           .3098777   .2857953    .3339601
        2 1 "Black"           .4111989   .3447336    .4776641
        3 1 "Hispanic"        .4495887   .4070162    .4921613
        4 1 "Other"           .3784133   .3214117    .4354148
        
        7 2 "Straight"        .3722547   .3482369    .3962724
        8 2 "Gay"             .4308176   .3322067    .5294285
        9 2 "Bisexual"        .1735612   .1072834    .239839
        10 2 "Other"            .2354152   .1267703    .3440602
        
        13 3 "Employed"       .3181715   .2748631    .36148
        14 3 "Not employed"   .376631    .3486276    .4046343
        
        17 4 "Married"        .4433231   .3893667    .4972795
        18 4 "Cohabiting"     .322586    .2642459    .3809261
        19 4 "Wid/Div/Sep"    .2422118   .1611492    .3232744
        20 4 "Never married"  .3224436   .2862682    .3586191
        
        23 5 "No children"    .3378785   .3062092    .3695478
        24 5 "Has children"   .4110671   .3636095    .4585247
        
        end
        
        clonevar WHICH = which 
        
        forval i = 1/24 { 
            local this = which[`i']
            local lbl = category[`i']
            if "`this'" != "." {
            label define WHICH `this' "`lbl'", add
            }
        }
        
        label val WHICH WHICH
        
        levelsof WHICH, local(yla)
        
        *capture set scheme s1color 
        capture set scheme stblack 
        
        twoway scatter WHICH estimate, ysc(reverse) yla(`yla', ang(h) valuelabel noticks) ytitle("") || rcap lower upper WHICH, horizontal legend(off) lcolor(black) xsc(alt) xtitle(Predicted Probability) msymbol(circle) mcolor(black) xlabel(0(.1).6)
        Here is the graph it produced:

        Click image for larger version

Name:	sample graph.png
Views:	1
Size:	32.5 KB
ID:	1777725


        I am still struggling to get the markers show as black as opposed to green, as the
        Code:
        mcolor(black)
        doesn't appear to get me there.

        Comment


        • #5
          Good job. In essence, mcolor() belongs with scatter, not rcap.

          Here is some optional extra code. Stop at G1 if you prefer it to G2 (shown here).


          Code:
          clear
          input float(which variable) str20 category float(estimate upper lower)
          1 1 "White"           .3098777   .2857953    .3339601
          2 1 "Black"           .4111989   .3447336    .4776641
          3 1 "Hispanic"        .4495887   .4070162    .4921613
          4 1 "Other"           .3784133   .3214117    .4354148
          
          7 2 "Straight"        .3722547   .3482369    .3962724
          8 2 "Gay"             .4308176   .3322067    .5294285
          9 2 "Bisexual"        .1735612   .1072834    .239839
          10 2 "Other"            .2354152   .1267703    .3440602
          
          13 3 "Employed"       .3181715   .2748631    .36148
          14 3 "Not employed"   .376631    .3486276    .4046343
          
          17 4 "Married"        .4433231   .3893667    .4972795
          18 4 "Cohabiting"     .322586    .2642459    .3809261
          19 4 "Wid/Div/Sep"    .2422118   .1611492    .3232744
          20 4 "Never married"  .3224436   .2862682    .3586191
          
          23 5 "No children"    .3378785   .3062092    .3695478
          24 5 "Has children"   .4110671   .3636095    .4585247
          
          end
          
          clonevar WHICH = which
          
          forval i = 1/24 {
              local this = which[`i']
              local lbl = category[`i']
              if "`this'" != "." {
              label define WHICH `this' "`lbl'", add
              }
          }
          
          label val WHICH WHICH
          
          levelsof WHICH, local(yla)
          
          *capture set scheme s1color
          capture set scheme stblack
          
          twoway scatter WHICH estimate, ysc(reverse) yla(`yla', ang(h) valuelabel noticks) mcolor(black) ytitle("") || rcap lower upper WHICH, horizontal legend(off) lcolor(black) xsc(alt) xtitle(Predicted Probability) xlabel(0(.1).6) name(G1, replace)
          
          local extra ymla(2.5 "Ethnicity" 8.5 `" "Sexual" "identity" "' 13.5 "Employed" 18.5 `" "Marital" "status" "' 23.5 "Children?", labsize(medsmall) tlength(*35) tlc(none))
          
          twoway scatter WHICH estimate, ysc(reverse) yla(`yla', ang(h) valuelabel noticks) mcolor(black) ytitle("") || rcap lower upper WHICH, horizontal legend(off) lcolor(black) xsc(alt) xtitle(Predicted Probability) xlabel(0(.1).6) ysc(r(. 25)) `extra' name(G2, replace)
          Click image for larger version

Name:	thisG2.png
Views:	1
Size:	38.4 KB
ID:	1777731

          Comment


          • #6
            Thank you! I appreciate it!

            Comment


            • #7
              Hello, I'd like to give proper credit in helping me create this figure. Is there a particular way to cite this from the listserv?

              Comment


              • #8
                That's a generous impulse, but much depends on what form of publication (making public) you're talking about. Journal paper, presentation, thesis or dissertation, and so on?

                Most reviewers of papers would (I guess) not be positive about referring to Statalist posts as if they were publications -- if indeed they looked carefully at your references.

                I doubt that anyone would seek a reference on plotting confidence intervals as error bars. Oddly enough I have no idea when that practice started.

                The most unusual Stata trick in the code is setting up an axis variable with gaps rather than using a by() option.

                I would imagine that a mention in Acknowledgments would be as much as anyone will expect (or allow).

                By the way, Statalist has not been a listserv since 2014. I would call it a web forum.

                Comment

                Working...
                X