0

I am trying to plot parts of three countries using ggplot2 and rnaturalearth, but for some reason it is only possible two plot two at a time and sometimes ony one. I have no idea why that is happening. The code below only plots Denmark, if I change the line target <- c("Netherlands", "Germany", "Denmark") to target <- c("Germany", "Netherlands", "Denmark") it plots Denmark and the Netherlands, but not Germany. target <- c("Germany", "Denmark") gives me Germany and Denmark as desired. But as soon as I add Netherlands only Germany will be plotted. I am really confused here.

library(rnaturalearth)
library(dplyr)

target <- c("Netherlands", "Germany", "Denmark")

ne_countries(scale = 10, returnclass = 'sf') %>%
  filter(name == target) %>%
  ggplot() +
  geom_sf(fill = "#e0e8a0") +
  xlim(5,9) +
  ylim(53,56) +
  theme(panel.background = element_rect(fill = "#8088d0"),
        panel.grid = element_line(size = 0.1))

Kj Ell
  • 23
  • 4

1 Answers1

1

You are using == when it should be %in%

library(rnaturalearth)
library(dplyr)

target <- c("Netherlands", "Germany", "Denmark")

ne_countries(scale = 10, returnclass = 'sf') %>%
  filter(name %in% target) %>%
  ggplot() +
  geom_sf(fill = "#e0e8a0") +
  xlim(5,9) +
  ylim(53,56) +
  theme(panel.background = element_rect(fill = "#8088d0"),
        panel.grid = element_line(size = 0.1))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87