Announcement

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

  • Standardising weight for bubble charts across two variables and several graphs

    Hello,

    I wanted to kindly ask for your help regarding some bubble charts I am trying to create. (I tried to find solutions for my issue but have not managed to find anything..)

    Code for bubble charts:
    twoway scatter pov1 nyear1 [aw=npoor1] if region == "Arab States" & period == 1 || scatter pov2 nyear2 [aw=npoor2] if region == "Arab States" & period == 1

    twoway scatter pov1 nyear1 [aw=npoor1] if region == "East Asia and the Pacific" & period == 1 || scatter pov2 nyear2 [aw=npoor2] if region == "East Asia and the Pacific" & period == 1

    ... (for all other world regions)

    When plotting these graphs the size of the bubbles is relative to each other within each figure but not across the two figures and for both variables (npoor1 and npoor2). Is there any way of adjusting the code or preparing the data so that the bubble size if is relative to each other and across the different graphs?

    Data looks more or less like this:
    country region nyear1 nyear2 pov1 pov2 npoor1 npoor2 period
    Algeria Arab States 2006 2010 5 3 10 6 1
    Cambodia East Asia and the Pacific 2008 2012 10 8 12 10 1
    Egypt Arab States 2000 2005 9 6 15 10 1
    Fiji East Asia and the Pacific 2010 2015 2 1 2 1 1
    ...
    so Egypt's bubble for nyear1 should be bigger than Cambodia's nyear2 bubble but both should have the same size for year2.

    I would be grateful for any help!
    Alex

  • #2
    You will benefit from having a long layout. See

    Code:
    help reshape
    On between and within scaling of the weighted markers, see https://journals.sagepub.com/doi/pdf...36867X20931008.

    Comment


    • #3
      Many thanks for your response!!!

      I have reshaped the data and it does help with the issue, however, in the initial figure I also had a line between the two bubbles of a country - here is the code: (I apologise for not including it immediately, I didn't foresee that it would be relevant)

      twoway scatter pov1 nyear1 [aw=npoor1] if region == "Arab States" & period == 1 || scatter pov2 nyear2 [aw=npoor2] if region == "Arab States" & period == 1 || pcspike pov1 nyear1 pov2 nyear2 if region == "Arab States" & period == 1

      So each line would connect the bubbles from scatter pov1 with the bubbles from scatter pov2 for each country..

      The new code based on the long format looks like:
      twoway scatter pov nyear [aw=npoor] if region == "Arab States"

      How can I do the same with the a long layout?

      Best, Alex

      Comment


      • #4
        As I stated, a solution is provided in the linked article. The reshaping is just part of it.


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str8 country str25 region int(nyear1 nyear2) byte(pov1 pov2 npoor1 npoor2 period)
        "Algeria"  "Arab States"               2006 2010  5 3 10  6 1
        "Cambodia" "East Asia and the Pacific" 2008 2012 10 8 12 10 1
        "Egypt"    "Arab States"               2000 2005  9 6 15 10 1
        "Fiji"     "East Asia and the Pacific" 2010 2015  2 1  2  1 1
        end
        
        *ORIGINAL
         set scheme s1color
        twoway scatter pov1 nyear1 [aw=npoor1] if region == "Arab States" & period == 1, ms(oh) ///
        || scatter pov2 nyear2 [aw=npoor2] if region == "Arab States" & period == 1, ms(oh) ///
        leg(order(1 "1" 2 "2")) saving(gr1, replace)
        
        *RESHAPE + FILLIN
        reshape long nyear pov npoor, i(country region) j(which)
        *THE CATEGORICAL VARIABLE GROUP WILL DEFINE BOTH REGION AND WHICH
        egen group= group(region which), label
        fillin group npoor
         twoway scatter pov nyear [aw=npoor] if group==1, ms(oh) ///
        || scatter pov nyear [aw=npoor] if group == 2, ms(oh) ///
        leg(order(1 "1" 2 "2")) xtitle("") saving(gr2, replace)
        
        *SIDE-BY-SIDE
        gr combine gr1.gph gr2.gph
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.5 KB
ID:	1712915

        Comment


        • #5
          Dear Andrew,

          As stated in my message, I understood your previous message and I had already implemented it. But thank you for demonstrating it as well.

          My question was how I can replicated this part of the code "pcspike pov1 nyear1 pov2 nyear2 if region == "Arab States" & period == 1" which had constructed a line between the two bubbles for a country?

          Many thanks,
          Alex

          Comment


          • #6
            Create the pairs and merge back with the data in long layout.

            Comment


            • #7
              I am really sorry, I do not really follow. Could you elaborate what you mean exactly by creating the pairs?

              Comment


              • #8
                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input str8 country str25 region int(nyear1 nyear2) byte(pov1 pov2 npoor1 npoor2 period)
                "Algeria"  "Arab States"               2006 2010  5 3 10  6 1
                "Cambodia" "East Asia and the Pacific" 2008 2012 10 8 12 10 1
                "Egypt"    "Arab States"               2000 2005  9 6 15 10 1
                "Fiji"     "East Asia and the Pacific" 2010 2015  2 1  2  1 1
                end
                
                
                *RESHAPE + FILLIN
                reshape long nyear pov npoor, i(country region) j(which)
                *THE CATEGORICAL VARIABLE GROUP WILL DEFINE BOTH REGION AND WHICH
                egen group= group(region which), label
                fillin group npoor
                
                *FORM PAIRS
                cap frame drop merge
                frame put which nyear pov group, into(merge)
                frame merge{
                   contract which nyear pov group, nomiss
                   decode gr, g(gr2)
                   replace gr2= trim(ustrregexra(gr2, "\d", ""))
                   gen nyear2= nyear
                   gen pov2= pov
                   bys gr2 which (nyear): replace which=_n
                   egen gr3= group(gr2 which), label
                   bys gr3 which: replace nyear2=cond(_n==1, nyear[2], nyear[1])
                   bys gr2 which: replace pov2=cond(_n==1, pov[2], pov[1])
                }
                frlink m:1 group which nyear, frame(merge gr3 which nyear2)
                frget nyear2=nyear pov2=pov, from(merge)
                
                set scheme s1color
                 twoway scatter pov nyear [aw=npoor] if group==1, ms(oh) ///
                || scatter pov nyear [aw=npoor] if group == 2, ms(oh) ///
                || pcspike pov nyear pov2 nyear2 if group==1, ///
                || pcspike pov nyear pov2 nyear2 if group==2, ///
                leg(order(1 "1" 2 "2")) xtitle("")
                Res.:

                Click image for larger version

Name:	Graph.png
Views:	1
Size:	29.5 KB
ID:	1713133

                Comment

                Working...
                X