Is it possible to not display 0 in diagram

There is sample code below, is it possible to block "0"s from the diagram? Only need the value greater than 1 to display. Thank you.

library("ggVennDiagram") 
t1<-c(1,1,0,1,1,1,1,0,1,0) 
t2<-c(0,1,0,1,0,1,0,1,1,0) 
t3<-c(1,1,1,1,1,1,0,0,1,0) 

ggVennDiagram(apply(cbind(t1, t2, t3), 2, function(x) which(x == 1)))

Hi Jerry!

I haven't used this package yet, but it seems there is no out-of-the-box solution for what you ask.
However, there is in fact a hack-ish solution to this, using the plot object that the ggVennDiagram function returns:

library(ggVennDiagram)

t1<-c(1,1,0,1,1,1,1,0,1,0) 
t2<-c(0,1,0,1,0,1,0,1,1,0) 
t3<-c(1,1,1,1,1,1,0,0,1,0) 

plt <- ggVennDiagram(apply(cbind(t1, t2, t3), 2, function(x) which(x == 1)))

# Show the plot with label with zeros.
plt

# I found that the label data is stored in the object, so we can filter out the zeros.
remove_zero_labels <- function(layer) {
  if(layer$constructor$data == "region_label") {
    layer$data <- layer$data[layer$data$count != 0, ]
  }
  return(layer)
}

# Apply the function to the plt object (2 options)
# base R.
lapply(plt$layers, remove_zero_labels)
# Nicer approach because it does not return anything. Requires {purrr} package.
purrr::walk(plt$layers, remove_zero_labels) 

# Show the plot **without** label with zeros.
plt

Hope this helps :slight_smile:
Cheers!


This post was published by an Appsilon team member. Our company can help you get the most out of RShiny and Posit/RStudio products.

Check our open positions here.

Appsilon: Building impactful RShiny Dashboards and providing R coding services.
Appsilon_GIFsmall_whitebg

1 Like

Sounds great! Agus,
But I got an error message when run "lapply(plt$layers, remove_zero_labels)" or "purrr::walk(plt$layers, remove_zero_labels) "

Here is the error message:

lapply(plt$layers, remove_zero_labels)
Error in if (layer$constructor$data == "region_label") { :
argument is of length zero
purrr::walk(plt$layers, remove_zero_labels)
Error in if (layer$constructor$data == "region_label") { :
argument is of length zero

It is R version problem, after updated to latest version, it works now. Thank you!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.