简体   繁体   中英

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:

There's another dataframe, called nomes , that has the names of variables I want to plot in column nomes and variable labels

# 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.

I've tried changing the main parameter and setting it equal to nomes$label[n] , but I get this error message:

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 (n in seq_along(nomes$nomes)) 

and replace every instance of n with nomes$nomes[n] . Then you can replace main=n with

main = nomes$label[n]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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