Announcement

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

  • how to add a grouping by variable to the scatter plot to create a panel New user to STATA pls help

    Hi all,
    Im very new user to STATA. I have the code below and am wanting to create a panel of scatter plots by a grouping variable (country).
    I cannot seem to find where to add this grouping into the code.
    Any help would be appreciated. thanks


    twoway (scatter recoverylatitude_c recoverylongitude if conditionaliveordead=="Alive", mcolor(pink) msymbol(square)) ///
    (scatter recoverylatitude_c recoverylongitude if conditionaliveordead=="Dead", mcolor(green) msymbol(D)) ///
    (scatter recoverylatitude_c recoverylongitude if conditionaliveordead=="Unknown", mcolor(red) msymbol(O)), ///
    title("Scatter plot of Recovery Latitude & Recovery Longitude, Finland", size(med)) ///
    ytitle("Recovery latitude (degrees)", size(med)) ///
    xtitle("Recovery longitude (degrees)", size(med)) ///
    legend(label(1 "Alive") label(2 "Dead") label(3 "Unknown") subtitle("Survival Status", size(med)) size(med)) ///

  • #2
    If you have more than a small number of countries, each of the panels will be too small to be readable.

    In any case, you cannot do this by just adding some grouping option to the command. You will need to write a loop over the countries and then combine the graphs after each country is graphed separately and stored in memory. I also assume that you want the title to mention the individual country's name, not all of them being Finland. So it would be like this:

    Code:
    levelsof country, local(countries)
    local graphs
    foreach c of local countries {
        local for_name = strtoname("`c'")
        twoway (scatter recoverylatitude_c recoverylongitude if conditionaliveordead=="Alive" ///
            & country == "`c'", mcolor(pink) msymbol(square)) ///
        (scatter recoverylatitude_c recoverylongitude if conditionaliveordead=="Dead" ///
            & country == "`c'", mcolor(green) msymbol(D)) ///
        (scatter recoverylatitude_c recoverylongitude if conditionaliveordead=="Unknown" ///
            & country == "`c'", mcolor(red) msymbol(O)), ///
        title("Scatter plot of Recovery Latitude & Recovery Longitude, `c' ", size(med)) ///
        ytitle("Recovery latitude (degrees)", size(med)) ///
        xtitle("Recovery longitude (degrees)", size(med)) ///
        legend(label(1 "Alive") label(2 "Dead") label(3 "Unknown") ///
        subtitle("Survival Status", size(med)) size(med)) name (`for_name', replace)
        local graphs `graphs' `for_name'
    }
    graph combine `graphs'
    Note: I assume that country is a string variable. If it is a (possibly value-labeled) numeric variable, then replace "`c'" by just `c' throughout and replace the -local for_name- command with -local for_name g`c'-.

    As no example data was provided, this code is not tested and may contain errors. In general, when asking for help with code it is best to also provide example data to test code on. The preferred way to do that is by using the -dataex- command.*

    Since you seem to prefer highly customizing the appearance of your graphs, you might want to read -help graph combine- to see options it provides for customizing that aspect of things as well.

    *If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      See also https://www.statalist.org/forums/for...lable-from-ssc


      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . sepscatter mpg weight, by(foreign) sep(rep78)
      
      . sepscatter mpg weight, by(foreign) sep(rep78) legend(row(1))
      In Marti Kaur's case I fear that this won't work well if the number of countries is large, but we don't know what it is.

      In principle , you can do this without a loop.

      Comment

      Working...
      X