
# import the required packages
library("dplyr")
library("ggplot2")
library("RColorBrewer")
library("dichromat")
library("scales")

# read in the data
a = read.csv("thedataset.csv")

# colorRampPalette can expand Color Brewer palettes
getPalette = colorRampPalette(brewer.pal(6, "Paired"))

# get the count of apologies by setting
g2 = a %>% group_by(Setting) %>%
  summarize(count = n())

# Pie chart of # of apologies by setting
ggplot(g2, aes(x="", y=count, fill=Setting)) +
  geom_bar(stat="identity", width=1, color="white") +
  coord_polar("y", start=0) + # makes this a pie chart
  theme_void() + 
  ggtitle("Setting of apology") +
  theme(plot.title = element_text(hjust = 0.5), # center title
        text = element_text(size = 12)) + # make the text large enough to read
  scale_fill_manual(values = getPalette(nrow(g2)), name = NULL)

# Choosing the # of colors in "Paired" to include based on what looks good
# Then getPalette expands the palette as specified when it is called
getPalette = colorRampPalette(brewer.pal(8, "Paired"))

# Now pie chart for the apology audience
g1 = a %>% group_by(ToWhom) %>%
  summarize(count = n())

ggplot(g1, aes(x="", y=count, fill=ToWhom)) +
  geom_bar(stat="identity", width=1, color="white") +
  coord_polar("y", start=0) +
  theme_void() +
  ggtitle("Who I was apologizing to") +
  theme(plot.title = element_text(hjust = 0.5),
        text = element_text(size = 12)) +
  # getPalette expands the palette to have a distinct color for each value
  scale_fill_manual(values = getPalette(nrow(g1)), name = NULL)

# Now pie chart for the reasons for apologizing
getPalette = colorRampPalette(brewer.pal(10, "Paired"))

g3 = a %>% group_by(ForWhat) %>%
  summarize(count = n())

ggplot(g3, aes(x="", y=count, fill=ForWhat)) +
  geom_bar(stat="identity", width=1, color="white") +
  coord_polar("y", start=0) +
  theme_void() +
  ggtitle("What I apologized for") +
  theme(plot.title = element_text(hjust = 0.5),
        text = element_text(size = 12)) +
  scale_fill_manual(values = getPalette(nrow(g3)), name = NULL)

# Now make a stacked bar chart showing what apologized for and setting
getPalette = colorRampPalette(brewer.pal(10, "Paired"))

g5 = a %>% group_by(ForWhat, Setting) %>%
  summarize(count = n())

ggplot(g5, aes(fill=Setting, y=count, x=ForWhat)) + 
  geom_bar(position="stack", stat="identity") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_fill_manual(values = getPalette(10), name = "Where?") +
  theme(axis.text.x = element_text(angle = 45,  hjust=1),
        text = element_text(size = 12)) +
  xlab("What was I apologizing for?")  + # vjust = 0.5
  ylab("Count") +
  ggtitle("What I apologized for and where") +
  theme(plot.title = element_text(hjust = 0.5))

# Now make a stacked bar chart showing what apologized for and to whom
g6 = a %>% group_by(ForWhat, ToWhom) %>%
  summarize(count = n())

getPalette = colorRampPalette(brewer.pal(11, "Spectral"))

ggplot(g6, aes(fill=ToWhom, y=count, x=ForWhat)) + 
  geom_bar(position="stack", stat="identity") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_fill_manual(values = getPalette(10), name = "To who?") +
  theme(axis.text.x = element_text(angle = 45,  hjust=1),
        text = element_text(size = 12)) +
  xlab("What was I apologizing for?")  + # vjust = 0.5
  ylab("Count") +
  ggtitle("What I apologized for and who I apologized to")

# now create the data set to graph preventability, harm, and would change
e = as.data.frame(matrix(nrow = 5, ncol = 4))

colnames(e) = c("Label","Preventability","Harm","Change")
e[1,] = c("A - roommate",0.95, 0.22, "No")
e[2,] = c("B - car door",0.8, 0.6, "Yes")
e[3,] = c("C - truck noise",0.05, 0.2, "No")
e[4,] = c("D - bookstore",0.6, 0.05, "No")
e[5,] = c("E - coworker",0.95, 0.35, "Yes")
e[6,] = c("F - dishes",0.95, 0.1, "Maybe")

# change types of variables so they're plotted correctly
e$Preventability = as.numeric(as.character(e$Preventability))
e$Harm = as.numeric(as.character(e$Harm))
e$Change = factor(e$Change, levels = c("Yes","Maybe","No"))

# create the plot
ggplot(e, aes(x = Preventability, y = Harm, color = Change, label = Label)) +
  geom_point(size = 4) +
  theme_classic() +
  # axis go from approximately 0 to 1 (x-axis needs room for labels)
  xlim(c(0,1.2)) +
  ylim(c(0,1)) +
  # manually change colors of would change and label legend more clearly
  scale_color_manual(values = c("#4275f5","#c544fc","#fc6044"), name = "Would change?") +  
  theme(axis.text.x=element_blank(), #remove x axis labels
        axis.ticks.x=element_blank(), #remove x axis ticks
        axis.text.y=element_blank(),  #remove y axis labels
        axis.ticks.y=element_blank(),#remove y axis ticks
        text = element_text(size = 10)) + 
  geom_text(hjust=-.1, vjust=0) # move labels slightly to the right so they're next to points