Announcement

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

  • Overlay a two-way graph and an hisogram from two distinct datasets

    Hello,

    I’m using Stata BE/17.0. I want to overlay a twoway graph from data A over an histogram from data B (or vice-versa). I want the final figure to have two y axis. My intuition is that I would need to have my info in only one dataset. Is it feasible with two separate datasets using a saved graph/histogram? Any suggestions?

    Data A (twoway graph):
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float duree double(estimate min95 max95)
     30 -.39709141041976465 -1.7826537251028372  .9884709042633077
     60 -.39709141041976465 -1.7826537251028372  .9884709042633077
     90 -.45052648862218997 -1.8140828455056424  .9130298682612623
    120  -.5234470219012863 -1.7725581317823662  .7256640879797935
    150  -.5234470219012863 -1.7725581317823662  .7256640879797935
    180  -.5646014010734139 -1.9337486345279202  .8045458323810923
    210   -.790618265194683 -2.1615262120481495  .5802896816587834
    240  -.7445110715021096 -1.9676471592732083 .47862501626898923
    270  -.9396592958036535 -2.1593530138289143 .28003442222160746
    end
    Code for the two-way graph, where y axis is an estimate of a program return in a school subject and x axis values are numbers of days of participation to the program:

    Code:
    twoway (scatter estimate duree,legend(region(col(white))) yscale(titlegap(2)) xscale(range(0 300) titlegap(2)) msymbol(square) msize(small)) (rcap min95 max95 duree, lcolor(blue)), xlabel(0(30)300, valuelabels angle(0) labsize(small))  xtitle("Number of days") ytitle("Grade variation (%)") ysca(titlegap(2)) ylab(, labs(small)) xsca(titlegap(2)) yline(0, lcolor(red)) graphregion(color(white))
    Dataset B (histogram):
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id jours)
      6 214
     35 181
     41 225
     51 207
     58 206
     63 184
     68 227
     77 150
     80 165
    102  64
    131 241
    136 261
    140 177
    162 192
    163  76
    166 236
    176 157
    185 106
    209  87
    213 269
    237  83
    266 239
    277  10
    359 191
    360 191
    373  97
    end
    Code for the histogram, where y axis is a proportion in percentage and x axis values are numbers of days of participation to the program:

    Code:
    histogram jours if jours>0 & jours<270, xscale(range(0 300)) percent fcolor(bluishgray) ytitle(Proportion (%)) title(Proportion of students in the program by days of participation) xlabel(0(30)300) bin (10) name(jours, replace)

    Thank you for your help,

    Nicolas Charette

  • #2
    Hello,

    I will try to reformulate. I have modified the data. I think what I wanted to do was impossible. TO THE STATALIST VETERANS: Should I create a new post?

    I created a twoway scatter diagram that shows the estimated coefficients of the impact of a stay-in-school program on the students grade, depending on the number of days they participated in the program. For each estimate, the minimum of days required to be included in the treated group for the regression goes up by 30 days, so as the number of days goes up, the number of treated students in the treated group (variable “treated”) goes down. The variable “proportion” shows the proportion of the initial 315 students that are considered treated for that number of days. I want to add the number of student included in the treated group for each estimate, but I’m having problems.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(days treated proportion) double(estimate min95 max95)
      30 314  .9968254 .046494984169676644 -1.9137120599203097  2.006702028259663
      60 312  .9904762 .046494984169676644 -1.9137120599203097  2.006702028259663
      90 307  .9746032   .7679641564922828 -1.2171593010667443   2.75308761405131
     120 304  .9650794   .7053769464142945 -1.3752948607208155 2.7860487535494043
     150 303  .9619048   .7053769464142945 -1.3752948607208155 2.7860487535494043
     180 299  .9492064   .7496901996016438  -1.094470087186972 2.5938504863902594
     210 288  .9142857   .8744209239132374   -.937688168573908 2.6865300164003827
     240 283  .8984127  1.0652281440416944  -.6800340685545461  2.810490356637935
     270 277  .8793651  1.2252341610868356  -.5849420620836219 3.0354103842572933
     300 269  .8539683   1.622480267793473 -.09256744557846797 3.3375279811654144
     330 266  .8444445  1.6464548726504031  -.0454302588594071  3.338340004160213
     360 262   .831746  1.7303964507057465 .031639609180645456  3.429153292230848
     390 252        .8  2.1070893356168483   .3949298994637567   3.81924877176994
     420 244  .7746032  2.7412028929879013   .8906725894595471  4.591733196516255
     450 238  .7555556  2.3624295839629177   .7458820651322966 3.9789771027935386
     480 233  .7396826  2.7401356084756063  1.2453542045037043  4.234917012447508
     510 229  .7269841  3.2365693377283065  1.6689275614528654  4.804211114003747
     540 225  .7142857  2.9633489603477297  1.4576143258043381  4.469083594891121
     570 222  .7047619  3.0624508868160554  1.5549986274808616  4.569903146151249
     600 216  .6857143  2.9580537025950657   1.500419624515906  4.415687780674226
     630 214  .6793651   2.802549173921975   1.362511785432553  4.242586562411397
     660 205  .6507937  2.8552935302961844  1.6429667871494913  4.067620273442877
     690 199  .6317461    2.60091867213188  1.4055934379393151  3.796243906324445
     720 196  .6222222   2.862556692980679  1.7134561672942563  4.011657218667102
     750 190  .6031746   3.020192408941428  1.7886077570320789 4.2517770608507774
     780 180  .5714286   3.270420375869851  2.1572063649020325  4.383634386837669
     810 179   .568254  3.2798848434042385  2.1580191162282016  4.401750570580275
     840 178  .5650794   3.334238577065541   2.190461802392833  4.478015351738249
     870 174   .552381  3.2029810659708073  1.9022880866712197  4.503674045270395
     900 171 .54285717  3.2249530746133077  1.7909727176552197  4.658933431571396
     930 169 .53650796  2.9694181687471892  1.4199996720686934  4.518836665425685
     960 165 .52380955  2.9693774549328418  1.4562584482583165  4.482496461607367
     990 160 .50793654  3.1921786652969804  1.5952422399213306   4.78911509067263
    1020 159 .50476193  3.1921786652969804  1.5952422399213306   4.78911509067263
    1050 153  .4857143  3.0669962431122233  1.3994037984757741  4.734588687748673
    1080 145  .4603175  3.3090450937010547  1.6462209310607643  4.971869256341345
    1110 142 .45079365   3.728517834601178   2.228913350223377  5.228122318978979
    1140 140 .44444445  3.7170518592235777  2.3227947841369128  5.111308934310243
    1170 137  .4349206  3.5958287913156295  1.9852390918627096   5.20641849076855
    1200 136   .431746  3.6005460550636914  1.9264560953962446  5.274636014731138
    1230 136   .431746  3.6005460550636914  1.9264560953962446  5.274636014731138
    1260 134  .4253968   3.720102331499618  1.9221698018836166   5.51803486111562
    1290 132  .4190476   3.948177458153095  2.0506693790139403  5.845685537292249
    1320 131   .415873  3.6746230141674796   1.668615296676586  5.680630731658374
    1350 129  .4095238  3.6848180255306016   2.035406144410557  5.334229906650647
    1380 117  .3714286  3.9744511535877236    2.51210689875867  5.436795408416778
    1410 110  .3492064  3.9437167422282013   2.473638267386958 5.4137952170694446
    1440 100  .3174603  4.5122724855936855   2.935768996508071    6.0887759746793
    1470  96  .3047619   4.679545651356969   2.915008202769464  6.444083099944475
    1500  96  .3047619   4.679545651356969   2.915008202769464  6.444083099944475
    1530  93  .2952381   4.791127198504803  2.9017299378344097  6.680524459175197
    1560  90  .2857143   4.535125545765046    2.68041710129408  6.389833990236013
    1590  87  .2761905    4.56434819372004  2.7865120368652256  6.342184350574854
    1620  85 .26984128   4.711305948546441   3.112012418437499  6.310599478655383
    1650  83 .26349208   4.696330087243326  3.1017411347269785 6.2909190397596735
    1680  82 .26031747  4.5760002676350275  2.9981085791176625  6.153891956152393
    1710  80 .25396827   4.298243982053482  2.6962880990034757  5.900199865103489
    1740  73 .23174603   4.386591953033458  2.5268607287775184  6.246323177289397
    end
    Code:
    label variable estimate "Estimated coefficient"
    label variable min95 " Confidence interval "
    label variable max95 " 95% "
    twoway (scatter estimate days,legend(region(col(white))) yscale(titlegap(2)) xscale(range(0 1800) titlegap(2)) msymbol(square) msize(small)) (rcap min95 max95 days, lcolor(blue)), xlabel(0(250)1750, valuelabels angle(0) labsize(small)) xtitle("Number of days") ytitle("Grade variation (%)") ysca(titlegap(2)) ylab(, labs(small)) xsca(titlegap(2)) yline(0, lcolor(red)) graphregion(color(white)) ylabel(-2 (2) 8)

    When I try to add the number of treated as a label, using mlabel(treated), it is sloppy:

    Code:
    label variable estimate "Estimated coefficient"
    label variable min95 " Confidence interval "
    label variable max95 " 95% "
    twoway (scatter estimate days, mlabel(treated) legend(region(col(white))) yscale(titlegap(2)) xscale(range(0 1800) titlegap(2)) msymbol(square) msize(small)) (rcap min95 max95 days, lcolor(blue)), xlabel(0(250)1750, valuelabels angle(0) labsize(small)) xtitle("Number of days") ytitle("Grade variation (%)") ysca(titlegap(2)) ylab(, labs(small)) xsca(titlegap(2)) yline(0, lcolor(red)) graphregion(color(white)) ylabel(-2 (2) 8)
    Any suggestions making this more aesthetic? I wanted to overlay a density plot using variable “proportion”, but I always fail. Could someone help me, please?







    Comment

    Working...
    X