#- horizontal bar charts with proportions -
library(tidyverse)
crackers.df <- read_csv("./data/chap5/crackers-loc.csv")
crackers.df %>%
  count(location)%>%
  mutate(perc=n/nrow(crackers.df),
         size=ifelse(perc<0.3,"<30%",">=30%")) -> percent.df
ggplot(percent.df, aes(x=reorder(location,n), 
                       y=perc,fill=size)) +
  geom_bar(stat="identity",width=0.6) +
  geom_text(aes(label=round(perc,2),hjust=-0.25)) +
  scale_fill_manual(values=c("red","blue")) +
  labs(y="Percentages",x="Location") +
  theme_linedraw() -> vbar.pl
vbar.pl +
  coord_flip() +
  scale_y_continuous(limits = c(0,0.42)) +
  theme(legend.position = "top")
ggsave(filename="./images/chap5/bar@hpercent.pdf",
  width = 4.5,height = 3.8,units = "in")