Hi!
Why do the legend explanations not have the same colors as in the graph?

I am using this code:
/// Loop through your variables
foreach y in Meanthickness72 {
// First regression with 'E'
regress `y' E i.sex2
local betahat1 = `:di %5.3f _b[E]'
matrix T = r(table) // Store regression table in matrix T
local p_value1 = `:di %05.3f T[1,3]' // Extract p-value for 'E' from the matrix
// Store the results for later use with coefplot
quietly eststo E: margins, at(E=(0(1)10)) post
// regression with 'M'
regress `y' M i.sex2
local betahat2 = `:di %5.3f _b[M]'
matrix T = r(table) // Store regression table in matrix T
local p_value2 = `:di %05.3f T[1,3]' // Extract p-value for 'M' from the matrix
// Store the results for later use with coefplot
quietly eststo M: margins, at(M=(0(1)10)) post
// Coefplot command with updated legend
coefplot E M, ///
at ///
title("`: var label `y''") ///
ytitle("`: var label `y''") ///
xtitle(Score) ///
legend(order( ///
1 "EAT-Lancet (B{sub:1}=`betahat1', p=`p_value1')" ///
2 "Mediterranean (B{sub:2}=`betahat2', p=`p_value2')" ///
) pos(6) col(2)) ///
recast(line)
}
coefplot (M, recast(connected) lc(red)) (E, recast(connected) lc(blue)), drop(_cons)
foreach y in Meanthickness72 {
// First regression with 'M'
regress `y' M i.sex2
local betahat1 = `:di %5.3f _b[M]'
matrix T = r(table) // Store regression table in matrix T
local p_value1 = `:di %05.3f T[1,3]' // Extract p-value for 'M' from the matrix
// Store the results for later use with coefplot
quietly eststo M: margins, at(M=(0(1)10)) post
// Second regression with 'E'
regress `y' E i.sex2
local betahat2 = `:di %5.3f _b[E]'
matrix T = r(table) // Store regression table in matrix T
local p_value2 = `:di %05.3f T[1,3]' // Extract p-value for 'E' from the matrix
// Store the results for later use with coefplot
quietly eststo E: margins, at(E=(0(1)10)) post
coefplot M E, ///
at ///
title("`: var label `y''") ///
ytitle("`: var label `y''") ///
xtitle(Score) ///
recast(line) ///
mstyle(M, color(blue) lpattern(solid)) /// Specify color and line pattern for M
mstyle(E, color(red) lpattern(dash)) /// Specify color and line pattern for E
legend(order( ///
1 "Mediterranean (B{sub:1}=`betahat1', p=`p_value1')" ///
2 "EAT-Lancet (B{sub:2}=`betahat2', p=`p_value2')" ///
) pos(6) col(2))
}
Why do the legend explanations not have the same colors as in the graph?
I am using this code:
/// Loop through your variables
foreach y in Meanthickness72 {
// First regression with 'E'
regress `y' E i.sex2
local betahat1 = `:di %5.3f _b[E]'
matrix T = r(table) // Store regression table in matrix T
local p_value1 = `:di %05.3f T[1,3]' // Extract p-value for 'E' from the matrix
// Store the results for later use with coefplot
quietly eststo E: margins, at(E=(0(1)10)) post
// regression with 'M'
regress `y' M i.sex2
local betahat2 = `:di %5.3f _b[M]'
matrix T = r(table) // Store regression table in matrix T
local p_value2 = `:di %05.3f T[1,3]' // Extract p-value for 'M' from the matrix
// Store the results for later use with coefplot
quietly eststo M: margins, at(M=(0(1)10)) post
// Coefplot command with updated legend
coefplot E M, ///
at ///
title("`: var label `y''") ///
ytitle("`: var label `y''") ///
xtitle(Score) ///
legend(order( ///
1 "EAT-Lancet (B{sub:1}=`betahat1', p=`p_value1')" ///
2 "Mediterranean (B{sub:2}=`betahat2', p=`p_value2')" ///
) pos(6) col(2)) ///
recast(line)
}
coefplot (M, recast(connected) lc(red)) (E, recast(connected) lc(blue)), drop(_cons)
foreach y in Meanthickness72 {
// First regression with 'M'
regress `y' M i.sex2
local betahat1 = `:di %5.3f _b[M]'
matrix T = r(table) // Store regression table in matrix T
local p_value1 = `:di %05.3f T[1,3]' // Extract p-value for 'M' from the matrix
// Store the results for later use with coefplot
quietly eststo M: margins, at(M=(0(1)10)) post
// Second regression with 'E'
regress `y' E i.sex2
local betahat2 = `:di %5.3f _b[E]'
matrix T = r(table) // Store regression table in matrix T
local p_value2 = `:di %05.3f T[1,3]' // Extract p-value for 'E' from the matrix
// Store the results for later use with coefplot
quietly eststo E: margins, at(E=(0(1)10)) post
coefplot M E, ///
at ///
title("`: var label `y''") ///
ytitle("`: var label `y''") ///
xtitle(Score) ///
recast(line) ///
mstyle(M, color(blue) lpattern(solid)) /// Specify color and line pattern for M
mstyle(E, color(red) lpattern(dash)) /// Specify color and line pattern for E
legend(order( ///
1 "Mediterranean (B{sub:1}=`betahat1', p=`p_value1')" ///
2 "EAT-Lancet (B{sub:2}=`betahat2', p=`p_value2')" ///
) pos(6) col(2))
}
Comment