Closed VictimOfMaths closed 2 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.
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?
@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')
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.
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()
}
}
I'm having this issue with South Africa, if removing the whole then we lost Lesotho. Is there any other workaround?
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.
@BobBallance yes thanks, I was reducing alpha
fill to show Lesotho which also work.
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()
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?
When using geom_sf_pattern, more detailed shapefiles seem to throw an error.
e.g.
Produces an adorable kitten map, but using a higher resolution map from {rnaturalearth} gives an error:
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.