Dear Stata list,
I am trying to produce an area plot that shades positive values as red and negative values as green for a concentration index I have constructed (flip in colours to convention to do with inequality - the values shown are the gap to line of equality in a concentration curve)
I have gotten 95% of the way there (attached below) - but would like to join the lines to create a smooth transition from neg-pos / pos-neg, rather than have overlap around changes from negative to positive/ positive to negative (i.e., between 0.3-0.5, the line does not run onto each other)
I have thought through this, and the first solution that comes to mind is creating points between every existing point, and coding those points to zero if the coordinates before and after are of differing signs - but I have struggled to implement this.
I have also tried the dropline plot, which gives a better overall look - but would like to have the area shaded.
(I have tried this too, but do not get the clean transitions I am looking for - https://www.statalist.org/forums/for...-cyclic-series)
If you have a solution / could help in coding the transitions to ensure they line up with each other, I would be most grateful!
I have tried separating the series into a positive and negative series, which is why there is now overlap, having coded missing values to zero. However if there is a simpler way to colour positive area (area above a value) differently to negative values (below a value) that would also be truly helpful.
Code and data is below.
Thanks in advance!
Zac

Data example:
Code for graph (with some formatting/looping variables omitted)
Edit: Apologies if I have missed any conventions - but have tried as much as possible to stick to them.
I am trying to produce an area plot that shades positive values as red and negative values as green for a concentration index I have constructed (flip in colours to convention to do with inequality - the values shown are the gap to line of equality in a concentration curve)
I have gotten 95% of the way there (attached below) - but would like to join the lines to create a smooth transition from neg-pos / pos-neg, rather than have overlap around changes from negative to positive/ positive to negative (i.e., between 0.3-0.5, the line does not run onto each other)
I have thought through this, and the first solution that comes to mind is creating points between every existing point, and coding those points to zero if the coordinates before and after are of differing signs - but I have struggled to implement this.
I have also tried the dropline plot, which gives a better overall look - but would like to have the area shaded.
(I have tried this too, but do not get the clean transitions I am looking for - https://www.statalist.org/forums/for...-cyclic-series)
If you have a solution / could help in coding the transitions to ensure they line up with each other, I would be most grateful!
I have tried separating the series into a positive and negative series, which is why there is now overlap, having coded missing values to zero. However if there is a simpler way to colour positive area (area above a value) differently to negative values (below a value) that would also be truly helpful.
Code and data is below.
Thanks in advance!
Zac
Data example:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(DR_CONTACTS pos_DR_CONTACTS neg_DR_CONTACTS) str3 strCOUNTRY float PTILE 0 0 0 "FRA" 0 .004836503 .004836503 0 "FRA" .05 .00766629 .00766629 0 "FRA" .1 .004911914 .004911914 0 "FRA" .15 .00768739 .00768739 0 "FRA" .2 .010643587 .010643587 0 "FRA" .25 .008195519 .008195519 0 "FRA" .3 -.0014103353 0 -.0014103353 "FRA" .35 -.005840927 0 -.005840927 "FRA" .4 -.0035054684 0 -.0035054684 "FRA" .45 .005300134 .005300134 0 "FRA" .5 .0016778708 .0016778708 0 "FRA" .55 .001959741 .001959741 0 "FRA" .6 .005188048 .005188048 0 "FRA" .65 .0039619207 .0039619207 0 "FRA" .7 .003540456 .003540456 0 "FRA" .75 .003118992 .003118992 0 "FRA" .8 .002697468 .002697468 0 "FRA" .85 .0022759438 .0022759438 0 "FRA" .9 .006375968 .006375968 0 "FRA" .95 0 0 0 "FRA" 1 end
Code for graph (with some formatting/looping variables omitted)
Code:
graph twoway area neg_DR_CONTACTS PTILE if strCOUNTRY == "FRA", yla(-0.04(0.01)0.06) yline(0, lcolor(navy)) col(dkgreen) || area pos_DR_CONTACTS PTILE if strCOUNTRY == "FRA", color(maroon) legend(label(1 "Pro-poor") label(2 "Pro-rich")) ytitle("DR_CONTACTS_equality_gap") xtitle("Cumulative population")
Comment