Announcement

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

  • Legend explanation does not show the same colors as in graph

    Hi!

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

    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	36.4 KB
ID:	1746329


    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))
    }

  • #2
    Your graph is made up of 4 plots : 2 -rcap- for the confidence intervals (which are plots 1 & 2) and 2 -line-. Try:
    Code:
    legend(order(3 "Medi ... "  4 "EAT ..." )

    Comment


    • #3
      Thank you!!

      I updated the code with 2 and 3, which made it work. But is there a way to simplify this code?

      /// Loop through your variables for regression and graphs.
      foreach y in Meanthickness72 GCS1 AD_MRIsignature_Jack_adjusted Hippadj72 gap_ensemble {

      // First regression with 'Eat-Lancet diet'
      regress `y' E i.sex2 Rök_dic_2 Physact2 BMI3 EDU3
      local betahat1 = `:di %5.3f _b[E]'
      matrix T = r(table) // Store regression table in matrix T
      local p_value1 = `:di %05.3f T[4,1]' // 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 'Mediterranean diet'
      regress `y' M i.sex2 Rök_dic_2 Physact2 BMI3 EDU3
      local betahat2 = `:di %5.3f _b[M]'
      matrix T = r(table) // Store regression table in matrix T
      local p_value2 = `:di %05.3f T[4,1]' // 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( ///
      2 "EAT-Lancet (B{sub:1}=`betahat1', p=`p_value1')" ///
      3 "Mediterranean (B{sub:2}=`betahat2', p=`p_value2')" ///
      ) pos(6) col(2)) recast(line)
      graph save `y'.gph, replace
      graph export `y'.jpg, quality (100) replace
      }

      Comment


      • #4
        I recommend statements like

        Code:
        local betahat1 : di %5.3f _b[E]
        rather than

        Code:
        local betahat1 = `:di %5.3f _b[E]'
        That's only a saving of 3 characters, but three characters you can get wrong too.

        Comment

        Working...
        X