Announcement

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

  • Graph combine bar plots and make them in same horizontal line

    Dear Stata users,

    Let me put my question simply. I want to combine two bar plots, one of them have a split and multiline x-axis labels. In the resulting graph, this two bar plots have different zero starting heights. So, is there anyway to make them starting at the same horizontal line? Thank you.
    Note: In the following code, I use -splitvallabels- command written by Nicholas Winter & Ben Jann from SSC.

    Code:
    sysuse lifeexp
    xtile gngroup=gnppc, n(5)
    label define gngroup 1 "this is a long label" 2 "this label is long" 3 "this long label" 4 "long label it is" 5 "long labels too"
    label values gngroup gngroup
    splitvallabels gngroup, length(4) local(gngroup) nobreak
    graph bar lexp, over(region) name(g1)
    graph bar lexp, over(gngroup, relabel(`gngroup')) name(g2)
    graph combine g1 g2, ycommon
    Click image for larger version

Name:	long.png
Views:	1
Size:	55.5 KB
ID:	1637070

    Last edited by Chen Samulsion; 18 Nov 2021, 07:17. Reason: to note user-written command from SSC

  • #2
    Hi Chen,

    If you add the following prior to producing the graphs:

    Code:
    label define region 1 `""Europe" "&" "C. Asia" " ""' 2 `""North" "America" " " " ""' 3 `""South" "America" " " " ""', modify
    It will adjust the labels enough to make them align like the below:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	42.5 KB
ID:	1637077


    - joe
    Joseph Nicholas Luchman, Ph.D., PStatĀ® (American Statistical Association)
    ----
    Research Fellow
    Fors Marsh

    ----
    Version 18.0 MP

    Comment


    • #3
      Joseph Luchman's excellent advice is not contradicted by advising scrutiny of https://www.stata-journal.com/articl...article=gr0085

      Comment


      • #4
        You can restructure the data to use -graph, by()- rather than -graph combine-

        Using Asjad Naqvi -schemepack-

        Code:
        sysuse lifeexp,clear
        xtile gngroup=gnppc, n(5)
        
        preserve
        keep region lexp
        replace region = region + 5
        rename region over
        save temp, replace
        restore
        keep gng lexp
        rename gng over
        append using temp, gen(tag)
        
        label define over 1 `"this is a long label"', modify
        label define over 2 `"this label is long"', modify
        label define over 3 `"this long label"', modify
        label define over 4 `"long label it is"', modify
        label define over 5 `"long labels too"', modify
        label define over  6 `"Europe & C. Asia"', modify
        label define over 7 `"North America"', modify
        label define over 8 `"South America"', modify
        label values over over
        splitvallabels over if over<6, length(4) local(gngroup) nobreak
        
        graph bar lexp, over(over, relabel(`gngroup')) /// 
           by(tag,  note("")) nofill subtitle("") scheme(swift_red)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	19.4 KB
ID:	1637080

        Comment


        • #5
          Hi, Joseph Luchman Nick Cox Scott Merryman, thank you all of you so much! Wonderful solutions! You all have helped me out of a difficult situation in my writing of report.

          Comment

          Working...
          X