简体   繁体   English

R:使用来自forloop的数据帧中的一列元素命名映射,并使用来自另一列的元素命名其文件

[英]R: Naming a map with elements from a column and naming its file with elements from another column in a dataframe with a forloop

I have georeferenced data in a dataframe called dados and I'm making maps from it with a forloop: 我已经在称为dados的数据帧中对数据进行了地理参考,并且正在使用forloop从中创建地图:

There's another dataframe, called nomes , that has the names of variables I want to plot in column nomes and variable labels 还有一个称为nomes的数据nomes ,其中包含我要在列nomes和变量标签中绘制的变量的名称

# A tibble: 18 x 3
index nomes                  label                                       
<dbl> <chr>                  <chr>                                       
1     1 v_a_s_emp_3112         Variação anual média: Total de empregados e~
2     2 v_a_s_emp_medio        Variação anual média: Soma do emprego medio~
3     3 v_a_emprego_total      Variação anual média: Emprego total (BNDES) 
4     4 v_a_h                  Variação anual média: Total de contratações 
5     5 v_a_s                  Variação anual média: Total de desligamentos
6     6 v_a_prop_emprego_3     Variação anual média: Proporção de estab. c~
7     7 v_a_estab              Variação anual média: Número de estabelecim~
8     8 v_a_emprego_medio      Variação anual média: Média de empregados n~
9     9 v_a_emprego_medio_ent~ Variação anual média: Média do emprego dos ~
10    10 v_a_nascimento_data    Variação anual média: Número de estab. entr~
11    11 v_a_nascimento_rais    Variação anual média: Número de estab. entr~
12    12 v_a_morte_rais         Variação anual média: Número de estab. ause~
13    13 v_a_tx_sobrev          Variação anual média: Taxa de sobrevivência~
14    14 v_a_n_solicitacao      Variação anual média: Número de estabelecim~
15    15 v_a_prop_idade_3       Variação anual média: Proporção de estab. c~
16    16 v_a_n_uso              Variação anual média: Número de estabelecim~
17    17 v_a_valorfinanc        Variação anual média: Valor financiado em R~
18    18 v_a_valor_invest       Variação anual média: Bens de investimento ~

The code that generates and saves the maps is this one and it works. 生成和保存地图的代码就是这一代码,并且可以正常工作。

for (n in nomes$nomes) {

png(filename = paste("mapa_", n,".png", sep = ""), width = 1200,
height = 700, res = 200)

print(spplot(dados, c(n),
       col.regions = cores,
       at = intervalos,
       colorkey = list(at = c(-15, 0,20,40,60,80,100)),
       par.settings = my.settings,
       main = n) #title

)

dev.off()

}

The problem is the maps have names that are hard to understand and present. 问题在于地图的名称很难理解和呈现。

在此处输入图片说明

I want the map's title to be the element in nomes$label in the same row of the element in nomes$nomes being plotted. 我希望地图的标题是nomes$nomes被绘制元素的同一行中nomes$label中的元素。

I've tried changing the main parameter and setting it equal to nomes$label[n] , but I get this error message: 我尝试更改main参数并将其设置为等于nomes$label[n] ,但收到以下错误消息:

Error in if (with(lab, is.null(label) || (is.character(label) && (label ==  : 
missing value where TRUE/FALSE needed

Sadly I can't provide data for you to reproduce the maps fully because it's administrative. 遗憾的是,由于它是管理性的,因此我无法为您提供完整再现地图的数据。 Any help is appreciated. 任何帮助表示赞赏。

Maybe this would work: change the for call as follows: 也许可以这样做:如下更改for调用:

for (n in seq_along(nomes$nomes)) 

and replace every instance of n with nomes$nomes[n] . 并将n每个实例替换为nomes$nomes[n] Then you can replace main=n with 然后您可以将main=n替换为

main = nomes$label[n]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM