Announcement

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

  • Kernel density graph and color of lines

    Dear Stata users,

    I have an apparently minor issue with the finalization of a Kernel density graph.

    I produced the graph with the following code:

    Code:
    kdensity sodtotale if (sodd_general ==0 )  , plot(kdensity sodtotale if(sodd_general == 2 ), lcolor (black) lpattern(dash) || ///
                                                    kdensity sodtotale if (sodd_general == 4 ), lcolor (grey) lpattern(dot)  ||  ///
                                                    kdensity sodtotale if (sodd_general ==6 ), lcolor (gs11) lpattern(dot) || ///
                                                    kdensity sodtotale if (sodd_general ==8 ) || ///
                                                    kdensity sodtotale if (sodd_general ==10 )) ///
                                                    title("Soddisfazione generale")  ///
                                                    legend(ring(0) pos(11) size(small) ///
                                                    label(1 "0") label(2 "2") label(3 "4") label(4 "6") label(5 "8") label(6 "10")) ///
                                                    ylabel (0.0(0.02)0.12) ///
                                                    name(gen, replace)
    I would like to plot against the var. "sodtotale" the lines associated wit different categories of "soddgeneral", and then to associate to each line one different tone of black-and-grey and style (so to make possible to distinguish the final six lines without using colors other than black and grey).

    Everything is fine, expect for the first line (associated with sodd_general == 0), where I am not able to add the -lcolor- and -lpattern- options (I guess Stata does not allow to use them with the command -kdensity-).

    Do you have any clue about that?

    Thanks for your support, best, G.

  • #2
    Absolutely allowed and perfectly possible.


    Code:
    sysuse auto, clear
    kdensity mpg if foreign, lc(red) plot(kdensity mpg if !foreign, lc(blue)) legend(order(2 "Domestic" 1 "Foreign") col(1) pos(1) ring(0))

    Comment


    • #3
      Dear Nick,

      thanks a lot, I did not know.

      Many thanks, best, G.

      Comment


      • #4
        Hi Nick

        I am facing difficulties estimating a kernel density plot. I am trying to differentiate the lines with colour and style.
        kdensity totalsleep, bwidth (1) lc(black) nograph generate (x fx)
        kdensity totalsleep if female==1, bwidth (3) lc(red) nograph generate(fx1) at(x)
        kdensity totalsleep if female==0, bwidth (3) lc(grey) nograph generate(fx0) at(x)
        label var fx1 "females"
        label var fx0 "males"
        line fx1 fx0 x, sort ytitle(Density)

        I get the error that says that the option lc() not allowed

        kind regards
        Shadrack

        Comment


        • #5
          My wild guess is that you're using an old version of Stata (and not telling us about it, contrary to https://www.statalist.org/forums/help#version)

          Try clc() not lc() if so.

          I am stumped if not.

          Naturally, your line command knows nothing of the colours you used earlier.

          Comment

          Working...
          X