benjann / geoplot

Stata module to draw maps
MIT License
33 stars 3 forks source link

Legend title over two lines #21

Closed ericmelse closed 11 months ago

ericmelse commented 1 year ago

Dear Ben, I tried to set the title of a legend over two lines using `" "Mobility" "Av. pers. day" "' using:

local textclr ""58 144 254"*1.4"
geoplot ///
    (area regions moveTot, lw(*.3) lc(gs7) ///         
    cuts(2.5(.05)2.9) /// => 17 bins
        color(gs14 "168 144 8", ipolate(10) intensity(.8))) ///
    , xsize(4.2) ysize(5) ///
    legend(pos(5) layout(1 - `" "Mobility" "Av. pers. day" "')) /// , m(t+1 b-1)
    compass(t(1) pos(2) si(2.3) xm(.2) ym(.2) c(gs9) lab(si(*.5) c(`textclr') gap(50))) /// 
    sbar(units(km) ym(2) c(gs8) lab(si(*.55) c(gs5)))

But, although geoplot does run, without any complaint, the result is such that a single line is used, expanding the width of the legend, like: CBS_Legend_Title_Two_Lines_XL Do you think it is possible to adjust the geoplot ado file as to support using multiple lines for the title of the legend?

Furthermore, it seems that it also not possible (as far as I can understand from the help file) to set properties of the legend title text, like its color or font size (possibly also its text box margins).

benjann commented 1 year ago

Use legend(title()) to set the title of the legend; or legend(subtitle()) or legend(note()) or whatever; i.e. use standard legend suboptions as document in -help legend_option-.

Syntax - "text" within layout() adds labels without symbol to the list of legend keys; of course you can use this to set a legend title, but you will be limited in terms of styling. However, you can used SMCL tags, e.g. - "{bf:my heading)". To generate multiline labels, type - "line 1" "line 2" ... (i.e. simply provide multiple text elements).

ericmelse commented 1 year ago

Yes, Ben, thanks again. Using layout(1 - "Mobility" "Av. pers. day")) indeed works just fine. I am so 'used' to use for multilines ""Mobility" "Av. pers. day"" that I did not expect that I could do without the doubly double quote markers.

benjann commented 1 year ago

Stata is not fully consistent here. For multiline tick labels, the outer quotes are needed. For multiline legend labels, they have to be omitted. geoplot behaves in the same way.

Note that you should actually type `""Mobility" "Av. pers. day""' (i.e. use "compound" double quotes) rather than ""Mobility" "Av. pers. day"". The latter works in some situations (e.g. when defining a local) but not in others (e.g. when specifying a multiline label in xlabel() or similar).