Home > OS >  How to make a pieplot with overlap in R?
How to make a pieplot with overlap in R?

Time:11-09

i have a dataset with three columns, small example:

    A      B    
 1  sety   NA
 2  NA     bety
 3  NA     bety
 4  sety   bety
 5  sety   NA

how can i plot a pieplot where i have percentage of people having sety, percentage of people having bety, and percentage of people having both sety and bety, in the example above , the percents are respectively 60%?, 60%, 20%, The third percent must show as overlap between the first two.

CodePudding user response:


library(ggplot2)
library(stringr)
library(dplyr)

#initial dataset
data <- data.frame(A = c("sety", NA, NA, "sety", "sety"), B = c(NA, "bety", "bety", "bety", NA))

#combine values and remove the NA
data$C <- str_remove_all(paste0(data$A, data$B), "NA")

#get frequency of each value
freq <- data %>%
    group_by(C) %>%
    select(C) %>%
    summarize(count = n())

#plot
ggplot(freq, aes(x="", y=count, fill = C)) 
    geom_bar(stat="identity", width = 1)  
    coord_polar("y", start = 0)

enter image description here

  •  Tags:  
  • r
  • Related