library(tidyverse)
readme_groupings <- read.csv('text_analysis/0203_readme_merged_manifest.csv')
contrib_groupings <- read.csv('text_analysis/0203_contributing_merged_manifest.csv')
contrib_groupings$filename <- contrib_groupings$fvf_filepath
readme_groupings$filename <- readme_groupings$fvf_filepath
readme_textstat <- read.csv('text_analysis/020325_README_readability.csv')
contributing_textstat <- read.csv('text_analysis/020125_CONTRIBUTING_readability.csv')


doctypeColors <-
  setNames( c('#5da2d8', '#c7756a')
            , c("CONTRIBUTING", "README"))
readme_textstat$type = "README"
contributing_textstat$type = "CONTRIBUTING"
all_df = rbind(readme_textstat, contributing_textstat)
length_plot_all <- ggplot(all_df, aes(x=word_count, group=as.factor(type))) + 
  geom_density(aes(fill = as.factor(type)), color = NA, alpha=0.6, position="identity")+
  scale_fill_manual(values = doctypeColors) + 
  xlim(-10, 500) +
  labs(
    x = "Word Count",
    y = "Density Across Documents",
    fill="Document Type"
  ) +
  theme_bw() + 
  theme(legend.position = "top") 
length_plot_all

#ggsave(filename = "plots/cr-0203-wc-density.png", plot = length_plot_all, width = 9, height = 9, dpi = 800)

contributing_df <- inner_join(contributing_textstat, contrib_groupings, by="filename")
readme_df <- inner_join(readme_textstat, readme_groupings, by="filename")

subdirColors <-
  setNames( c('#31449c', '#4a7c85', '#c5db68')
            , c(0,1,2)  )

contributing_reading_time_plot <- ggplot(contributing_df, aes(x=reading_time, group=as.factor(ranef_grouping))) + 
  scale_fill_manual(values = subdirColors, labels=c('CI < 0', '0 in CI', '0 < CI')) + 
  geom_density(aes(fill=as.factor(ranef_grouping)), position="fill") +
  xlim(-5, 90) +
  labs(x= NULL, y= NULL, fill="RE Grouping")+ 
  theme_bw() +
  theme(legend.position = "inside", 
        legend.position.inside = c(.90, .90),
        legend.justification = c("right", "top"),
        legend.direction = "horizontal",
        legend.margin = margin(6, 6, 6, 6))
contributing_reading_time_plot

contributing_reading_ease <- ggplot(contributing_df, aes(x=flesch_reading_ease, group=as.factor(ranef_grouping))) + 
  geom_density(aes(fill=as.factor(ranef_grouping)), position="fill") +
  scale_fill_manual(values = subdirColors, labels=c('CI < 0', '0 in CI', '0 < CI')) + 
  labs(x= NULL, y="CONTRIBUTING Density", fill="RE Grouping")+ 
  xlim(-5, 90) +
  theme_bw() + 
  guides(fill="none", color="none")
#contributing_reading_ease 

readme_reading_time_plot <- ggplot(readme_df, aes(x=reading_time, group=as.factor(ranef_grouping))) + 
  geom_density(aes(fill=as.factor(ranef_grouping)), position="fill") +
  scale_fill_manual(values = subdirColors) + 
  xlim(-5, 90) +
  labs(x= "Reading Time (s)", y= NULL)+ 
  guides(fill="none", color="none")+
  theme_bw() 
#readme_reading_time_plot

readme_reading_ease <- ggplot(readme_df, aes(x=flesch_reading_ease, group=as.factor(ranef_grouping))) + 
  geom_density(aes(fill=as.factor(ranef_grouping)), position="fill") +
  scale_fill_manual(values = subdirColors) + 
  xlim(-5, 90) +
  labs(x= "Flesch Reading Ease", y= "README Density")+ 
  guides(fill="none", color="none")+
  theme_bw() 
#readme_reading_ease 
library(gridExtra)
grid.arrange(contributing_reading_ease, contributing_reading_time_plot, readme_reading_ease, readme_reading_time_plot, nrow = 2)

