Announcement

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

  • Dot plot help

    Hello, Can anyone advise on how to code a dotplot to produce similar results to the graph shown below. I have panel data with similar variables to that detailed in the graph but cannot understand how to replicate using my own data in stata using statas command for dotplots.
    Click image for larger version

Name:	1-s2.0-S0304387822001249-gr1_lrg.jpg
Views:	2
Size:	788.7 KB
ID:	1710807
    Attached Files

  • #2
    Code:
    help graph dot
    We cannot really do any better without a data example. See FAQ Advice #12 for details.

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      Code:
      help graph dot
      We cannot really do any better without a data example. See FAQ Advice #12 for details.

      Sorry for being too vague. I have a panel dataset in which I'm hoping to calculate the effect of earthquakes on political score in developing countries. I would like to insert a dot plot similar to that as shown above that can help visualise the # of earthquakes per year across each Country (or a smaller sample as i have 118 countries). The size of the dot indicates the number of earthquakes a country experienced in a given year. More earthquakes = Bigger dot.

      I have tried following the dot plot help guide, but I'm just guessing and don't really understand it
      Code:
      graph dot NoOfQuakes ascategory(Year), over(Cntry)
      I understand this is probably nonsense and is a long way off what I'm after.

      Essentially, I would like a dot matrix with my countries on the Y axis, years on the X axis, and each dot representing an earthquake in a given country in a given year, likewise More earthquakes=Bigger dot

      A sample of my data is:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str24 Country int Year byte NoOfQuakes
      "Albania" 1960 .
      "Albania" 1961 .
      "Albania" 1962 .
      "Albania" 1963 .
      "Albania" 1964 .
      "Albania" 1965 .
      "Albania" 1966 .
      "Albania" 1967 .
      "Albania" 1968 .
      "Albania" 1969 .
      "Albania" 1970 .
      "Albania" 1971 .
      "Albania" 1972 .
      "Albania" 1973 .
      "Albania" 1974 .
      "Albania" 1975 .
      "Albania" 1976 .
      "Albania" 1977 .
      "Albania" 1978 .
      "Albania" 1979 .
      "Albania" 1980 .
      "Albania" 1981 .
      "Albania" 1982 .
      "Albania" 1983 .
      "Albania" 1984 .
      "Albania" 1985 .
      "Albania" 1986 .
      "Albania" 1987 .
      "Albania" 1988 .
      "Albania" 1989 .
      "Albania" 1990 .
      "Albania" 1991 .
      "Albania" 1992 .
      "Albania" 1993 .
      "Albania" 1994 .
      "Albania" 1995 .
      "Albania" 1996 .
      "Albania" 1997 .
      "Albania" 1998 .
      "Albania" 1999 .
      "Albania" 2000 .
      "Albania" 2001 .
      "Albania" 2002 .
      "Albania" 2003 .
      "Albania" 2004 .
      "Albania" 2005 .
      "Albania" 2006 .
      "Albania" 2007 .
      "Albania" 2008 .
      "Albania" 2009 .
      "Albania" 2010 .
      "Albania" 2011 .
      "Albania" 2012 .
      "Albania" 2013 .
      "Albania" 2014 .
      "Albania" 2015 .
      "Albania" 2016 .
      "Albania" 2017 .
      "Albania" 2018 .
      "Algeria" 1960 1
      "Algeria" 1961 .
      "Algeria" 1962 .
      "Algeria" 1963 .
      "Algeria" 1964 .
      "Algeria" 1965 1
      "Algeria" 1966 .
      "Algeria" 1967 1
      "Algeria" 1968 .
      "Algeria" 1969 .
      "Algeria" 1970 .
      "Algeria" 1971 .
      "Algeria" 1972 .
      "Algeria" 1973 .
      "Algeria" 1974 .
      "Algeria" 1975 .
      "Algeria" 1976 .
      "Algeria" 1977 .
      "Algeria" 1978 .
      "Algeria" 1979 .
      "Algeria" 1980 1
      "Algeria" 1981 .
      "Algeria" 1982 .
      "Algeria" 1983 .
      "Algeria" 1984 .
      "Algeria" 1985 1
      "Algeria" 1986 .
      "Algeria" 1987 1
      "Algeria" 1988 1
      "Algeria" 1989 1
      "Algeria" 1990 .
      "Algeria" 1991 .
      "Algeria" 1992 1
      "Algeria" 1993 .
      "Algeria" 1994 1
      "Algeria" 1995 .
      "Algeria" 1996 .
      "Algeria" 1997 .
      "Algeria" 1998 .
      "Algeria" 1999 1
      "Algeria" 2000 .
      "Algeria" 2001 .
      "Algeria" 2002 .
      "Algeria" 2003 2
      "Algeria" 2004 2
      "Algeria" 2005 .
      "Algeria" 2006 1
      "Algeria" 2007 .
      "Algeria" 2008 .
      "Algeria" 2009 .
      "Algeria" 2010 .
      "Algeria" 2011 .
      "Algeria" 2012 .
      "Algeria" 2013 .
      "Algeria" 2014 1
      "Algeria" 2015 .
      "Algeria" 2016 .
      "Algeria" 2017 .
      "Algeria" 2018 .
      "Angola"  1960 .
      "Angola"  1961 .
      "Angola"  1962 .
      "Angola"  1963 .
      "Angola"  1964 .
      "Angola"  1965 .
      "Angola"  1966 .
      end
      Last edited by Jack Bowley; 21 Apr 2023, 12:17.

      Comment


      • #4
        You can use twoway scatter. Probably you would want to sort the axis in terms of frequency of earthquakes. Below, I use labmask from the Stata Journal to do this. Change the highlighted numbers to correspond to the number of countries. In this case, the data example has only three.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str24 Country int Year byte NoOfQuakes
        "Albania" 1960 .
        "Albania" 1961 .
        "Albania" 1962 .
        "Albania" 1963 .
        "Albania" 1964 .
        "Albania" 1965 .
        "Albania" 1966 .
        "Albania" 1967 .
        "Albania" 1968 .
        "Albania" 1969 .
        "Albania" 1970 .
        "Albania" 1971 .
        "Albania" 1972 .
        "Albania" 1973 .
        "Albania" 1974 .
        "Albania" 1975 .
        "Albania" 1976 .
        "Albania" 1977 .
        "Albania" 1978 .
        "Albania" 1979 .
        "Albania" 1980 .
        "Albania" 1981 .
        "Albania" 1982 .
        "Albania" 1983 .
        "Albania" 1984 .
        "Albania" 1985 .
        "Albania" 1986 .
        "Albania" 1987 .
        "Albania" 1988 .
        "Albania" 1989 .
        "Albania" 1990 .
        "Albania" 1991 .
        "Albania" 1992 .
        "Albania" 1993 .
        "Albania" 1994 .
        "Albania" 1995 .
        "Albania" 1996 .
        "Albania" 1997 .
        "Albania" 1998 .
        "Albania" 1999 .
        "Albania" 2000 .
        "Albania" 2001 .
        "Albania" 2002 .
        "Albania" 2003 .
        "Albania" 2004 .
        "Albania" 2005 .
        "Albania" 2006 .
        "Albania" 2007 .
        "Albania" 2008 .
        "Albania" 2009 .
        "Albania" 2010 .
        "Albania" 2011 .
        "Albania" 2012 .
        "Albania" 2013 .
        "Albania" 2014 .
        "Albania" 2015 .
        "Albania" 2016 .
        "Albania" 2017 .
        "Albania" 2018 .
        "Algeria" 1960 1
        "Algeria" 1961 .
        "Algeria" 1962 .
        "Algeria" 1963 .
        "Algeria" 1964 .
        "Algeria" 1965 1
        "Algeria" 1966 .
        "Algeria" 1967 1
        "Algeria" 1968 .
        "Algeria" 1969 .
        "Algeria" 1970 .
        "Algeria" 1971 .
        "Algeria" 1972 .
        "Algeria" 1973 .
        "Algeria" 1974 .
        "Algeria" 1975 .
        "Algeria" 1976 .
        "Algeria" 1977 .
        "Algeria" 1978 .
        "Algeria" 1979 .
        "Algeria" 1980 1
        "Algeria" 1981 .
        "Algeria" 1982 .
        "Algeria" 1983 .
        "Algeria" 1984 .
        "Algeria" 1985 1
        "Algeria" 1986 .
        "Algeria" 1987 1
        "Algeria" 1988 1
        "Algeria" 1989 1
        "Algeria" 1990 .
        "Algeria" 1991 .
        "Algeria" 1992 1
        "Algeria" 1993 .
        "Algeria" 1994 1
        "Algeria" 1995 .
        "Algeria" 1996 .
        "Algeria" 1997 .
        "Algeria" 1998 .
        "Algeria" 1999 1
        "Algeria" 2000 .
        "Algeria" 2001 .
        "Algeria" 2002 .
        "Algeria" 2003 2
        "Algeria" 2004 2
        "Algeria" 2005 .
        "Algeria" 2006 1
        "Algeria" 2007 .
        "Algeria" 2008 .
        "Algeria" 2009 .
        "Algeria" 2010 .
        "Algeria" 2011 .
        "Algeria" 2012 .
        "Algeria" 2013 .
        "Algeria" 2014 1
        "Algeria" 2015 .
        "Algeria" 2016 .
        "Algeria" 2017 .
        "Algeria" 2018 .
        "Angola"  1960 .
        "Angola"  1961 .
        "Angola"  1962 .
        "Angola"  1963 .
        "Angola"  1964 .
        "Angola"  1965 .
        "Angola"  1966 .
        end
        
        bys Country: egen freq= total(NoOfQuakes)
        sort freq Country
        gen order= sum(Country!= Country[_n-1]) 
        labmask order, values(Country)
        set scheme s1mono
        tw scatter order Year if NoOfQuakes [fw= NoOfQuakes ] , ///
        msy(oh) ylab(1/3, val angle(hor))  xlab(1960(5)2015) ytitle("") ///
        xtitle("") yline(1/3, lcolor(black) lp(dot)) ysc(r(0.5 3.5))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	32.9 KB
ID:	1710830

        Comment


        • #5
          Also note that the sample should include countries that have had at least one earthquake. So start with

          Code:
          bys Country (NoOfQuakes): drop if missing(NoOfQuakes[1])

          Comment


          • #6
            Originally posted by Andrew Musau View Post
            Also note that the sample should include countries that have had at least one earthquake. So start with

            Code:
            bys Country (NoOfQuakes): drop if missing(NoOfQuakes[1])
            Sorry for my delayed reply, I have been occupied with some other things. With a little bit of fiddling this is exactly what i was after. Thankyou very much for your haste response and expertise.

            By any chance, would it be possible to take a random selection of countries to use in the graph, as opposed to using all 58 countries that I have data for?

            Comment


            • #7
              Originally posted by Jack Bowley View Post
              By any chance, would it be possible to take a random selection of countries to use in the graph, as opposed to using all 58 countries that I have data for?
              Code:
              frame put Country, into(select)
              frame select{
                  contract Country, nomiss
                  gen rand= rnormal()
                  sort rand
                  *SELECT 20 (CHANGE TO SPECIFIC NUMBER)
                  keep in 1/20
              }
              frlink m:1 Country, frame(select)
              keep if !missing(select)
              frame drop select

              Comment


              • #8
                Here is another way to do it without frames. The premise is that we select either all observations or none for each country. Choose your own value of 20.

                select is 1 for selected countries.

                Code:
                egen tag = tag(Country)
                set seed 314159265
                gen rndm = runiform() if tag
                sort rndm
                gen select = _n <= 20
                bysort Country (select) : replace select = select[_N]
                Last edited by Nick Cox; 26 Apr 2023, 01:41.

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  Here is another way to do it without frames. The premise is that we select either all observations or none for each country. Choose your own value of 20.

                  select is 1 for selected countries.

                  Code:
                  egen tag = tag(Country)
                  set seed 314159265
                  gen rndm = runiform() if tag
                  sort rndm
                  gen select = _n <= 20
                  bysort Country (select) : replace select = select[_N]
                  Thankyou to you and Andrew for your advice. Sorry for the silly questions, how would I incorporate this code into Andrews aforementioned code so that the final graph uses the random selection code that you have described.

                  Comment


                  • #10
                    "aforementioned" here I think means #2. If so then the code should start

                    Code:
                     
                     tw scatter order Year if NoOfQuakes & select

                    Comment


                    • #11
                      Originally posted by Nick Cox View Post
                      "aforementioned" here I think means #2. If so then the code should start

                      Code:
                      tw scatter order Year if NoOfQuakes & select
                      Hi Nick, can you address where I'm going wrong. So before I use the random code, I use

                      Code:
                      tw scatter order Year if NoOfQuakes [fw= NoOfQuakes ] ,msy(oh) ylab(1/58, val angle(hor))  xlab(1960(5)2015) ytitle("Country") xtitle("Years") yline(1/58, lcolor(black) lp(dot)) ysc(r(0.5 3.5))
                      and obtain...
                      Click image for larger version

Name:	Graph for statalist.png
Views:	1
Size:	173.6 KB
ID:	1711612



                      Now after I run your code,

                      Code:
                      egen tag = tag(Country)
                      
                      . set seed 314159265
                      
                      . gen rndm = runiform() if tag
                      (3,364 missing values generated)
                      
                      . sort rndm
                      
                      . gen select = _n <= 20
                      
                      . bysort Country (select) : replace select = select[_N]
                      (1,160 real changes made)
                      
                      . tw scatter order Year if NoOfQuakes & select [fw= NoOfQuakes ] ,msy(oh) ylab(1/58, val angle(hor))  xlab(1960(5)2015) ytitle("Country") xtitle("Years") yline(1/58, lcolor(black) lp(dot)) ysc(r(0.5 3.5))
                      This isn't changing the graph above. Could you please identify my mistakes.

                      Thankyou again both Nick and Andrew.
                      Last edited by Jack Bowley; 28 Apr 2023, 10:10.

                      Comment


                      • #12
                        Possibly somethng to do with order and Country.

                        Comment

                        Working...
                        X