Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with zoom in tm_borders() #634

Closed
rafapereirabr opened this issue Mar 9, 2022 · 5 comments
Closed

Problem with zoom in tm_borders() #634

rafapereirabr opened this issue Mar 9, 2022 · 5 comments

Comments

@rafapereirabr
Copy link

rafapereirabr commented Mar 9, 2022

Hi all. I'm trying to plot a map with tmap but it's showing a somewhat strange behavior. Pleasse see the reproducible example below:

# devtools::install_github("ipeaGIT/geobr", subdir = "r-package")
library(tmap)
library(geobr)

# download data of Brazilian states
brasil <- read_state(code_state = "all")

# plot 1
tm_shape(brasil) + tm_borders()

Rplot01

For some reason, this plot zooms in one specific state of Brazil. However, if I run a simple subset remove any state, the tm_borders() returns the correct map showing the whole country. In fact, this solution works even when I run a subset removing a state that does not existe in the data.

# subset
brasil2 <- subset(brasil, abbrev_state  != "RR")
brasil3 <- subset(brasil, abbrev_state  != "xx")

# plot
tm_shape(brasil2) + tm_borders()
tm_shape(brasil3) + tm_borders()

Rplot073
Rplot072

Do you have any idea what this is happening? thanks for the help and for such a great package!

@Nowosad
Copy link
Member

Nowosad commented Mar 9, 2022

Hi @rafapereirabr -- it seems to be a deeper (than tmap) problem:

# devtools::install_github("ipeaGIT/geobr", subdir = "r-package")
library(tmap)
library(geobr)

# download data of Brazilian states
brasil <- read_state(code_state = "all")
#> Using year 2010
#>   |                                                                              |                                                                      |   0%  |                                                                              |===                                                                   |   4%  |                                                                              |=====                                                                 |   7%  |                                                                              |========                                                              |  11%  |                                                                              |==========                                                            |  15%  |                                                                              |=============                                                         |  19%  |                                                                              |================                                                      |  22%  |                                                                              |==================                                                    |  26%  |                                                                              |=====================                                                 |  30%  |                                                                              |=======================                                               |  33%  |                                                                              |==========================                                            |  37%  |                                                                              |=============================                                         |  41%  |                                                                              |===============================                                       |  44%  |                                                                              |==================================                                    |  48%  |                                                                              |====================================                                  |  52%  |                                                                              |=======================================                               |  56%  |                                                                              |=========================================                             |  59%  |                                                                              |============================================                          |  63%  |                                                                              |===============================================                       |  67%  |                                                                              |=================================================                     |  70%  |                                                                              |====================================================                  |  74%  |                                                                              |======================================================                |  78%  |                                                                              |=========================================================             |  81%  |                                                                              |============================================================          |  85%  |                                                                              |==============================================================        |  89%  |                                                                              |=================================================================     |  93%  |                                                                              |===================================================================   |  96%  |                                                                              |======================================================================| 100%

plot(brasil)

brasil3 <- subset(brasil, abbrev_state  != "xx")

plot(brasil3)

Created on 2022-03-09 by the reprex package (v2.0.1)

@Nowosad
Copy link
Member

Nowosad commented Mar 9, 2022

Bounding box information seems to be off:

# devtools::install_github("ipeaGIT/geobr", subdir = "r-package")
library(tmap)
library(geobr)
library(sf)
#> Linking to GEOS 3.9.2, GDAL 3.3.2, PROJ 8.2.1; sf_use_s2() is TRUE

# download data of Brazilian states
brasil <- read_state(code_state = "all")
#> Using year 2010
#>   |                                                                              |                                                                      |   0%  |                                                                              |===                                                                   |   4%  |                                                                              |=====                                                                 |   7%  |                                                                              |========                                                              |  11%  |                                                                              |==========                                                            |  15%  |                                                                              |=============                                                         |  19%  |                                                                              |================                                                      |  22%  |                                                                              |==================                                                    |  26%  |                                                                              |=====================                                                 |  30%  |                                                                              |=======================                                               |  33%  |                                                                              |==========================                                            |  37%  |                                                                              |=============================                                         |  41%  |                                                                              |===============================                                       |  44%  |                                                                              |==================================                                    |  48%  |                                                                              |====================================                                  |  52%  |                                                                              |=======================================                               |  56%  |                                                                              |=========================================                             |  59%  |                                                                              |============================================                          |  63%  |                                                                              |===============================================                       |  67%  |                                                                              |=================================================                     |  70%  |                                                                              |====================================================                  |  74%  |                                                                              |======================================================                |  78%  |                                                                              |=========================================================             |  81%  |                                                                              |============================================================          |  85%  |                                                                              |==============================================================        |  89%  |                                                                              |=================================================================     |  93%  |                                                                              |===================================================================   |  96%  |                                                                              |======================================================================| 100%
brasil3 <- subset(brasil, abbrev_state  != "xx")

st_bbox(brasil)
#>       xmin       ymin       xmax       ymax 
#> -66.810253 -13.693700 -59.774353  -7.969294
st_bbox(brasil3)
#>       xmin       ymin       xmax       ymax 
#> -73.990450 -33.752081 -28.835944   5.271841

Created on 2022-03-09 by the reprex package (v2.0.1)

@Nowosad
Copy link
Member

Nowosad commented Mar 9, 2022

@rafapereirabr
Copy link
Author

Thanks for the heads up @Nowosad . This was on the spot! (see test below). I am now wondering, though, why data.table::rbindlist() is messing up with the bounding box? and whether this should be raised as an issue on the data.table repo.

library(geobr)
library(sf)
library(data.table)

# download data
rr <- read_state(code_state = 'RR')
rs <- read_state(code_state = 'RS')

test_list <- list(rr, rs)

# row bind with rbindlist
t1 <- sf::st_as_sf(data.table::rbindlist(test_list, fill = TRUE)) 
plot(t1['code_state'])

# base row bind
t2 <- rbind(rr,rs)
plot(t2['code_state'])


st_bbox(t1)
>       xmin       ymin       xmax       ymax 
> -64.825251  -1.580633 -58.886884   5.271841 

st_bbox(t2)
>       xmin       ymin       xmax       ymax 
> -64.825251 -33.752081 -49.691460   5.271841 

@Robinlovelace
Copy link
Collaborator

Robinlovelace commented Mar 13, 2022

Heads-up @rafapereirabr this is a known issue and I've been bitten by it before. I recall seeing recent discussion and maybe even development towards a fix of better data.table/sf integration but not sure if the bbox issue, which is a price you pay for waaay faster rbinding, is fixed. There are work-arounds. Keep us posted with how you get on but this is a tmap issue so I closing it here. 👍 to opening an issue on the data.table issue tracker. Quick glance at this PR suggests that they are unaware of the issue: Rdatatable/data.table#2273

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants