user36756
user36756

Reputation: 21

tm_symbols: how to control size?

I am new to tmap. I am working with tm_dots and I want the size of dots to be determined by a numeric variable. So I wrote tm_dots(size="variable"). I would like to control manually the size of the dots specifying breaks. If I use sizes.legend I can modify the dots in the legend, but this is not reflected in the size of the dots on the map.

Notably, the same is easily accomplished with colours: tm_dots(col="variable",style="fix",breaks = c(45, 60, 75, 90)) But seems to be not available for size. I am wondering why...

thanks

Upvotes: 0

Views: 1773

Answers (1)

dieghernan
dieghernan

Reputation: 3402

What is your expected outcome? On this context tmap treats the size as a continous variable, that's why sizes.legend only modifies the legend, and not the size of the dots (the variable of each point has not changed).

library(sf)

nc <- st_read(system.file("shape/nc.shp", package="sf"))
nc_point <- st_centroid(nc, of_largest_polygon = TRUE)


# Create something in your range
nc_point$variable <- nc_point$NWBIR74/100

library(tmap)

tm_shape(nc_point) +
  tm_dots(size="variable")

enter image description here

tm_shape(nc_point) +
  tm_dots(size="variable",
          sizes.legend = c(0, 45, 60, 75, 90)) +
  tm_layout(title = "Changes only the legend")

enter image description here The col option behaves differently, since it creates categories (i.e. make cuts on the continuous value) and then plots the categories instead the continuous values:

tm_shape(nc_point) +
  tm_dots(col="variable",
          size=2,
          breaks = c(0, 45, 60, 75, 90), pal=hcl.colors(10))

enter image description here

Note the legend. So if you want the size to behave similar to the col you may need to categorise it first (via cuts) and label the legend using legend.format:

# Create cuts, levels are the left limit
nc_point$cat <- cut(nc_point$variable,
  breaks = c(0, 45, 60, 75, 90),
  labels = FALSE
)

tm_shape(nc_point) +
  tm_dots(size = "cat", legend.format = list(fun = function(x) {
    c(45, 60, 75, 90)[x]
  }))

enter image description here

Hope this helps you

Upvotes: 1

Related Questions