2017-04-15 571 views
0

我想创建一个函数来映射特定州的学区的不同变量。但是我遇到了一些格式化图例的问题。目前,我已经制定了适合地图(水平)的最佳方式,但部分文本正在被切断(低于平均贫困率),并且我想将百分比添加到图例中的数字标签。下面是我的代码和图例。任何帮助你可以提供将非常感激。谢谢。R:tmap图例格式

MakeLEAMap <-function(StateNum,NCHE_VAR,VAR1_NAME,In,Folder){ 
    as.character(substitute(StateNum)) 
    as.character(substitute(NCHE_VAR)) 
    as.character(substitute(NCHE_In)) 
    as.character(substitute(VAR1_NAME)) 
    as.character(substitute(Folder)) 

    map <- 
    tm_shape(LEA_1415_New[[email protected]$STATEFP == StateNum, ]) + 
    tm_polygons(NCHE_VAR,border.col="#000000", lwd= .5, textNA="Below Average \nPoverty Rate" , palette = 'Blues', style="quantile", 
       title=paste(In," State LEA Map: ",VAR1_NAME), 
       legend.is.portrait = FALSE) + 
    tm_text("LCITY", size=NCHE_VAR,scale=.8, root=2,print.tiny = FALSE, size.lowerbound = .85, bg.alpha = .75, 
      remove.overlap = TRUE,legend.size.show = FALSE, col="black") + 
    tm_layout(legend.title.size = 3, 
       frame = FALSE, inner.margins = c(0,.0,.05,.0), asp = 1.5, 
       legend.text.size = 1, 
       legend.outside=TRUE, legend.outside.position = 'bottom', 
       legend.frame = TRUE, 
       legend.outside.size = .3, legend.position = c(-0.1, 0.3)) 

    save_tmap(map, filename=paste("State_Maps_TEST/",Folder,"/",In,".pdf", sep = ''),width=8, height=8) 
} 
MakeLEAMap("48","Abv_Diff_Home_Pov","% Children in Poverty minus \n% Children HCY (Ages5-17)", 
      "TX","ALL") 

Here is what the legend looks like now

+0

我认为这是相关的:[在传奇中格式化数字](https://github.com/mtennekes/tmap/issues/67) – Valentin

回答

0

为了让传说秀百分比使用此功能,您tm_polygons调用内部:

legend.format =名单(FUN =函数(X)paste0(formatC(X,数字= 0,格式=“f”),“%”))

你可以玩数字(小数点),你可以在%符号前面放置空格,陛下。

为了使说明更清晰增加周围的地图通过制造更大的BBOX的空间(可能使用extent函数从raster包读取您的空间物体的BBOX然后放大它),并通过调整其位置移动图例。

这就是我在不同的背景下想出来的,但也有一个在tmap图例中也要求百分比符号的图例。 enter image description here