r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
858 stars 120 forks source link

Error in valid.viewport(x, y, width, height, just, gp, clip, mask, xscale #935

Open anjelinejeline opened 3 weeks ago

anjelinejeline commented 3 weeks ago

Hello, I am trying to plot the ADM2 shapefile that can be found here https://www.geoboundaries.org/globalDownloads.html However I am facing this error

Error in valid.viewport(x, y, width, height, just, gp, clip, mask, xscale,  : 
  invalid 'xscale' in viewport
In addition: Warning message:
The shape World is invalid. See sf::st_is_valid

I tried fixing it with tmap_options(check.and.fix = TRUE) and sf_use_s2(FALSE) but none of them worked. I read that others faced similar issues https://github.com/r-tmap/tmap/issues/606 But I cannot understand what the problem is in my case given that I can plot it with the plot function of sf.

mtennekes commented 3 weeks ago

Strange, plotting with tmap works for me:

library(sf)

x = st_read("sandbox/geoBoundariesCGAZ_ADM2/geoBoundariesCGAZ_ADM2.shp")

tm_shape(x) +
    tm_polygons()

Although st_is_valid is not always TRUE:

> which(!(st_is_valid(x)))
 [1]  3930  6416 13555 13629 13658 14644 20263 20267 27839 37796 39819 40614 41306 42077
> x = st_make_valid(x)
> which(!(st_is_valid(x)))
[1]  6416 13555 14644 39819

Even applying st_make_valid didn't work.

(I still have to implement check.and.fix, which basically consists of st_make_valid)

anjelinejeline commented 3 weeks ago

Hello @mtennekes I am using version 3.3.-4, how do you suggest I fix the issue? I really would like to use tmap to make maps instead of other packages..

Also, in general I am really struggling with using tmap lately .. some functions seem not work . Eg. I am using tm_dots in my code but the arguments related to the title and palette are ignored. When I run this simple code

   tm_shape(EIOS_sf) +
   tm_dots(col = "disease", palette="set1", title = "Disease") +
  tm_facets("YearMonth")

I get this

── tmap v3 code detected ────────────────────────────────────────────────────────────────
[v3->v4] `tm_dots()`: migrate the argument(s) related to the scale of the visual
variable `fill` namely 'palette' (rename to 'values') to fill.scale = tm_scale(<HERE>).

When I change the argument palette to values I still have issue with the title that is ignored.

I am just wondering whether all these problems are related to the version I am using

Thanks for your help

anjelinejeline commented 2 weeks ago

Just to continue with the above comment, I noticed a very strange behaviour of the package If I try to plot the World shapefile from rnaturalearth the plot looks very strange. Given all these issues I do believe that there's a problem with the version I am using

World=rnaturalearth::ne_countries(scale = "medium", returnclass = "sf")
 tm_shape(World) +

tm_borders(col="black")
Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

tmap_options(check.and.fix = TRUE)
tm_shape(World) +
tm_borders(col="black")
Warning message:
The shape World is invalid. See sf::st_is_valid 

image

mtennekes commented 2 weeks ago

Hello @mtennekes I am using version 3.3.-4, how do you suggest I fix the issue? I really would like to use tmap to make maps instead of other packages..

Also, in general I am really struggling with using tmap lately .. some functions seem not work . Eg. I am using tm_dots in my code but the arguments related to the title and palette are ignored. When I run this simple code

   tm_shape(EIOS_sf) +
   tm_dots(col = "disease", palette="set1", title = "Disease") +
  tm_facets("YearMonth")

I get this

── tmap v3 code detected ────────────────────────────────────────────────────────────────
[v3->v4] `tm_dots()`: migrate the argument(s) related to the scale of the visual
variable `fill` namely 'palette' (rename to 'values') to fill.scale = tm_scale(<HERE>).

When I change the argument palette to values I still have issue with the title that is ignored.

I am just wondering whether all these problems are related to the version I am using

Thanks for your help

The docs are still behind. Without being able to check it should be something like this:

tm_shape(EIOS_sf) +
  tm_dots(
      col = "disease", 
      col.scale = tm_scale(values = "set1"), 
      col.legend = tm_legend(title = "Disease")) +
   tm_facets("YearMonth")
mtennekes commented 2 weeks ago

Just to continue with the above comment, I noticed a very strange behaviour of the package If I try to plot the World shapefile from rnaturalearth the plot looks very strange. Given all these issues I do believe that there's a problem with the version I am using

World=rnaturalearth::ne_countries(scale = "medium", returnclass = "sf")
 tm_shape(World) +

tm_borders(col="black")
Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

tmap_options(check.and.fix = TRUE)
tm_shape(World) +
tm_borders(col="black")
Warning message:
The shape World is invalid. See sf::st_is_valid 

image

Check this out: https://github.com/r-tmap/tmap/issues/606#issuecomment-2344652187

Instead of using tmap_options(check.and.fix = TRUE), you can do this:

tm_shape(World) +
tm_borders(col="black") +
tm_check_fix()
anjelinejeline commented 2 weeks ago
  tm_dots(
      col = "disease", 
      col.scale = tm_scale(values = "set1"), 
      col.legend = tm_legend(title = "Disease")) +
   tm_facets("YearMonth")

@mtennekes when are the docs planned to be updated? Title is OKAY now but the colours are still ignored

mtennekes commented 2 weeks ago

Docs: don't know yet, I'm still working to make v4 as stable as possible. For the time being: there is an excellent chapter about tmap4 in https://r.geocompx.org/adv-map

Apologies, it had to be 'fill' instead of 'col'. (This is a breaking change with v3 which we had to do to make the visual variables consistent across different layers: in v4, col is only used for lines and fill for areas)

  tm_dots(
      fill = "disease", 
      fill.scale = tm_scale(values = "set1"), 
      fill.legend = tm_legend(title = "Disease")) +
   tm_facets("YearMonth")