Announcement

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

  • getting scatter and lfit to assign the same color for an observation

    Hi All,

    I am using twoway to generate a scatterplot with an lfit overlay for several individual units, and would like to have the color of the lfit line match the color of the markers for each unit.

    In the following example, I have only two units (domestic and foreign), so I could have done it manually (i.e. with mcolor() and lcolor()) . As this is intended for many units, and many different colors will be used, this will have to be performed via code.

    Thanks in advance!

    Ariel

    Code:
    sysuse auto, clear
    tw(scatter weight mpg if foreign ==1)(lfit weight mpg if foreign==1)(scatter weight mpg if foreign ==0)(lfit weight mpg if foreign==0)

  • #2
    Here is one solution:

    Code:
    sysuse auto,clear
    levelsof foreign,local(ids)
    local colors black gray
    
    forval x = 1/`:word count `ids''{
        local graph `graph'scatter weight mpg if foreign==`:word `x' of `ids'',mcolor(`:word `x' of `colors'') || lfit weight mpg if foreign==`:word `x' of `ids'',lcolor(`:word `x' of `colors'') ||
        local legend `legend'`=`x'*2-1' "`:word `x' of `ids''" 
    }
    twoway `graph',legend(order(`legend') rows(1))
    All you need to do is change "foreign" to the name of your category variable, and add to the list of colors such that the amount of colors equals the amount of categories.

    Comment


    • #3
      Thank you, Ali!

      There could be up to a hundred units, so I'd rather avoid using a local to specify the colors (since I'd have to include every color available) and let Stata choose the colors for the markers and just have the lfit match that color.... Perhaps that's not possible?

      Thanks, again!

      Ariel

      Comment


      • #4
        One way is to use random colors, with two caveats - first, there is a very small chance of repeated colors, and a higher chance of very similar colors, being used for different categories; second, I can't imagine a graph with >100 lines and >(100*N) markers providing any sort of useful information to the viewer, given that the example below looks like spaghetti with only 10 observations per category.

        In any case, here's the code:

        Code:
        clear
        set obs 100
        gen id = _n
        expand 10
        gen x = runiform()
        gen y=runiform()
        //start here
        levelsof id,local(ids)
        forval x = 1/`:word count `ids''{
            local color  `=runiformint(0,255)' `=runiformint(0,255)' `=runiformint(0,255)'
            local graph `graph'scatter x y if id==`:word `x' of `ids'', mcolor("`color'")|| lfit x y if id==`:word `x' of `ids'', lcolor("`color'") ||
            local legend `legend'`=`x'*2-1' "`:word `x' of `ids''" 
        }
        twoway `graph',legend(order(`legend') rows(1))
        An alternative you might consider is something like Nick Cox's subsetplot/fabplot. If such an approach is appealing, here is some sample code which replicates the effect of the programs mentioned but allows the use of two twoway plottyples (lfit and scatter)

        Code:
        clear *
        set obs 15
        gen id = _n
        expand 10
        gen x = runiform()
        gen y=runiform()
        //graph
        levelsof id,local(ids)
        foreach id of local ids{
            levelsof id if id!=`id',local(moreids)
            local graph ""
            foreach i of local moreids{
                local graph `graph' scatter y x if id ==`i',mcolor(gs13) ||
            }
            local line :word `id' of `xline'
            twoway  `graph' scatter y x if id == `id',mcolor(orange) || lfit y x if id == `id', lcolor(orange) name(graph`id') title(`id') legend(off) 
            local comb `comb' graph`id'
        }
        graph combine `comb',commonscheme ycommon xcommon name(graphcomb)
        Producing:

        Click image for larger version

Name:	graphcomb.png
Views:	1
Size:	202.6 KB
ID:	1617745

        Comment


        • #5
          Thank you again, Ali!

          These two approaches have given me something to play with!

          Ariel

          Comment

          Working...
          X