Announcement

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

  • Panel Data Scatter Plot Not Displaying All Data

    I'm trying to create a nice scatter plot with some panel data that's coloured, the problem is that it isn't displaying all of the data for some reason.

    The code I used is
    scatter X? Y, mcolor(blue orange black pink red lime purple maroon teal olive_teal cyan magenta
    They're identified by a variable called regionNum of which there are 12. It seems to happily display 9 of them, but misses off 3.

    Example : https://i.imgur.com/EV40KWj.png

    Doing a regular scatter
    scatter Y X
    Returns a scatter of all the data : https://i.imgur.com/Mb8duGe.png

  • #2
    The ? wildcard matches one, and only one, character. The * character is needed for your problem.

    Comment


    • #3
      That worked perfectly thank you very much.

      Sorry if I could just take up a bit more of your time, is there a way to add lfits to each piece of data? (I'm aware this might look rather ridiculous).

      I tried

      HTML Code:
      graph twoway lfit realgvaperh* lifesatisfaction

      But there are too many variables, unfortunately.

      Comment


      • #4
        You need to spell out each variable separately — although the code for that could be the result of a loop.

        Comment


        • #5
          regionNum is how each bit of the panel data is split up, there 7 dates in the time series bit of the panel data. I've read your "Making foreach go through all values of a variable", and I'm quite confused.

          So I use foreach or forval to iterate through my panel variable (regionNum), and I create a new variable that will hold these best fits? Not sure how I'd actually write the loop out, and then what I'd put in the loop to generate lines of best fit for each section of data. Sorry if I am asking a lot.

          Comment


          • #6
            If you have 12 response or outcome variables it is going to be hard to avoid a mess. But in principle this is what you could try.

            Code:
            local colours blue orange black pink red lime purple maroon teal olive_teal cyan magenta
            tokenize "`colours'" 
            
            local call 
            forval j = 1/12 { 
                  local call `call' || lfit X`j' Y, lcolor(``j'') 
            } 
            
            * see what you just did 
            macro list 
            
            scatter X* Y, mcolor(`colours')  `call'
            which is best executed as a block from a do-file editor window.

            In practice, I guess I would prefer something like

            Code:
            scatter X Y || lfit X Y, by(regionNum)

            Comment


            • #7
              Cross-posted at https://www.reddit.com/r/stata/comme...ying_all_data/ Please note our policy on cross-posting, which we make explicit at https://www.statalist.org/forums/help#crossposting -- it is that you should tell us about it.

              Not giving credit to Statalist for the answer is a little disappointing. (And I mean Statalist; I don't care about personal credit, as you would have got the same answer from many people.)

              Comment


              • #8
                That worked perfectly thank you so much, the default result without moving the legend is rather hilarious

                With legend : https://i.imgur.com/qfADE4o.png

                It actually looks kind of okay without the legend forcing it into a tiny space. I also changed the code ever so slightly so the colours all lined up and I think it works fairly well.

                For anyone in the future who's curious about matching up colours

                Code:
                local colours blue orange black pink red lime purple maroon teal olive_teal cyan magenta
                tokenize "`colours'"
                
                local call
                forval j = 1/12 {
                      local call `call' || lfit X`j' Y, lcolor(``j'')
                      local call `call' || scatter X`j' Y, mcolor(``j'')
                }
                
                * see what you just did
                macro list
                
                scatter X* Y, mcolor(`colours') legend(off)  `call'
                Without legend : https://i.imgur.com/v16TQZv.png

                Thanks again for all the advice, really happy with the result. Have a great night!

                Comment


                • #9
                  Things that sometimes work well.

                  1. One or two character abbreviations as marker labels. https://www.stata-journal.com/sjpdf....iclenum=gr0023

                  2. Text labels. i.e. explanatory text next to each cluster on the graph, not a legend.

                  3. Front and back plots. https://www.statalist.org/forums/for...e-on-ssc/page2

                  Comment

                  Working...
                  X