这是indexloc提供的服务,不要输入任何密码
Skip to content

How to position the legend outside when plotting several layers #1164

@bienflorencia

Description

@bienflorencia

I need help to locate the legend of my maps outside when I have several layers. How can I do this?

Here is the code I'm using:

tm_shape(predictors) +
    tm_polygons(c('precipitation', 'temperature', 'footprint', 'cropland'), 
                fill_alpha = 0.9,
                col='grey45', col_alpha = 0.2, lwd = 0.25,
                fill.scale = list(
                    tm_scale_intervals(n = 6, style = 'pretty', 
                                       midpoint = 0,
                                       values = 'brewer.pu_bu',
                                       value.na = 'grey80'),
                    tm_scale_intervals(n = 6, style = 'pretty', 
                                       midpoint = 0,
                                       values = 'brewer.yl_or_rd',
                                       value.na = 'grey80'),
                    tm_scale_intervals(n = 6, style = 'pretty', 
                                       midpoint = 0,
                                       values = 'brewer.purples',
                                       value.na = 'grey80'),
                    tm_scale_intervals(n = 6, style = 'pretty', 
                                       midpoint = 0,
                                       values = 'brewer.pu_bu_gn',
                                       value.na = 'grey80')),
                fill.legend = tm_legend(#title='',
                    item.space = 0,
                    reverse=T,
                    frame=F,
                    frame.lwd = 0.1,
                    bg.color = 'white',
                    position = tm_pos_out()) 
    ) +
    tm_layout(panel.show = FALSE) + 
    tm_crs(crs=st_crs(4326)) + 
    tm_options(component.autoscale=F)

And this is the map I get, see the legend overlapped:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions