VennDiagram:如何显示label = item(而不是count),并排除字符(0),0(0%)和相关字符串?

mfpqipee  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(80)
# # 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()

字符串

edqdpe6u

edqdpe6u1#

虽然我可能不完全理解你想要绘制什么样的标签,但我希望这个想法可能会有所帮助:在标签函数中使用数据参数处理:

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, group = id), data=venn_region(data) %>% 
                  rowwise %>% 
                  mutate(item = ifelse(identical(item, character(0)),
                                       NA,
                                       item %>% unlist() %>% paste(collapse = ", ")))) +
  theme_void()

字符串
所以character(0)值和c(“",“”)被删除了。我注意到了一个事实,即项目没有为所有区域分配值:

相关问题