library(tidyverse)

#Simulate some data
set.seed(2)
Region<-factor(rep(c('A','B','C','D'),each=200))
Outcome<-sample(1:5,size=800,rep=T)
Outcome<-factor(Outcome,levels=c('1','2','3','4','5'))
reg.df = data.frame(Region,Outcome)

reg.df0<-reg.df %>%
  group_by(Outcome) %>%
  summarize(Count=n())

reg.df1<-reg.df %>%
  group_by(Region,Outcome) %>%
  summarize(Count=n())

reg.pct<-reg.df %>%
  group_by(Outcome) %>%
  count() %>%
  ungroup() %>%
  mutate(perc = `n` / sum(`n`)) %>%
  mutate(Perc = 100*perc) %>%
  arrange(perc) %>%
  mutate(labels = scales::percent(perc),Count=n)

#Pies

ggplot(data = reg.df0, aes(x = "", y = Count, fill = Outcome )) +
  geom_bar(stat = "identity", position = position_fill()) +
  geom_text(aes(label = Count), position = position_fill(vjust = 0.5)) +
  coord_polar(theta = "y",start=0, direction=-1) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text=element_blank(),
        axis.ticks=element_blank(),
        panel.grid = element_blank())

ggplot(data = reg.pct, aes(x = "", y = Count, 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) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text=element_blank(),
        axis.ticks=element_blank(),
        panel.grid = element_blank())

ggplot(data = reg.df1, aes(x = "", y = Count, group=Region, 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(~ Region, labeller=labeller(Region =~ c('Region A','Region B','Region C','Region D')))  +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text=element_blank(),
        axis.ticks=element_blank(),
        panel.grid = element_blank())

#Bars
ggplot(reg.df0, aes(x=Outcome, y=Count, fill=Outcome)) +
  geom_bar(stat="identity")+theme_minimal()+
  geom_text(aes(label = Count), vjust = 1.5, colour = "white")

ggplot(reg.pct, aes(x=Outcome, y=Perc, fill=Outcome)) +
  geom_bar(stat="identity")+theme_minimal()+
  geom_text(aes(label = Perc), vjust = 1.5, colour = "white")+
  labs(y='Percentage')

ggplot(reg.df1, aes(x=Outcome, y=Count, fill=Outcome, group=Region)) +
  geom_bar(stat="identity")+theme_minimal() +
  geom_text(aes(label = Count), vjust = 1.5, colour = "white")+
  facet_wrap(~ Region, labeller=labeller(Region =~ c('Region A','Region B','Region C','Region D')))
