wesleysc352
wesleysc352

Reputation: 617

Error applying facet_wrap() in geom_sf() incomplete polygon in ggplot R

I have a shapefile with 7 regions. I have an excel file with data about reptiles in these 7 regions. I merged this shapefile with excel.

Using ggplot I tried to generate facet_wrap() from nome_popular, however the rest of the polygon parts were omitted in each facet created.

My tentative code

shapefile: https://drive.google.com/file/d/1I1m9lBX69zjsdGBg2zfpii5H4VFYE1_0/view?usp=sharing

excel:https://docs.google.com/spreadsheets/d/1eKQWWCAalehTTrUuqUlMPQnSTEZxF--g/edit?usp=sharing&ouid=118442515534677263769&rtpof=true&sd=true

# load data.frame
serpentes <- read_excel("E:/22-serpentes_cg/R/serpentes_cg_finall.xlsx")

# filer data.frame
total_especies <- serpentes %>%
  rename(regiao_cg = REGIAO_CG) %>%
  group_by(
    especie, nome_popular,
    regiao_cg
  ) %>%
  summarise(Total_esp = sum(quant))

# load shapefile
regiao <- sf::st_read("E:/22-serpentes_cg/geo/regioes_urbanas.shp") %>%
  rename(regiao_cg = REGIAO_CG)

# join shapefile and excel
total_especies_shp <- dplyr::left_join(regiao, total_especies, by = "regiao_cg")

# map facet_warp
p_total_especies_shp <- ggplot(
  na.omit(total_especies_shp),
  aes(fill = factor(Total_esp))
) +
  geom_sf() +
  scale_fill_brewer(
    palette = "Spectral", na.value = "grey", direction = -1,
    "Total de\nSepertens Regatadas"
  ) +
  facet_wrap(~nome_popular)

p_total_especies_shp

output incomplete

enter image description here

OBS EDIT

I tried @stefan's answer which partly worked, but generated a facet called "NA" bad.

new code:

p_total_especies_shp <- ggplot(total_especies_shp)+
  geom_sf(data=regiao)+
  geom_sf(aes(fill=factor(Total_esp)))+
  scale_fill_brewer(
    palette = "Spectral", na.value = "grey", direction = -1,
    "Total de\nSepertens Regatadas")+
  
  facet_wrap(~nome_popular)

p_total_especies_shp

enter image description here

Upvotes: 3

Views: 704

Answers (2)

MalditoBarbudo
MalditoBarbudo

Reputation: 2015

Working on @stefan answer, if you want to get rid of the NA panel, you need to provide the data in the second geom with the na.omit, leaving the ggplot call empty:

p_total_especies_shp <- ggplot() +
    geom_sf(data = regiao) +
    geom_sf(aes(fill = factor(Total_esp)), data = na.omit(total_especies_shp)) +
    scale_fill_brewer(
      palette = "Spectral", na.value = "grey", direction = -1,
      "Total de\nSepertens Regatadas"
    ) +
    facet_wrap(~nome_popular, drop = TRUE)
  
  p_total_especies_shp

Which gives the result you want: enter image description here

Upvotes: 1

stefan
stefan

Reputation: 124183

The issue is that with faceting the data is splitted in groups and only the polygons contained in the splitted data will show up.

If you want all regions to be shown in each facet then one option would be to add a base map via second geom_sf layer. In your case + geom_sf(regiao) + geom_sf() should do the job.

As an example I make use of the default example from ?geom_sf:

library(ggplot2)

set.seed(42)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
base <- nc
nc$facet <- sample(c("a", "b", "c", "d"), size = nrow(nc), replace = TRUE)

ggplot(nc) +
  geom_sf(data = base) +
  geom_sf(aes(fill = AREA)) +
  facet_wrap(~facet)

Upvotes: 3

Related Questions