# Load the required packages
library(ggplot2)
library(waffle)
library(dplyr)
library("RColorBrewer")
library("forcats")
library("scales")

# read in the data
l = read.csv("lyingfile.csv")


# Summarize data on who the audience was
lt = l %>% group_by(ToWhom) %>% summarise(Count = n())
lt = lt[order(lt$Count, decreasing = TRUE),]
lt$ToWhom = fct_reorder(lt$ToWhom, lt$Count, .desc = TRUE)

# summarize data on all or nothing conclusions
le = l %>% group_by(All.or.nothing.conclusion, TruthAbout) %>% summarise(Count = n())
le = le[order(le$Count, decreasing = TRUE),]
le$All.or.nothing.conclusion = fct_reorder(le$All.or.nothing.conclusion, le$Count, .desc = TRUE)

# Use my favorite color palette - use a function that will add extra colors between colors
# in palette
getPalette = colorRampPalette(brewer.pal(8, "Paired"))

# Get the number of colors I need and assign them to the right values
myColors = getPalette(14)
names(myColors) = lt$ToWhom
colScale1 = scale_fill_manual(values = myColors, name = "Audience") 

# Graph audience
# Put columns in order from most to least
ggplot(lt, aes(x = reorder(ToWhom, -Count), y = Count, fill = ToWhom)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 45,  hjust=1),
        text = element_text(size = 14, vjust = 0.5),
        plot.title = element_text(hjust = 0.5)) +
  xlab("Audience")
  ylab("Count") +
  ggtitle("Who was my audience?") +
  colScale1 # Add my 

# Use a different palette for the next graphs
getPalette = colorRampPalette(brewer.pal(8, "Accent"))

# Graph all or nothing conclusions about other people
ggplot(le[le$TruthAbout == "Other" & le$All.or.nothing.conclusion != "NA",], 
       aes(x = reorder(All.or.nothing.conclusion, -Count), 
           y = Count, fill = All.or.nothing.conclusion)) +
  geom_bar(stat = "identity") + 
  theme(text = element_text(size = 14, vjust = 0.5),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.title = element_text(hjust = 0.5)) +
  xlab("All-or-nothing conclusion")  
  ylab("Count") +
  ggtitle("All-or-nothing conclusions about the other person") +
  scale_fill_manual(values = getPalette(26), name = NULL) +
  scale_y_continuous(breaks= pretty_breaks())
  
# graph all or nothing conclusions about me
ggplot(le[le$TruthAbout == "Self" & le$All.or.nothing.conclusion != "NA",], 
       aes(x = reorder(All.or.nothing.conclusion, -Count), 
           y = Count, fill = All.or.nothing.conclusion)) +
  geom_bar(stat = "identity") + 
  theme(text = element_text(size = 14, vjust = 0.5),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.title = element_text(hjust = 0.5)) +
  xlab("All-or-nothing conclusion")  
  ylab("Count") +
  ggtitle("All-or-nothing conclusions about me") +
  # Don't repeat the colors used in the above graph, use new colors in the palette
  scale_fill_manual(values = getPalette(22)[c(10:22,5:9)], name = NULL) +
  scale_y_continuous(breaks= pretty_breaks(), limits = c(0,10))
  
# If you have any questions about my code or how I did something, please reach out to me!