ipeadata-lab / ipeaplot

ipeaplot: criando graficos no padrão editorial do Ipea
https://ipeadata-lab.github.io/ipeaplot/
Other
1 stars 0 forks source link

`theme_ipea()` não funciona com dados espaciais #29

Closed PedroMilreuCunha closed 9 months ago

PedroMilreuCunha commented 10 months ago

Boa tarde, pessoal! Não sei se é por definição de vocês durante a criação do pacote ou se é um bug mesmo, mas a função theme_ipea() não funciona com dados espaciais. Segue um reprex abaixo:


#remotes::install_github("ipeadata-lab/ipeaplot")

library(ggplot2)
library(sf)
library(dplyr)
library(ipeaplot)

# read all states
states <- geobr::read_state(
    showProgress = TRUE
)

# Read data.frame with life expectancy data
df <- utils::read.csv(system.file("extdata/br_states_lifexpect2017.csv", package = "geobr"), encoding = "UTF-8")

states$name_state <- tolower(states$name_state)
df$uf <- tolower(df$uf)

# join the databases
states <- dplyr::left_join(states, df, by = c("name_state" = "uf"))

# plot the thematic map using theme_void 
ggplot() +
    geom_sf(data=states, aes(fill=ESPVIDA2017), color= NA, size=.15) +
    labs(subtitle="Life Expectancy at birth, Brazilian States, 2014", size=8) +
    theme_void()
# (works as expected)

# plot the thematic map using theme_ipea
ggplot() +
    geom_sf(data=states, aes(fill=ESPVIDA2017), color= NA, size=.15) +
    labs(subtitle="Life Expectancy at birth, Brazilian States, 2014", size=8) +
    theme_ipea()
# (doesn't work)

O erro é:

Error in lat > -90 : 
  comparison (>) is possible only for atomic and list types
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
5: In min(x) : no non-missing arguments to min; returning Inf
6: In max(x) : no non-missing arguments to max; returning -Inf
7: In min(x) : no non-missing arguments to min; returning Inf
8: In max(x) : no non-missing arguments to max; returning -Inf

Acredito que seja algo relacionado às escalas dos eixos.

Informações da minha sessão:

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Brazil.utf8  LC_CTYPE=Portuguese_Brazil.utf8    LC_MONETARY=Portuguese_Brazil.utf8
[4] LC_NUMERIC=C                       LC_TIME=Portuguese_Brazil.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ipeaplot_0.0.1 dplyr_1.1.2    sf_1.0-12      ggplot2_3.4.3 

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10        pillar_1.9.0       compiler_4.2.2     geobr_1.7.0        sysfonts_0.8.8    
 [6] class_7.3-20       tools_4.2.2        extrafont_0.19     checkmate_2.2.0    lifecycle_1.0.3   
[11] tibble_3.2.1       gtable_0.3.4       pkgconfig_2.0.3    rlang_1.1.1        DBI_1.1.3         
[16] cli_3.6.1          rstudioapi_0.15.0  curl_5.0.0         Rttf2pt1_1.3.12    e1071_1.7-13      
[21] httr_1.4.5         showtextdb_3.0     withr_2.5.0        generics_0.1.3     vctrs_0.6.3       
[26] classInt_0.4-9     grid_4.2.2         tidyselect_1.2.0   glue_1.6.2         data.table_1.14.8 
[31] R6_2.5.1           fansi_1.0.4        farver_2.1.1       extrafontdb_1.0    magrittr_2.0.3    
[36] backports_1.4.1    scales_1.2.1       units_0.8-1        showtext_0.9-6     colorspace_2.1-0  
[41] labeling_0.4.3     utf8_1.2.3         KernSmooth_2.23-20 proxy_0.4-27       munsell_0.5.0  
cavalcanti1985 commented 10 months ago

Boa tarde, @PedroMilreuCunha, obrigado pelo issue.

Para conseguir utilizar o theme_ipea() com dados espaciais, você precisa incluir o parâmetro axis_lines = 'none'.

Veja o exemplo:

# Load municipality and state spatial data
mun = read_municipality()
uf  = read_state()

# Subset and select specific columns from the 'pnud_muni' dataset
df_escola <- df |>
             subset(ano == 2010) %>%
             select(ano, code_muni = codmun7, e_anosestudo)

# Perform a left join between the 'mun' and 'pnud' data frames
df3 <- dplyr::left_join(mun, df_escola, by = 'code_muni')

ggplot() +
        geom_sf(data = df3, aes(fill = e_anosestudo), color = NA) +
        geom_sf(data = uf, color = "black", fill = NA) +
        ggtitle("Média de anos de estudo") +
        scale_fill_ipea(palette = 'Orange-Blue', 
                             name='Anos de\nestudo') + 
        theme_ipea(axis_lines = 'none', include_ticks = F, axis_values = F)

De fato, existe um bug que não permite o funcionamento do theme com a opcao default do atributo axis_lines. Estamos trabalhando em uma solução mais intutitiva para os usuários, por isso deixarei o issue aberto.

cavalcanti1985 commented 10 months ago

@PedroJorge7, enquanto não temos uma solução melhor para o bug, devemos configurar uma mensagem de erro que oriente o usuário a incluir o parâmetro axis_lines = 'none'.

PedroJorge7 commented 9 months ago

Fiz a correção desse problema. O comando ainda aparece alguns warnings, mas o mapa é gerado