How to manipulate tmap legend?

空扰寡人 提交于 2020-04-07 12:41:59

问题


I'm creating a thematic map of percent change per year for bird species. here is the code I have:

tm_shape(grid83)+
  tm_fill("trend", title = "Percent change per Year", textNA = "None counted", style="fixed",
    breaks=c(-Inf, -1.5, -0.25, 0.25, 1.5, Inf),
    palette = c("red", "orange", "yellow", "turquoise", "blue", "white"))+
  tm_borders(NA)+
tm_shape(uscan83)+ # add US and CAN 
  tm_borders()+
tm_layout(
  "Western Grebe",
  legend.title.size=1,
  legend.text.size = 0.6,
  legend.position = c("left","bottom"),
  legend.bg.color = "white",
  legend.digits = 5,
  legend.bg.alpha = 1)

Currently all NA values show up as gray. I've tried to change the color palette:

palette = c("red", "orange", "yellow", "turquoise", "blue", "white"))

but this doesn't seem to be working. NA values are all still gray. What am I doing wrong?

Thanks so much!


回答1:


So you are trying to change the color specifically for NA values? The colorNA argument to tm_fill() serves that purpose.

Here's an example:

library(tmap)
data(Europe)
tm_shape(Europe) +
tm_fill("gdp_cap_est", title = "GDP", style = "fixed",
        breaks = c(0, 10000, 20000, 30000, 40000, Inf),
        textNA = "Dunno", 
        colorNA = "green",   # <-------- color for NA values
        palette = c("red", "orange", "yellow", "turquoise", "blue", "white")) +
tm_borders() +
tm_layout("Wealth (or so)",
          legend.title.size = 1,
          legend.text.size = 0.6,
          legend.position = c("left","bottom"),
          legend.bg.color = "white",
          legend.digits = 5,
          legend.bg.alpha = 1)

It looks like this:



来源:https://stackoverflow.com/questions/32890762/how-to-manipulate-tmap-legend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!