library(dplyr)
library(ggplot2)
library(gridExtra)
library(fixest)
library(ggpubr)
options(tibble.print_max = Inf)
library(tidyr)
library(tidyverse)
library(haven)

position_dodge2(
    width = 0.3,
    preserve = c("total", "single"),
    padding = 0.3,
    reverse = FALSE
)

## Figure 2 - Releases raw
physical.raw  <- read.csv('physical_raw.csv')

fig2  <- ggplot(physical.raw, aes(age)) +
    geom_point(aes(y=count_uk, color = 'gray42'), shape=19) +
    geom_point(aes(y=countres, color = 'black'),shape = 19) +
    geom_vline(xintercept = 50, linetype="dotted") +
    labs(y="UK Releases",
         x="Age") +
    scale_colour_manual(name = '',
                        values =c('black'='black','gray42'='gray42'), labels = c('Residualized Releases','Raw Releases')) +
    scale_x_continuous(breaks = seq(0, 60, by = 5))+
    theme_bw() + theme(panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       legend.position = "bottom",
                       legend.title = element_text(size=8),
                       legend.text = element_text(size=8)
                       )
ggsave("fig2_revised_raw_physical.pdf", height = 5, width=8, units = "in", fig2)


## Figure 3 - Setlist raw
setlist.raw  <- read.csv('setlist_raw.csv')
setlist.raw
fig3  <- ggplot(setlist.raw, aes(age)) +
    geom_point(aes(y=uk_count, color = 'gray42'), shape=19) +
    geom_point(aes(y=countres, color = 'black'),shape = 19) +
    geom_vline(xintercept = 50, linetype="dotted") +
    labs(y="UK Releases",
         x="Age") +
    scale_colour_manual(name = '',
                        values =c('black'='black','gray42'='gray42'), labels = c('Residualized Releases','Raw Releases')) +
    scale_x_continuous(breaks = seq(0, 60, by = 5))+
    theme_bw() + theme(panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       legend.position = "bottom",
                       legend.title = element_text(size=8),
                       legend.text = element_text(size=8)
                       )

ggsave("fig3_revised_raw_setlist.pdf", height = 5, width=8, units = "in", fig3)


## Figure 4 - DSP raw
dsp.raw  <- read.csv("spotify_raw.csv")
dsp.raw$country = ifelse(dsp.raw$uk == "1", "UK", "US")
fig4<-ggplot(dsp.raw, aes(x=original_release_year, y=available, fill=country)) +
    geom_bar(stat="identity", position=position_dodge())+
    geom_vline(xintercept = 1963, linetype="dotted") +
    scale_fill_grey() +
    scale_x_continuous(breaks = seq(1954, 1973, by = 1))+
    labs(y="Availability",
         x="Original Release Year") +
    theme_bw() + theme(panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       legend.position = "bottom",
                       legend.title = element_text(size=8),
                       legend.text = element_text(size=8),
                                              axis.text.x = element_text(face = c('bold', 'bold', 'bold', 'bold', 'bold', 'bold', 'bold', 'bold', 'bold', 'plain','plain','plain','plain','plain','plain','plain','plain','plain','plain','plain'))
                       )

ggsave("fig4_revised_raw_dsp.pdf", height = 5, width=8, units = "in", fig4)


## Figure 5
origtrend  <- read.csv("physical_figure_origtrend.csv")
origtrend$t  <- -8:4
origtrend  <- origtrend %>% select(c(t,estimate,min95,max95)) %>% rename(origestimate = estimate,
                                                           origmin95 = min95,
                                                           origmax95 = max95)
pre63  <- read.csv("physical_figure_origxpre63.csv")
pre63$t  <- -8:4
pre63  <- pre63 %>% select(c(t,estimate,min95,max95)) %>% rename(pre63estimate = estimate,
                                                           pre63min95 = min95,
                                                           pre63max95 = max95)

graph.physical.data  <- origtrend %>% left_join(pre63, by = "t")
graph.physical.data

position_dodge2(
    width = 0.3,
    preserve = c("total", "single"),
    padding = 0.3,
    reverse = FALSE
)




p1  <- ggplot(graph.physical.data, aes(t)) +
    geom_point(aes(y=origestimate, color = 'gray42'), shape=19, position=position_nudge(x=0.1)) +
    geom_point(aes(y=pre63estimate, color = 'black'),shape = 19, position=position_nudge(x=-0.1)) +
    geom_linerange(aes(ymax=origmax95, ymin=origmin95, color = 'gray42'), position=position_nudge(x=0.1)) +
    geom_linerange(aes(ymax=pre63max95, ymin=pre63min95, color='black'), position=position_nudge(x=-0.1)) +
    labs(title="UK Releases",
         y="Releases",
         x="Years to Expiry") +
    scale_colour_manual(name = '',
                        values =c('black'='black','gray42'='gray42'), labels = c('Linear Age x pre-1963','Linear Original Release Year')) +
    scale_x_continuous(breaks = seq(-8, 4, by = 2))+
    theme_bw() + theme(plot.title = element_text(hjust = 0.5),
                       panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       legend.position = "bottom",
                       legend.title = element_text(size=8),
                       legend.text = element_text(size=8)
                       )


sl.origtrend  <- read.csv("uk_setlist_orig.csv")
sl.origtrend$t  <- -8:4
sl.origtrend  <- sl.origtrend %>%
    select(c(t,estimate,min95,max95)) %>%
    rename(origestimate = estimate,
           origmin95 = min95,
           origmax95 = max95)
sl.pre63  <- read.csv("uk_setlist_origxpre63.csv")
sl.pre63$t  <- -8:4
sl.pre63  <- sl.pre63 %>%
                     select(c(t,estimate,min95,max95)) %>%
                     rename(pre63estimate = estimate,
                            pre63min95 = min95,
                            pre63max95 = max95)
graph.setlist.data  <- sl.origtrend %>% left_join(sl.pre63, by = "t")
graph.setlist.data

p2  <- ggplot(graph.setlist.data, aes(t)) +
    geom_point(aes(y=origestimate, color = 'gray42'), shape=19, position=position_nudge(x=0.1)) +
    geom_point(aes(y=pre63estimate, color = 'black'),shape = 19, position=position_nudge(x=-0.1)) +
    geom_linerange(aes(ymax=origmax95, ymin=origmin95, color = 'gray42'), position=position_nudge(x=0.1)) +
    geom_linerange(aes(ymax=pre63max95, ymin=pre63min95, color='black'), position=position_nudge(x=-0.1)) +
    labs(title = "UK Setlists",
         y="Performances",
         x="Years to Expiry") +
    scale_x_continuous(breaks = seq(-8, 4, by = 2))+
    scale_colour_manual(name = '',
                        values =c('black'='black','gray42'='gray42'), labels = c('Linear Age x pre-1963','Linear Original Release Year')) +
    theme_bw() + theme(plot.title = element_text(hjust = 0.5),
                       panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       legend.position = "bottom",
                       legend.title = element_text(size=8),
                       legend.text = element_text(size=8)
                       )

legend <- get_legend(p2)
g1 <- grid.arrange(p1 + theme(legend.position = "none"),
                   p2 + theme(legend.position = "none"),
                   legend, ncol=2, nrow = 2,
             layout_matrix = rbind(c(1,2), c(3,3)),
             widths = c(2.7, 2.7), heights = c(2.5, 0.2))
ggsave("fig5_revised_physicalsetlist_figure.pdf", height = 5, width=8, units = "in", g1)













## Spotify & Amazon Figure

df <- read.csv("spotify_figure.csv")
df.sub <- subset(df, (df$parm != "_cons") & (df$parm != "uk"))
df.sub$year <- seq(1959,1967,by = 1)
## df.sub$year <- seq(-4,4,by = 1)
df.sub <-  df.sub[order(-df.sub$year),]
df.sub


spotify  <- ggplot(df.sub, aes(year)) +
    geom_point(aes(y=estimate), shape=18) +
    geom_linerange(aes(ymax=max95, ymin=min95)) +
    labs(y="Availability",
         x="Original Release Year",
         title="Availability on Spotify") +
    ylim(-0.25,0.25)+
    scale_x_reverse(breaks = seq(1959, 1967, by = 1)) +
    theme_bw() + theme(panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       axis.text.x = element_text(face = c('bold','bold','bold','bold','plain','plain','plain','plain','plain')))



df <- read.csv("amazon_figure.csv")
df.sub <- subset(df, (df$parm != "_cons") & (df$parm != "uk"))
df.sub$year <- seq(1959,1967,by = 1)
df.sub


amazon  <- ggplot(df.sub, aes(year)) +
    geom_point(aes(y=estimate), shape=18) +
    geom_linerange(aes(ymax=max95, ymin=min95)) +
    labs(y="Count",
         x="Original Release Year",
         title = "Availability on Amazon") +
    scale_x_reverse(breaks = seq(1959, 1967, by = 1)) +
    theme_bw() + theme(panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       axis.text.x = element_text(face = c('bold','bold','bold','bold','plain','plain','plain','plain','plain')))


g2 <- arrangeGrob(spotify, amazon, nrow = 1)
ggsave("fig6_spotifyamazon_figure.pdf", height = 4, width=8, units = "in", g2)






#########APPENDIX FIGURES#########
releasedata <- read.csv("releasecoef_reponsefig.csv")
setlistdata <- read.csv("setlistcoef_reponsefig.csv")

#FIGURE 7
ggplot(releasedata,aes(x=age)) +
    geom_point(aes(y=count_uk, colour='Raw Releases')) +
    geom_point(aes(y=countres, colour='Residualized Releases')) +
    facet_wrap(~ pre) +
    theme_bw() +
    labs(x="Age", y="UK Count") +
    scale_color_manual(values = c("gray70","black")) +
    scale_x_continuous(breaks=seq(0, 55, by = 5)) +
    theme_bw() + theme(panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       legend.position = "bottom",
                       legend.title = element_blank(),
                       legend.text = element_text(size=8)
                       )


ggsave("fig7_mean_rel_by_age.pdf", height = 5, width=8, units = "in")


#FIGURE 8
ggplot(setlistdata,aes(x=age)) +
    geom_point(aes(y=uk_count, colour='Raw Performances')) +
    geom_point(aes(y=countres, colour='Residualized Performances')) +
    facet_wrap(~ pre) +
    labs(x="Age", y="UK Count") +
    theme(legend.position="bottom",legend.title=element_blank()) +
    scale_color_manual(values = c("gray70","black")) +
    scale_x_continuous(breaks=seq(0, 55, by = 5)) +
        theme_bw() + theme(panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       legend.position = "bottom",
                       legend.title = element_blank(),
                       legend.text = element_text(size=8)
                       )



ggsave("fig8_mean_perf_by_age.pdf", height = 5, width=8, units = "in")


#######################################################################################
amazondata <- read.csv("amazon_avail_by_origyear.csv")
satclubdata <- read.csv("satclub_avail_by_origyear.csv")

#FIGURE 9
ggplot(amazondata, aes(x=original_release_year, count, fill=market)) +
    geom_bar(stat="identity", width=0.75, position='dodge') +
    labs(x="Original Release Year", y="Version Count") +
    scale_fill_manual(values=c("UK"= "black", "US"= "gray70")) +
    scale_x_continuous(breaks=seq(1954, 1973, by = 1)) +
    theme_bw() + theme(panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       legend.position = "bottom",
                       legend.title = element_blank(),
                       legend.text = element_text(size=8)
                       )


ggsave("fig9_amz_avail.pdf", height = 5, width=8, units = "in")


#FIGURE 10
ggplot(satclubdata, aes(x=original_release_year, available, fill=market)) +
    geom_bar(stat="identity", width=0.75, position='dodge') +
    labs(x="Original Release Year", y="Availability") +
    scale_fill_manual(values=c("UK"= "black", "US"= "gray70")) +
    scale_x_continuous(breaks=seq(1959, 1966, by = 1)) +
        theme_bw() + theme(panel.border = element_blank(),
                       panel.grid.major.x = element_blank(),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(colour = "black"),
                       legend.position = "bottom",
                       legend.title = element_blank(),
                       legend.text = element_text(size=8)
                       )

ggsave("fig10_satclub_avail.pdf", height = 5, width=8, units = "in")


