trevorld / ggpattern

ggplot geoms with pattern fills
https://trevorldavis.com/R/ggpattern/dev/
Other
361 stars 18 forks source link

'Elements must equal the number of rows or 1' error #4

Closed VictimOfMaths closed 2 years ago

VictimOfMaths commented 4 years ago

When using geom_sf_pattern, more detailed shapefiles seem to throw an error.

e.g. image

Produces an adorable kitten map, but using a higher resolution map from {rnaturalearth} gives an error:

image

I've also seen this with other (quite detailed) shapefiles from elsewhere, so pretty sure it's not a {rnaturalearth] issue.

The higher resolution map does add in some additional (tiny) countries and I wondered if it was failing because they were too small to call an image for them, but even if I strip them out of the higher resolution data I get the same error.

RobertMyles commented 4 years ago

Also having this error. rmapshaper's ms_simplify() helped, but didn't get me around it. I was running into trouble plotting countries like Norway, so I guess it's detail that's the problem.

giob1994 commented 4 years ago

Sadly, I've been encountering this error while trying the "geom_sf_pattern()" example code with official geospatial data of the German federal states. That's only 16 spatial polygons of reasonably large size but with some islands too... Seems like the level of detail is the issue?

coolbutuseless commented 4 years ago

@VictimOfMaths - your example works on my machine.

Could anyone post some more examples of the issue? With code if possible.

library(ggplot2)
library(sf)
library(rgeos)
library(rnaturalearth)
library(ggpattern)

shapefile <- ne_countries(continent = 'europe', returnclass = "sf", scale = 110)

ggplot(shapefile) +
  geom_sf_pattern(aes(geometry = geometry, pattern_fill = name), pattern = "stripe") +
  # geom_sf(aes(geometry = geometry)) +
  xlim(-10, 30) +
  ylim( 37, 60) +
  theme_bw() +
  theme(legend.position = 'none')

BobBallance commented 4 years ago

This error can be triggered providing polygons that have holes. If you remove the holes using nngeo::st_remove_holes(), the error not triggered. I can't say for sure whether that's the only source of the problem, though. Here's a simple function to trigger the issue, and an attached R data file that contains a single polygon with a hole --- it's Census Tract 35006974700, from New Mexico that I happened to save.

Unzip the attachment to get the .Rdata file.

geom_with_hole.zip

library(ggplot2)
library(sf)
library(ggpattern)
library(nngeo)

#' Demonstrate the issue with ggpattern and polygons with holes.
#' @param remove.holes causes the hole to be removed
#' @param show.pattern causes geom_sf_pattern to be called.

show_pattern_issue_with_holes <- function(remove.holes=TRUE, show.pattern=TRUE) {

  # Load creates a variable 'geom_with_holes` which is US Census Tract 35006974700.
  load("geom_with_hole.Rdata")

  if (remove.holes) {
    # Use nngeo::st_remove_holes to clean up the polygons.
    geom_with_hole <- st_remove_holes(geom_with_hole)
  }

  # The base map is just the plain polygon
  p <-+ geom_sf() 

  if (show.pattern) {
    ggplot(data=geom_with_hole)  + geom_sf_pattern()
  } else {
    ggplot(data=geom_with_hole)  + geom_sf()
 }
}
kklot commented 3 years ago

I'm having this issue with South Africa, if removing the whole then we lost Lesotho. Is there any other workaround?

BobBallance commented 3 years ago

Can you put Lesotho on a separate layer that overlays the South Africa layer? If so you can remove the hole in the underlying layer.

... Bob

On Nov 4, 2020, at 09:44, Kinh Nguyen notifications@github.com wrote:

I'm having this issue with South Africa, if removing the whole then we lost Lesotho. Is there any other workaround?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

kklot commented 3 years ago

@BobBallance yes thanks, I was reducing alpha fill to show Lesotho which also work.

trevorld commented 2 years ago

Although there are still a couple of issues with R 4.1 clipping (#59, #60) to look out for geom_sf_pattern() should now support holed polygons as of {ggpattern} v0.3.1. Here is @BobBallance's example from above:

library(ggplot2)
library(sf)
library(ggpattern)

# Load creates a variable 'geom_with_holes` which is US Census Tract 35006974700.
load("geom_with_hole.RData")
options(ggpattern_use_R4.1_clipping = TRUE)
png("geom_with_hole.png", type = "cairo")
ggplot(data=geom_with_hole)  + geom_sf_pattern(fill = "lightblue")
dev.off()

geom_with_hole

Since I don't get an error I assume this bug is fixed if you have an up to date version of {sf} and {ggpattern} installed?