2017-09-28 4 views
1

第コード:TMAPパッケージ:同じtm_fillに複数tm_text()色依存行う()値

library(tmap) 
library(tmaptools) 

data("Europe") 

# subset just three countries for illustration 
threeC <- Europe[Europe$sovereignt %in% c("Germany", "Poland", "Ukraine"), ] 

tm_shape(threeC) + 
    tm_polygons() + 
    tm_shape(threeC) + 
    tm_borders() + 
    tm_fill("pop_est", 
      palette = get_brewer_pal("YlGnBu"), 
      legend.show = F, 
      style = "order") + 
    tm_text("sovereignt", size = .8) + 
    tm_shape(threeC) + 
    tm_text("pop_est", size = .8, auto.placement = .1) 

結果は: 3 countries with text labels

質問1:それは "を示すことができますpop_est "は、tm_text()関数内の式を使用して82.32m、38.48m、45.70mのように2桁の小数点以下2桁の数百万でマップします(つまり、データフレームに別のカスタム列を作成せずに、" pop_est ")?

質問2:国名を示すテキストの色は黒または白です(ここでは「ドイツ」は白で、他の2つの国名は黒で印刷されています)。塗りつぶしの色によって、大きな地図での視認性に大きく役立ちます。しかし、 "pop_est"数字を示す追加テキストの色は常に黒です。視認性を高めるために塗りつぶしの色に応じて黒または白にする方法?

答えて

0

回答1:事前にカスタム列を作成せずにそれを行うための方法はありませんように、いくつかの実験をした後、それはそうです:

threeC$pop_est.m <- (threeC$pop_est/1e6) %>% sprintf("%.2f", .) %>% paste0("m") 

回答2:以前のすべてのオプションで別のtm_fill機能を使用してaddtionallyそれを作ります透明で。

したがって改訂コードは次のようになります

所望の結果を与えるであろう
library(tmap) 
library(tmaptools) 

data("Europe") 

# subset just three countries for illustration 
threeC <- Europe[Europe$sovereignt %in% c("Germany", "Poland", "Ukraine"), ] 
threeC$pop_est.m <- (threeC$pop_est/1e6) %>% sprintf("%.2f", .) %>% paste0("m") 

tm_shape(threeC) + 
    tm_polygons() + 
    tm_shape(threeC) + 
    tm_borders() + 
    tm_fill("pop_est", 
      palette = get_brewer_pal("YlGnBu"), 
      legend.show = F, 
      style = "order") + 
    tm_text("sovereignt", size = .8) + 
    tm_shape(threeC) + 
    tm_fill("pop_est", 
      palette = get_brewer_pal("YlGnBu"), 
      legend.show = F, 
      style = "order", 
      alpha = 0) + # make it transparent 
    tm_text("pop_est.m", size = .8, auto.placement = .1) 

custom map
関連する問題