Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a legend show size and color for tm_bubbles in R?

Tags:

r

tmap

I am trying to develop a simple map with bubbles representing size.

library(tmap)
data(World, metro)

example.map <- tm_shape(World)+
       tm_borders()+
       tm_shape(metro)+
       tm_bubbles(size = "pop2010", 
           col = "pop2010",
           palette = "-RdYlBu")
example.map

The actual map is fine, it is what I want. My problem is with the legend. In the code above there are two, I can make it one by using:

legend.col.show = FALSE

or

legend.size.show = FALSE

But then I have a legend that shows either the size or the colour. Is it possible to make a single legend show both size and colour?

like image 633
Novo88 Avatar asked Sep 03 '25 16:09

Novo88


1 Answers

I had the same question so posted an issue on the tmap Github page. The package author kindly responded, https://github.com/mtennekes/tmap/issues/208#issuecomment-392348083, and copied here for reference:

"Unfortunately, this is not possible, at least automatically. However, you can add a manual legend:

data(metro)

tm_shape(metro) +
   tm_dots() +
   tm_add_legend("symbol", col = c("green", "blue"), size = c(.1,.5), labels = c("Cat1", "Cat2"))
like image 153
Jason Avatar answered Sep 05 '25 07:09

Jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!