library(tidyverse)
library(patchwork)#- needed to arrange multiple graphs side by side 
crackers.df <- read_csv("./data/chap5/crackers-loc.csv")
by.location <- crackers.df %>%
  group_by(location) %>%
  summarise(mx.pre=mean(x.pre))
sp <- ggplot(data=by.location,mapping=aes(x=location,y=mx.pre))
#-- Creating the initial vertical bar chart with no labels 
vbc0 <- sp +
  geom_bar(stat="identity",aes(fill=location),
           width=0.65,show.legend = FALSE) +
  scale_y_continuous(limits = c(0,35)) +
  scale_fill_manual(values=c("red","blue","yellow")) +
  labs(y="Average number pre-promotion sales\n") +
  theme_light()
#-- Creating vertical bar chart with labels
vbc <- vbc0 + 
  geom_text(aes(label=round(mx.pre,2)),vjust=-1) +
  labs(title = "(a) Vertical Bar Chart")

#-- Creating the horizontal bar chart
hbc <- vbc0 + coord_flip() + 
  geom_text(aes(label=round(mx.pre,2)),hjust=-0.5) +
  labs(title = "(b) Horizontal Bar Chart")

#- Combining the vertical and horizontal charts into one chart.
vbc + hbc
ggsave(filename="./images/chap5/bar@bylocation.pdf", width = 6.5,
       height = 3.8,units = "in")