plotchartslinestata

Connected line plots depicting average of groups, split by category


I have data where there are many observations across two variables, say rep78 and headroom.

I want to take the mean of a third variable, say weight within each rep78+headroom cell. For each headroom category, I separately want to plot (connected dot plot) these mean values with rep78 on the x-axis. I suppose a more intuitive way to think of this is to replace rep78 with year and headroom with state.

I have been able to get what I want with the code below:

sysuse auto2, clear
bys rep78 headroom: egen mean_w=mean(weight)
twoway  (scatter mean_w rep78 if headroom==1.5, connect(l)) ///
        (scatter mean_w rep78 if headroom==2.0, connect(l)) ///
        (scatter mean_w rep78 if headroom==2.5, connect(l)) ///
        (scatter mean_w rep78 if headroom==3.0, connect(l)) ///
        (scatter mean_w rep78 if headroom==3.5, connect(l)) ///
        (scatter mean_w rep78 if headroom==4.0, connect(l)) ///
        (scatter mean_w rep78 if headroom==4.5, connect(l)) ///
        (scatter mean_w rep78 if headroom==5.0, connect(l)), ///
        legend(label(1 "1.5") label(2 "2.0") label(3 "2.5") ///
              label(4 "3.0") label(5 "3.5") label(6 "4.0") ///
              label(7 "4.5") label(8 "5.0"))

However, is there an easier (i.e. shorter) way to do this?

I am fine with simplifying any and all parts of this code.


Solution

  • Thanks for the MCVE!

    You are correct. Here is one shorter solution to get the same graph, modulo small details you can tune too:

    sepscatter mean_w rep78, sep(headroom) recast(connected) name(G2, replace) 
    

    using sepscatter from SSC, as announced on Statalist. Further examples can be found using sepscatter as a search term on Statalist.