Treatment<-factor(rep(c('A','B'),each=350))
Outcome<-c(rep(c('Success','Failure'),c(273,350-273)),
           rep(c('Success','Failure'),c(289,350-289)))
Outcome<-factor(Outcome,levels=c('Success','Failure'))
Size<-rep(rep(c('Small','Large'),4),c(81,192,6,71,234,55,36,25))
Size<-factor(Size,levels=c('Small','Large'))
kidney.df = data.frame(Treatment,Outcome,Size)

kidney.df1<-kidney.df %>%
    group_by(Treatment,Outcome) %>%
    summarize(Count=n())

kidney.df1
ggplot(data = kidney.df1, aes(x = "", y = Count, group=Treatment, fill = Outcome )) +
    geom_bar(stat = "identity", position = position_fill()) +
    geom_text(aes(label = Count), position = position_fill(vjust = 0.5)) +
    coord_polar(theta = "y") +
    facet_wrap(~ Treatment, labeller=labeller(Treatment =~ c('Treatment A','Treatment B')))  +
    theme(axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          axis.text=element_blank(),
          axis.ticks=element_blank(),
          panel.grid = element_blank()) +
    theme(legend.position='bottom') +
    guides(fill=guide_legend(nrow=1, byrow=TRUE))

kidney.pctA <- kidney.df %>%
  filter(Treatment=='A') %>%
  group_by(Treatment,Outcome) %>% # Variable to be transformed
  count() %>%
  ungroup() %>%
  mutate(perc = `n` / sum(`n`)) %>%
  arrange(perc) %>%
  mutate(labels = scales::percent(perc),Count=n)

kidney.pctB <- kidney.df %>%
  filter(Treatment=='B') %>%
  group_by(Treatment,Outcome) %>% # Variable to be transformed
  count() %>%
  ungroup() %>%
  mutate(perc = `n` / sum(`n`)) %>%
  arrange(perc) %>%
  mutate(labels = scales::percent(perc), Count=n)

kidney.pct<-bind_rows(kidney.pctA,kidney.pctB)

kidney.pct
ggplot(data = kidney.pct, aes(x = "", y = Count, group=Treatment, fill = Outcome )) +
    geom_bar(stat = "identity", position = position_fill()) +
    geom_text(aes(label = labels), position = position_fill(vjust = 0.5)) +
    coord_polar(theta = "y", start=0, direction=-1) +
    facet_wrap(~ Treatment, labeller=labeller(Treatment =~ c('Treatment A','Treatment B')))  +
    theme(axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          axis.text=element_blank(),
          axis.ticks=element_blank(),
          panel.grid = element_blank()) +
    theme(legend.position='bottom') +
    guides(fill=guide_legend(nrow=1, byrow=TRUE))

