Search code examples
rvectorpie-chartintersection

Analyze the intersection of two numeric vectors and create pie chart


I'm having two vectors with numeric values:

v.typA <- c(1,2,110,111,20,22,46,80,89,100,210,320,999,1000)            # length = 14
v.typB <- c(1,2,10,11,20,22,40,44,60,66,80,88,100,210,320,430,540,666)  # length = 18

Now I want to create a pie chart, which visualizes the unique values and duplicates of these two vectors in percentage. I'm thinking of something like this:

enter image description here

How can I do this?


Solution

  • Do you have something like this in mind?

    library(dplyr)
    library(ggplot)
    
    v.typA <- c(1,2,110,111,20,22,46,80,89,100,210,320,999,1000)      
    v.typB <- c(1,2,10,11,20,22,40,44,60,66,80,88,100,210,320,430,540,666)
    vec <- c(v.typA,v.typB)
    
    df <- tibble(var = c("Unique", "Duplicated"),
                 val = c(length(unique(vec)), length(vec) - length(unique(vec)))) %>% 
      arrange(desc(var)) %>%
      mutate(prop = val / sum(val) *100) %>%
      mutate(ypos = cumsum(prop)- 0.5*prop )
    
    ggplot(df, aes(x="", y=prop, fill=var)) +
      geom_bar(stat="identity", width=1, color="white") +
      coord_polar("y", start=0) +
      theme_void() + 
      theme(legend.position="none") +
      geom_text(aes(y = ypos, label = paste0(var,", ", prop,"%")), color = "white", size=6) +
      scale_fill_brewer(palette="Set1")