Announcement

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

  • Combine Graphs with conditions

    Hi,

    I created two graphs using the following commands

    Graph 1
    Code:
    graph hbar (asis) avg? , over(yaxis,sort(1) descending) ytitle("") ylab(, valuelabel grid) plotregion( margin(large)) plotregion(style(none)) blabel(bar,format(%3.0f)) graphregion(color(white)) bar(1,color(green)) bar(2,color(red)) nolab legend(off) name(left)
    Graph 2
    Code:
    graph hbar (asis) score? , over(yaxis) ytitle("") plotregion( margin(large)) plotregion(style(none)) blabel(bar,format(%3.0f)) graphregion(color(white)) bar(1,color(green)) bar(2,color(red)) nolab legend(off) name(right)
    Now what I want is;

    1. The first graph red bars need to be sorted in descending order as given for the green bars above in graph 1
    2. The y-axis values of the graph 2 needs strictly be in the same order as they occurs in the graph 1
    3. Combine these graphs into one in such a way that the y-axis occurs only to the left and are not showed in the graph 2 to the right
    I tried this
    Code:
    graph combine left right
    , but it does not work.` The y-axis values occur in both graphs.

    I need suggestion to improve the command above and work out a combine graph.

    All three graphs are attached for clarity
    Attached Files
    Last edited by Ataullah Khan; 11 Dec 2018, 12:09.

  • #2
    Red and green are a poor graphical combination. Many people can't distinguish them readily.

    graph combine often does less than people hope. What it mainly does is place graphs next to each other.

    What you should do is give a data example sufficient to reproduce your graphs. Then people may be able to suggest better code or different code that works as well or better.

    Comment


    • #3
      Here is the data set I need to visualize.

      Code:
      * Example generated by -dataex-. To install:    ssc install dataex
      clear
      input str33 yaxis float(avg score1 score2 avg1    avg2)
      "HSS/High (B:92-T:97)"              87.66666    .  -.8666672    .    87.66666
      "Middle (B:91-T:96)"                86.66666    .  -.8666672    .    86.66666
      "SDEO Nowshera (B:93-T:96)"               90    .         -1    .    90
      "SDEO Pabbi (B:91-T:97)"            91.33334    .05555598          .    91.33334    .
      "SDEO Jehangira (B:87-T:95)"        88.66666    .208333          .    88.66666    .
      "Circle Akora khattak (B:89-T:96)"  89.66666    .09523773          .    89.66666    .
      "Circle Jallozai (B:90-T:96)"       89.33334    . -.11111069    .    89.33334
      "Circle Khair abad (B:84-T:94)"     87.66666    .3666664          .    87.66666    .
      "Circle Nowshera cantt (B:90-T:96)" 87.66666    .  -.3888893    .    87.66666
      "Circle Pabbi (B:92-T:97)"                93    .2          .    93    .
      "Circle Risalpur (B:92-T:96)"       91.33334    . -.16666603    .    91.33334
      end
      An example of a combined graph I need to generate is attached too.
      Click image for larger version

Name:	image005.png
Views:	1
Size:	46.7 KB
ID:	1474510

      Last edited by Ataullah Khan; 11 Dec 2018, 22:16.

      Comment


      • #4
        The design for the lefthand panel has bars starting (apparently) arbitrarily at about 80. That's precisely what most authorities on statistical graphics advise against. The base of a bar should have meaning. I don't follow those who insist that it should always be 0, as parity, freezing point at 32 deg F or normal body temperature are just some of many bases with meaning that are not zero.

        I would here use graph dot. To show measures in the same space, I just rescale linearly but ensure that the axis labels show what the reader needs to see. I did it roughly here. With a bit more work you could insist that the graphical range for each variable is the same.

        My point about colours stands.

        Code:
        * Example generated by -dataex-. To install:    ssc install dataex
        clear
        input str33 yaxis float(avg score1 score2 avg1    avg2)
        "HSS/High (B:92-T:97)"              87.66666    .  -.8666672    .    87.66666
        "Middle (B:91-T:96)"                86.66666    .  -.8666672    .    86.66666
        "SDEO Nowshera (B:93-T:96)"               90    .         -1    .    90
        "SDEO Pabbi (B:91-T:97)"            91.33334    .05555598          .    91.33334    .
        "SDEO Jehangira (B:87-T:95)"        88.66666    .208333          .    88.66666    .
        "Circle Akora khattak (B:89-T:96)"  89.66666    .09523773          .    89.66666    .
        "Circle Jallozai (B:90-T:96)"       89.33334    . -.11111069    .    89.33334
        "Circle Khair abad (B:84-T:94)"     87.66666    .3666664          .    87.66666    .
        "Circle Nowshera cantt (B:90-T:96)" 87.66666    .  -.3888893    .    87.66666
        "Circle Pabbi (B:92-T:97)"                93    .2          .    93    .
        "Circle Risalpur (B:92-T:96)"       91.33334    . -.16666603    .    91.33334
        end
        
        gen SCORE1 = 104 + 4 * score1 
        gen SCORE2 = 104 + 4 * score2
        set scheme s1color 
        
        local ytitle = "average" + (30 * " ") + "score" 
        graph dot (asis) avg1 avg2  SCORE1 SCORE2, ///
        marker(1, ms(Oh) mcolor(red)) marker(3, ms(Oh) mcolor(red)) ///
        marker(2, ms(+) mcolor(blue)) marker(4, ms(+) mcolor(blue)) ///
        over(yaxis) exclude0 yla(86(2)94 100 "-1"  102 "-0.5" 104 "0" 106 "0.5" 108 "1") legend(off) ///
        ytitle("`ytitle'") ysc(r(83 109)) yli(97, lc(black) lw(vthin)) linetype(line) lines(lc(gs12) lw(vthin))
        There are naturally other solutions and many ways to tinker with this idea.

        Click image for larger version

Name:	ataullah.png
Views:	1
Size:	45.8 KB
ID:	1474531


        More discussion that may help at https://www.stata-journal.com/sjpdf....iclenum=gr0034

        Comment


        • #5
          I think this is more systematic and works better.

          Code:
          * Example generated by -dataex-. To install:    ssc install dataex
          clear
          input str33 yaxis float(avg score1 score2 avg1    avg2)
          "HSS/High (B:92-T:97)"              87.66666    .  -.8666672    .    87.66666
          "Middle (B:91-T:96)"                86.66666    .  -.8666672    .    86.66666
          "SDEO Nowshera (B:93-T:96)"               90    .         -1    .    90
          "SDEO Pabbi (B:91-T:97)"            91.33334    .05555598          .    91.33334    .
          "SDEO Jehangira (B:87-T:95)"        88.66666    .208333          .    88.66666    .
          "Circle Akora khattak (B:89-T:96)"  89.66666    .09523773          .    89.66666    .
          "Circle Jallozai (B:90-T:96)"       89.33334    . -.11111069    .    89.33334
          "Circle Khair abad (B:84-T:94)"     87.66666    .3666664          .    87.66666    .
          "Circle Nowshera cantt (B:90-T:96)" 87.66666    .  -.3888893    .    87.66666
          "Circle Pabbi (B:92-T:97)"                93    .2          .    93    .
          "Circle Risalpur (B:92-T:96)"       91.33334    . -.16666603    .    91.33334
          end
          
          gen score = cond(score1 < ., score1, score2) 
          gen group = cond(score1 < ., 1, 2) 
          
          keep yaxis score avg group 
          gen y = _n 
          stack y yaxis avg group y yaxis score group, into(y yaxis whatever group) clear
          label define _stack 2 score 1 average 
          label val _stack _stack 
          
          separate whatever, by(group) 
          set scheme s1color 
          
          * install from Stata Journal 
          labmask y, values(yaxis) 
          
          set scheme s1color 
          scatter y whatever1, by(_stack, legend(off) note("") xrescale) ms(Oh) mc(red) ///
          yla(1/11, tlw(0) grid glc(gs12) glw(vthin) valuelabel ang(h)) ysc(r(0.5 11.5)) || ///
          scatter y whatever2, ms(+) mc(blue) subtitle(, fcolor(none))
          Click image for larger version

Name:	ataullah2.png
Views:	1
Size:	31.5 KB
ID:	1474556

          Comment


          • #6
            The previous post was careless about sort order but ysc(reverse) gets you the order of the example dataset.

            Comment

            Working...
            X