gaospecial / ggVennDiagram

A 'ggplot2' implement of Venn Diagram.
https://gaospecial.github.io/ggVennDiagram/
GNU General Public License v3.0
282 stars 38 forks source link

Missing aesthetic after updating sf package #65

Closed colebaril closed 8 months ago

colebaril commented 8 months ago

Hello,

After updating the sf package, I now get the following error:

Error in `geom_sf()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `compute_layer()`:
! `stat_sf()` requires the following missing aesthetics: geometry

Here is what my venn_region(data) looks like:

> venn_region(data)
# A tibble: 31 × 4
   id    name                 item       count
   <chr> <chr>                <list>     <int>
 1 1     Aedes                <chr [15]>    15
 2 2     Culex                <chr [20]>    20
 3 3     Coquillettidia       <chr [3]>      3
 4 4     Anopheles            <chr [1]>      1
 5 5     Ochlerotatus         <chr [4]>      4
 6 1/2   Aedes/Culex          <chr [5]>      5
 7 1/3   Aedes/Coquillettidia <chr [0]>      0
 8 1/4   Aedes/Anopheles      <chr [0]>      0
 9 1/5   Aedes/Ochlerotatus   <chr [6]>      6
10 2/3   Culex/Coquillettidia <chr [3]>      3
# ℹ 21 more rows
# ℹ Use `print(n = ...)` to see more rows

This is what the venn diagram looked like prior to the issue:

virus_venn_diagram

Here is my code:

venn <- virus_master_2023 %>% 
  separate_wider_delim(cols = mosquito_species, names = c("genus", "species"), delim = " ") %>% 
  distinct(virus_name, genus, .keep_all = TRUE) %>% 
  group_by(virus_name, genus) %>% 
  summarise(n = n()) %>% 
  ungroup() %>% 
  complete(virus_name, genus, fill = list(n = 0)) %>% 
  pivot_wider(names_from = "genus", values_from = "n") %>% 
  mutate(across(c("Aedes", "Anopheles", "Coquillettidia", "Culex", "Ochlerotatus"),
                ~ as.character(.))) %>%

  mutate(across(c("Aedes", "Anopheles", "Coquillettidia", "Culex", "Ochlerotatus"), 
                ~ case_when(. == "1" ~ virus_name,
                            TRUE ~ NA_character_))) %>% 
  select(-1)

aedes_venn <- venn %>%
  select("Aedes") %>%
  drop_na()

culex_venn <- venn %>%
  select("Culex") %>%
  drop_na()

coquillettidia_venn <- venn %>%
  select("Coquillettidia") %>%
  drop_na()

anopheles_venn <- venn %>%
  select("Anopheles") %>%
  drop_na()

ochlerotatus_venn <- venn %>%
  select("Ochlerotatus") %>%
  drop_na()

venn_map <- map(c(aedes_venn, culex_venn, coquillettidia_venn, anopheles_venn, ochlerotatus_venn), unique)

venn_data <- Venn(venn_map)

data <- process_data(venn_data) 

venn_diagram <- ggplot() +
  geom_sf(data = venn_region(data), aes(fill = count)) +
  # 2. set edge layer
  geom_sf(aes(color = id), data = venn_setedge(data), show.legend = FALSE) +
  # 3. set label layer
  geom_sf_text(aes(label = name), fontface = "bold.italic", data = venn_setlabel(data)) +
  # 4. region label layer
  geom_sf_label(aes(label = count), data = venn_region(data)) +
  theme_void() +
  scale_fill_viridis_c("Number of Viruses")
colebaril commented 8 months ago

I have determined that this is a bug with the sf package where what should be spatial sf data is converted into a tibble instead, which is why geom_sf throws the aforementioned error.

https://github.com/tidyverse/ggplot2/issues/3936

Looking into ways that the data can be converted from a tibble to sf data.

gaospecial commented 8 months ago

@colebaril New version of ggVennDiagram no longer depend on sf package. Please refer to https://gaospecial.github.io/ggVennDiagram/articles/fully-customed.html for customizations.

See why

colebaril commented 8 months ago

@colebaril New version of ggVennDiagram no longer depend on sf package. Please refer to https://gaospecial.github.io/ggVennDiagram/articles/fully-customed.html for customizations.

See why

Thank you! I will try this when I'm home, I'm sure it will work. Also, great work in reducing dependencies for the package.