Home > database >  R mapdata seems strange for the Netherlands
R mapdata seems strange for the Netherlands

Time:11-26

I noticed that there are several polygons overlaid when I plot the Netherlands map using ggplot and mapdata in R. I attached the plot. Could anyone help me understand why this happens? Is it possible to have only one layer? I attached the code.

Thanks very much!

we=map_data(map = "world", region = c("Netherlands"))
ggplot() 
  geom_polygon(data =we, aes(x=long, y=lat, group=group), fill = "red", color = "white",size=0.2,alpha=0.4) 
  theme(
    plot.margin = unit(c(0,0,0,0), "cm"),
    legend.title = element_blank(),
    legend.margin=margin(),
    legend.position = "bottom",
    strip.background = element_blank(),
    panel.background = element_rect(fill = "white",
                                colour = "white",
                                size= 0.5),
    panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    axis.ticks.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank(),
    axis.title = element_blank())

enter image description here

CodePudding user response:

If you want to have one polygon of the Netherlands you could remove IJsselmeer, which is a lake, and Zuid-Flevoland which is an artificial island. Is that what you mean and want by one polygon? You could use the following code:

library(ggplot2)
library(ggmap)
library(dplyr)
we=map_data(map = "world", region = c("Netherlands"))

# filter region
we_filtered <- we %>%
  filter(!subregion %in% c('IJsselmeer', 'Zuid-Flevoland'))

ggplot() 
  geom_polygon(data =we_filtered, aes(x=long, y=lat, group=group), fill = "red", color = "white",size=0.2,alpha=0.4) 
  theme(
    plot.margin = unit(c(0,0,0,0), "cm"),
    legend.title = element_blank(),
    legend.margin=margin(),
    legend.position = "bottom",
    strip.background = element_blank(),
    panel.background = element_rect(fill = "white",
                                    colour = "white",
                                    size= 0.5),
    panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    axis.ticks.x = element_blank(),
    axis.text.x = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank(),
    axis.title = element_blank())

Created on 2022-11-25 with reprex v2.0.2

CodePudding user response:

You're dealing with a polygon with holes. If you use the subgroup aesthetic instead of the group aesthetic, you can display this due to even-odd being the default polygon filling rules. Theme omitted for brevity:

library(ggplot2)
we=map_data(map = "world", region = c("Netherlands"))
ggplot() 
  geom_polygon(data =we, aes(x=long, y=lat, subgroup=group), 
               fill = "red", color = "white",size=0.2,alpha=0.4)

Created on 2022-11-25 by the reprex package (v2.0.0)

  • Related