Announcement

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

  • Controlling size of legend in spmap

    Dear Statalisters,

    I have a smallish kind of problem with spmap that, despite trying many things, has proven impossible to resolve for me. Any help would be greatly appreciated.

    My aim is to draw a proportional symbol map using spmap, wherein I superimpose two sets of events (the first in non-colonial contexts and the second in colonial contexts) on a world map that I downloaded from Natural Earth. Everything works out alright, except for the legend. While I managed to get spmap to show a legend, it is so small that it is simply impossible to read. So my question is: how can I control the size of a legend in spmap?

    This is the code I used to draw the map:

    spmap using worldcoordinates if admin !="Antarctica", ///
    id(id_shp) fcolor(gs10) ocolor(white) ///
    point(data("Geo-SDrefs.dta") x(longitude) y(latitude) proportional(count) by(colonial) fcolor(blue red) legenda(on) legshow(2 1))


    I tried two things, both unsuccessfully. First, via the standard legend option, adding something like this to the code:

    legend(symysize(*5) symxsize(*5) textwidth(*5) keygap(*5))

    Second, I tried to manually increase the size of the keys and text using the graph editor, which works, but then also the markers on the map become (too) big).


    Kind regards,

    Micha Germann

  • #2
    Just thought I give this a second try by pushing it up a bit...

    Comment


    • #3
      Try the size option. For example:
      Code:
      legend(size(medium))

      Comment


      • #4
        Hi Friedrich,

        Thanks for the reply. Unfortunately, the size option changes only the size of the text, while the keys remain unaffected (i.e. too small), and what I usually use in such cases (symxsize(*5)) does not appear to work.

        Best,

        Micha

        Comment


        • #5
          The problem you described applies to any graph type with symbols, including scatter plots. The symbols in the legend have the same size as the symbols in the graph. In the command below, the symxsize() option has no effect.
          Code:
          sysuse auto, clear
          twoway (scatter mpg weight, msize(*2)) ///
          (scatter mpg length), legend(symxsize(*3))
          You can solve this by adding plots without data to the graph and showing the symbols for those plots in the legend. In the example below, the first two scatter plots contain data. The other two scatter plots contain no data and are used to define the appearance of the symbols that are shown in the legend. This approach should also work with spmap.
          Code:
          sysuse auto, clear
          gen nodata = .
          twoway (scatter mpg weight, msize(*.5)) ///
          (scatter mpg length, msize(*1.5)) ///
          (scatter mpg nodata, mcolor(navy) msize(*3)) ///
          (scatter mpg nodata, mcolor(maroon) msize(*3)), ///
          legend(order(3 4))

          Comment


          • #6
            Thanks for the hint. Unfortunately, this approach does not work as is with spmap.

            The problem is that if you wrap spmap into this Stata won't accept, telling us that "spmap is not a twoway plot type"

            However, with some tweaking and drawing on the the user-written addplot programme by Ben Jann it does work. In case anyone is interested, here my (probably not too efficient) solution:

            For this the legend needs to be placed in the plot region using legend(ring(0)). bplacement() simply controls the location of the legend in the plot region. The margins of the graph and plot regions are afterwards manually tweaked. Important: specify "norescaling" so that the map is not rescaled. And finally, the location of the legend can be fine-tuned using the graph editor.


            Code:
            ssc install addplot, replace
            
            gen nodata1 = .
            label var nodata1 "The label of group 1"
            gen nodata2 = .
            label var nodata2 "The label of group 2"
            
            
            spmap using worldcoordinates, ///
            id(id_shp) mfcolor(white) mocolor(white) fcolor(gs10) ocolor(none) ///
            point(data("yourdata.dta") x(your x coordinates variable) y(your y coordinates variable) proportional(count) by(grouping variable) fcolor(blue red))
            addplot: (scatter nodata1 y_c, msize(*2) mcolor(red) msym(circle)) ///
            || (scatter nodata2 y_c, msize(*2) mcolor(blue) msym(circle)) ///
            , ///
            legend(on order(5 6) size(*1.7) cols(1) region(lstyle(none)) ///
            ring(0) bplacement(8)) ///
            plotregion(margin(-18 -18 -18 0)) norescaling
            gr_edit .legend.xoffset = -18
            gr_edit .legend.yoffset = -2
            Last edited by Micha Germann; 05 Aug 2015, 13:00.

            Comment

            Working...
            X