---
title: "2018 Reading"
author: "Lisa Cox"
date: "March 27, 2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


First and foremost, thanks to Adam Goodkind and his blog post on plotting Goodreads data, which can be found here:
https://medium.com/@adamgreatkind/books-i-read-in-2017-1c448aa75a82

I couldn't have done this without him!

## Data Prep

Load in libraries and data

```{r include=TRUE}
library(ggplot2)
library(ggalt)
library(ggrepel)
library(jcolors)
library(dplyr)
library(tidyr)
library(stringr)

gr <- read.csv("All_Books.csv")
gr <- subset(gr, gr$Title!="")
```

### Create and Format Columns of Interest

```{r pressure, echo=FALSE}
gr$Date.Started <- as.Date(gr$Date.Started, '%m/%d/%Y')
gr$Date.Finished <- as.Date(gr$Date.Finished, '%m/%d/%Y')
gr$Year <- as.numeric(format(gr$read.date, '%Y'))
gr$read.month <- as.numeric(format(gr$read.date, '%m'))
gr$gender <- substr(gr$M.F, 1,1)
gr$poc <- substr(gr$POC.,1,1)

#Create new dataframe with only this year's data
books2018 <- subset(gr, gr$Year=="2018")

#I could have done all of these operations on the larger dataframe, gr, but I know that it's missing a lot of data for previous years, so I tried it out on 2018 first

#this was my initial stop-gap solution for shortening titles, but I figured out a better solution
#books2018$abr.title <- substr(books2018$Title,1,20)

#Here, we abbreviate the titles (abr.title), choosing only the substring that precedes a colon or a paren, then removing any whitespace. This cuts out subtitles and series names, which are often formatted like: "Wild Magic (Immortals #1)"
books2018$abr.title <- sub('\\:.*','',books2018$Title)
books2018$abr.title <- sub('\\(.*','',books2018$abr.title)
books2018$abr.title <-trimws(books2018$abr.title)

#I needed to recode my genre columns so that they'd fit the discrete color palettes. Here, I glom Star Wars books into the Sci-Fi category - I didn't really read many SW books this year anyway. For some odd reason, recoding Dystopian -> Sci-Fi made Sci-Fi come up where "Dystopian" should have in the otherwise-alphabetically-ordered legend and I couldn't figure out a fix. Any suggestions are welcome.
books2018$Genre.mod <- books2018$Genre
books2018$Genre.mod <- recode(books2018$Genre.mod,"Star wars"="Sci-fi")

#I also needed to recode my "Format" column, because I'm using archaic names from 7 years ago, and I don't think other people are interested in whether my ebooks are ones I bought or were given to me as galleys/arcs.
books2018$new.form <- books2018$Format
books2018$new.form <- recode(books2018$new.form,"eAudiobook"="Audio")
books2018$new.form <- recode(books2018$new.form,"eArc"="eBook")
books2018$new.form <- sub('\\/.*','',books2018$new.form)
books2018$new.form <- sub('\\,.*','',books2018$new.form)

#My author names were originally formatted as Last, First. This splits them into separate columns and trims the whitespace.
books2018 <-separate(books2018, Author, c("last","first"),",")
books2018$first <- trimws(books2018$firstname)
books2018$first <- sub('\\*.*','',books2018$firstname)

#Troubleshooting! Replaced some accents, changed the format of some variables.
books2018$first <- str_replace(books2018$first,"[?]","a")
books2018$last <- str_replace(books2018$last,"[?]","")
books2018$Days.to.Read <- as.numeric(books2018$Days.to.Read)
books2018$num.month <- as.numeric(books2018$read.month)
```

##Plots

### How many days does it take me to read a short book versus a long one?

```{r include=TRUE}
ggplot(data=books2018, aes(x=Days.to.Read, y=Pages)) + 
  geom_point(aes(
    color=Genre.mod, 
    shape=gender), 
    size=7) + 
  scale_color_jcolors(palette="pal7")
```

### My ratings versus Goodreads averages

```{r include=TRUE}
ggplot(data=gr, 
       aes(x=Goodreads.Avg, y=My.Rating)) + 
  geom_point(aes(color=gender)) + 
  scale_color_manual(values=c("#2B9205", "#3C0568", "#FFC300"))

ggplot(data=gr, 
       aes(x=Goodreads.Avg, y=My.Rating)) + 
  geom_point(aes(color=poc)) + 
  scale_color_manual(values=c("#2B9205", "#3C0568", "#FFC300"))
```

### Reading over the year - my magnum opus!

I had a lot of fun (and spent a ridiculous amount of time) building this graph. Many thanks to my colleague Adam Goodkind, whose blog provided a lot of guidance!

```{r include=TRUE}
ggplot(books2018, 
       aes(x=Date.Started, 
           xend=Date.Finished,
           y=reorder(abr.title,Date.Started))) +
  geom_segment(aes(x=Date.Started,
                   xend=Date.Finished,
                   y=reorder(abr.title,Date.Started),
                   yend=reorder(abr.title,Date.Started)),
               color="gray", size=4) +
  geom_point(aes(x=Date.Started,
                 y=reorder(abr.title,Date.Started),
                 fill=Genre.mod,
                 shape=new.form,
                 size=2)) +
  #Adam's plot originally used geom_dumbbell, and I worked with that for a while, but I found it was too difficult to get the points and the segments to be exactly the color combinations I wanted with dumbbell, so I moved to a combination of geom_segment and geom_point, and specified the  colors separately
  # geom_dumbbell(aes(size=0,
  #                   shape=new.form,
  #                   color=Genre.mod),
  #               size_x=2,
  #               size_xend=0,
  #               colour_x="black")+
  scale_shape_manual(values=c(21,23,24,25)) +
  scale_fill_jcolors(palette=c("pal7")) +
  scale_y_discrete("",breaks=NULL) +
  scale_x_date(date_breaks="1 month",
               date_minor_breaks = "1 day",
               date_labels="%m/%d") +
  labs(x = 'Date Started',
       y = 'abr.title',
       title = 'Books Read During 2018',
       subtitle = 'Male authors in purple, non-white authors in bold') +
  theme_minimal() +
  theme(legend.position=c(0.93,0.3),
        legend.background=element_blank(),
        legend.text=element_text(size=10)) +
  guides(size=FALSE,
         fill=guide_legend(override.aes=list(size=5,shape=21),title="Genre"), 
         shape=guide_legend(override.aes=list(size=5),title="Format")) +
  geom_text(size=3.5, fontface=ifelse(books2018$poc=="Y","bold","plain"),
                  aes(label=paste(abr.title,"by",first,last)),
                  nudge_x=.5,
                  nudge_y=-.075,
                  hjust=-.05,
                  vjust=.2,
                  color=ifelse(books2018$gender=="F",'black','#800080'))
```

### Genre by format

```{r include=TRUE}

ggplot(data=books2018, aes(x=new.form))+
  #I think the stat='count' specification is unnecessary, as geom_bar automatically assumes count
  geom_bar(aes(fill=Genre.mod),stat="count") +
  scale_fill_jcolors(palette=c("pal7")) +
  labs(x="Format") +
  guides(fill=guide_legend(title="Genre")) +
  theme_minimal()
```

### Author Demographics

```{r include=TRUE}
ggplot(data=books2018, aes(x=gender))+
  geom_bar(aes(fill=poc))+
  scale_fill_manual(values=c("#3C0568", "#FFC300")) +
  labs(x="Gender") +
  guides(fill=guide_legend(title="POC")) +
  theme_minimal()
```


### Genre by Month
```{r include=TRUE}
ggplot(data=books2018, aes(x=as.numeric(num.month)))+
  #I had to specify the width here because geom_bar sometimes tries to plot the separate fills at the column width they would be if they were plotted on their own - here, poetry only occurred in a few months, and so I was getting the plot I wanted, except that the purple corresponding to poetry was spread out along most of the x axis. Specifying column width solved that.
  geom_bar(aes(fill=Genre.mod), width=.9)+
  scale_fill_jcolors(palette=c("pal7")) +
  labs(x="Month") +
  guides(fill=guide_legend(title="Genre")) +
  theme_minimal()
```
