Announcement

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

  • Sorting values in a scatter plot with confidence intervals

    Dear Stata users,

    I am trying to plot an average with 1SD confidence intervals by unit in a panel data. I want the values to be sorted in the scatter plot by mean_var_perc in descending order from left to write, but the sort is not helping and the values in the chart are still sorted by id values. Sorting the data by mean_var_perc did not help either. Additionally, I want the id variable labels to be used as x axes label for each tick (so instead of id values i get the unit name; id variable does have appropriate value labels). The code I am using is below, could you please help me solve these problems?

    Code:
    encode unit, generate(id)
    
    
    collapse (mean) mean_var_perc = var_perc (sd) sd_var_perc = var_perc, by(id)
    gen low=mean_var_perc - sd_var_perc
    gen hi=mean_var_perc + sd_var_perc
    
    
            twoway (rcap low hi id, sort(mean_var_perc)) ///
           (scatter mean_var_perc id, sort(mean_var_perc)), ///
           legend(off) ytitle("ytitle") xtitle("")
    Click image for larger version

Name:	Graph - Copy.png
Views:	1
Size:	89.1 KB
ID:	1743449
    Last edited by Michael Foreman; 15 Feb 2024, 17:30.

  • #2
    Something like this:
    Code:
    sysuse sp500,clear
    keep in 1/10
    gen id = "id_" + string(_n)
    gen diff = high - low
    sort diff
    gen x = _n
    forv i = 1/`=_N' {
        label define mylab `=x[`i']'  "`=id[`i']'", add
    }
    levelsof x, local(xvalues)
    label values x mylab
    list
    twoway rcap high low x,  xlabel(`xvalues', valuelabels)

    Comment

    Working...
    X