Announcement

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

  • Adding descriptive column in twoway scatter plot

    Dear Stata Gurus,
    I created a graph as attached, with additional statistics/info in the graph by manipulating the mlab functionality in Stata. Below is the code I produced to create this graph (graph I have). As you would see, I have two additional statistics (n, and pct) added to the graph on the left, and right end of the spike, respectively. I want these two additional statistics to be added as two columns as shown in the "graph I want". Any advice would be highly appreciated.

    Code I used:
    Code:
    graph twoway (scatter y x, mc(gs0)) (rspike xlow xup y, lc(gs0) hori), sub(, size(small)) by(j, cols(2) legend(off) t1("Graph I have") note (" ")) || /*
    */ (scatter y xup, m(oh) msize(vtiny) mc(white) mlab(pct) mlabs(small) mlabp(3) mlabc(blue)) || /*
    */ (scatter y xlow, m(oh) msize(vtiny) mc(white) mlab(n) mlabs(small) mlabp(9) mlabc(red))
    Dummy data:
    y j x xlow xup n pct
    1 A vs B 1.96 1.62 2.3 10 60.1
    1 A vs C 1.53 0.95 2.11 6 11.1
    2 A vs B 1.68 1.17 2.19 9 75.5
    2 A vs C 0.21 -0.33 0.75 6 10.7
    3 A vs B 1.98 1.52 2.44 6 61.8
    3 A vs C











    Graphs:
    Graph I have:
    Click image for larger version

Name:	graph_i_have.png
Views:	1
Size:	15.4 KB
ID:	1486708

    Graph I want:
    Click image for larger version

Name:	graph_i_want.JPG
Views:	1
Size:	33.8 KB
ID:	1486709
    Attached Files

  • #2
    It is possible to create invisible markers. Your position on the y-axis does not change, so just move rightwards along the x-axis.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte y str6 j float(x xlow xup) byte n float pct
    1 "A vs B" 1.96 1.62  2.3 10 60.1
    1 "A vs C" 1.53  .95 2.11  6 11.1
    2 "A vs B" 1.68 1.17 2.19  9 75.5
    2 "A vs C"  .21 -.33  .75  6 10.7
    3 "A vs B" 1.98 1.52 2.44  6 61.8
    3 "A vs C"    .    .    .  .    .
    end
    
    qui sum x
    gen x2= `r(max)' + 0.5
    gen x3= `r(max)' + 0.75
    
    graph twoway (scatter y x, mc(gs0)) (rspike xlow xup y, lc(gs0) hori), sub(, size(small)) /*
    by(j, cols(2) legend(off) t1("Graph I have") note (" ")) || (scatter y xup, m(oh) msize(vtiny) mc(white) ) || /*
    (scatter y x2, m(none) mlab(n) mlabcolor(red)) || (scatter y x3, m(none) mlab(pct) mlabcolor(red)) || /*
    (scatter y xlow, m(oh) msize(vtiny) mc(white) scheme(s1color) text(3.2 2.6 "n", color(red)) /*
    text(3.2 2.95 "pct.", color(red)) ylab(1 (0.5) 3.2))

    Result:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	44.0 KB
ID:	1486743








    Last edited by Andrew Musau; 05 Mar 2019, 12:19.

    Comment


    • #3
      Wow thank you so much. This worked like a champ!

      Comment

      Working...
      X