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

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

Open
anjelinejeline opened this issue Sep 9, 2024 · 7 comments

Comments

@anjelinejeline
Copy link

anjelinejeline commented Sep 9, 2024

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 #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
Copy link
Member

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
Copy link
Author

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
Copy link
Author

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
Copy link
Member

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
Copy link
Member

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: #606 (comment)

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

tm_shape(World) +
tm_borders(col="black") +
tm_check_fix()

@anjelinejeline
Copy link
Author

anjelinejeline commented Sep 13, 2024

  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
Copy link
Member

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")

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

2 participants