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

How to show label = item (not count), and exclude `character(0)`, `0 (0%)`, and related strings? #58

Closed realkrantz closed 7 months ago

realkrantz commented 10 months ago

ggVennDiagram is a great work. Thanks. I would appreciate any thoughts on solving the following issues (also posted here):

# # reproducible example

library(ggVennDiagram)

set.seed(0)
small_list <- lapply(sample(0:10, size = 4), function(x){
sample(letters,x)
})

names(small_list) <- c("Mercury", "Venus", "Earth", "Mars")

# via ggVennDiagram
# how to remove "0 (0%)" from intersections without an element?

ggVennDiagram(small_list, label_alpha = 0)

ggVennDiagram(small_list, 
                category.names = LETTERS[1:4], 
                show_intersect = TRUE)                  

# via ggplot
# how to remove the "c(", ")", and quotation marks from each item label?
# how to remove "character(0)" from intersections without an element?

venn <- Venn(small_list)
data <- process_data(venn)
View(data)

ggplot() +
# 1. region count layer
geom_sf(aes(fill = count), data = venn_region(data)) +
# 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), data = venn_setlabel(data)) +
# 4. region label layer
# geom_sf_label(aes(label = count), data = venn_region(data)) +
geom_sf_label(aes(label = item), data=venn_region(data)) +
theme_void()
gaospecial commented 10 months ago

you need to transform item from a character vector to a string, maybe using paste(). For example, data$item = sapply(data$item, paste, collapse = " "), and then plot it using geom_fun().