---
title: "figures"
format: pdf
editor: visual
execute:
  echo: false
  warning: false
---

```{r}
library(ggplot2)
library(dplyr)
library(tidyr)
sysfonts::font_add_google("EB Garamond")
theme_set(theme_minimal(base_family = "EB Garamond"))
theme_update(text = element_text(size = 14))
library(showtext)
showtext_auto()


data = read.csv("../full_data.csv")
rubriques_lemonde = c("international","culture","politique","société","économie","sport","science/technologie","inclassable")

z = data$rubrique %in% rubriques_lemonde
data$rubrique[!z] = "inclassable"

data$sexe_prenom = data$sexe_prenom %>% recode(Femme = "Women",Homme = "Men")
data$rubrique = data$rubrique %>% recode(économie = "Economics",politique = "Politics",société = 'Society',"science/technologie" = "Science/tech",culture="Culture",international = "International")


```

```{r}
#| fig-cap: Masculinity rate of mentions and quotes in the whole corpus

d = data %>% group_by(year) %>% summarise(
  citations_men = sum(citations_men,na.rm=T),
  citations_women = sum(citations_women,na.rm=T),
  mentions_men = sum(mentions_men,na.rm=T),
  mentions_women = sum(mentions_women,na.rm=T)
) 
d$mentions_total = d$mentions_men + d$mentions_women
ggplot(d %>% filter(mentions_women > 20),aes(year,mentions_men/(mentions_men+mentions_women),linetype="Mentions")) + geom_line() + 
         geom_line(aes(year,citations_men/(citations_men+citations_women),linetype="Quotes")) +
         ylab("Feminity rate") + xlab("Date") + labs(linetype="Measure") + ylim(NA, 1)


```

```{r}
#| fig-cap: Masculinity rate of mentions per news section, excluding sports and unclassifiable


d = data %>% filter(!rubrique %in% c("inclassable","sport")) %>%
  group_by(year,rubrique) %>% summarise(
  citations_men = sum(citations_men,na.rm=T),
  citations_women = sum(citations_women,na.rm=T),
  mentions_men = sum(mentions_men,na.rm=T),
  mentions_women = sum(mentions_women,na.rm=T)
) 
d$mentions_total = d$mentions_men + d$mentions_women
ggplot(d %>% filter(mentions_women > 20),aes(year,mentions_men/(mentions_men+mentions_women),linetype="Mentions")) + geom_line() + 
         geom_line(aes(year,citations_men/(citations_men+citations_women),linetype="Quotes")) +
         ylab("Masculinity rate") + xlab("Date") + labs(linetype="Measure") + ylim(c(NA,1)) + facet_wrap(.~rubrique) + theme(
  strip.text = element_text(size = 12, family = "EB Garamond"),
  axis.text.x = element_text(size = 10)
)


```

```{r}
#| fig-cap: Masculinity rates of mentions and quotes, depending on the journalist gender. We ignore the points where there are less than 100 mentions or citations, and the 1987-1994 and 2003-2004, where the author metadata is mostly missing due to errors of digitization.
#| fig-width: 9.5
missing_years = c(1987:1994,2003:2004)

d = filter(data,sexe_prenom %in% c("Women","Men"))
d = d %>% group_by(year,sexe_prenom) %>% summarise(
  mentions_men = sum(mentions_men,na.rm=T),
  mentions_women = sum(mentions_women,na.rm=T),
  citations_men = sum(citations_men,na.rm=T),
  citations_women = sum(citations_women,na.rm=T)
) 
d$mentions_total = d$mentions_men + d$mentions_women
a = ggplot(d %>% filter(mentions_total > 100,!year %in% missing_years),aes(year,mentions_men/mentions_total,shape=sexe_prenom)) + geom_point() + ylab("Masculinity rate of mentions") + labs(shape="Journalist gender") + xlab("Date") + scale_shape_manual(values = c("Women" = 2, "Men" = 16))+ theme(legend.position = "right")


d$citations_total = d$citations_men + d$citations_women
b = ggplot(d %>% filter(citations_total > 100,!year %in% missing_years),aes(year,citations_men/citations_total,shape=sexe_prenom)) + geom_point() + ylab("Masculinity rate of quotes") + labs(shape="Journalist gender") + xlab("Date") + scale_shape_manual(values = c("Women" = 2, "Men" = 16)) + theme(legend.position = "right")


library(cowplot)
legend <- get_legend(a)
plots = plot_grid(a + theme(legend.position = "none"),b + theme(legend.position = "none"))
plot_grid(plots,legend,ncol = 2,rel_widths = c(1, 0.15))

```

```{r}
#| fig-cap: Masculinity rates of mentions, depending on the journalist gender, by news section (omitting sports and unclassifiable articles).

library(dplyr)

d = filter(data,sexe_prenom %in% c("Women","Men"),!rubrique %in% c("inclassable","sport"))
d = d %>% group_by(year,sexe_prenom,rubrique) %>% summarise(
  mentions_men = sum(mentions_men,na.rm=T),
  mentions_women = sum(mentions_women,na.rm=T),
    citations_men = sum(citations_men,na.rm=T),
  citations_women = sum(citations_women,na.rm=T)
)
d$mentions_total = d$mentions_men + d$mentions_women
d$citations_total = d$citations_men + d$citations_women

ggplot(d %>% filter(citations_women > 10),aes(year,citations_women/citations_total,shape=sexe_prenom)) + geom_point() + ylab("Masculinity rate of quotes") + facet_wrap(.~rubrique,scales="free_y") + labs(shape="Journalist gender") + theme(
  strip.text = element_text(size = 10, family = "EB Garamond"),
  axis.text.x = element_text(size = 7)) + xlab("Date") + scale_shape_manual(values = c("Women" = 2, "Men" = 16))
```

```{r}
#| fig-cap: Masculinity rates of quotes, depending on the journalist gender, by news section (omitting sports and unclassifiable articles).
ggplot(d %>% filter(mentions_women > 100),aes(year,mentions_women/mentions_total,shape=sexe_prenom)) + geom_point() + ylab("Masculinity rate of mentions") + facet_wrap(.~rubrique,scales="free_y") + labs(shape="Journalist gender") + theme(
  strip.text = element_text(size = 10, family = "EB Garamond"),
  axis.text.x = element_text(size = 7)
) + scale_shape_manual(values = c("Women" = 2, "Men" = 16)) + xlab("Date")
```

```{r}
#| fig-cap: Difference between mentions and citations of women, between women and men journalists. This Figure aims at capturing the to what extent women journalists mention and quote more women, over time.

d = filter(data,sexe_prenom %in% c("Women","Men"),!year %in% missing_years) %>% group_by(year,sexe_prenom) %>% summarise(
  mentions_men = sum(mentions_men,na.rm=T),
  mentions_women = sum(mentions_women,na.rm=T),
  citations_men = sum(citations_men,na.rm=T),
  citations_women = sum(citations_women,na.rm=T)
) 
d$mentions_total = d$mentions_men + d$mentions_women
d$citations_total = d$citations_men + d$citations_women
d$feminity_mentions = d$mentions_women/d$mentions_total
d$feminity_citations = d$citations_women/d$citations_total

#ggplot(d %>% filter(mentions_total > 200),aes(year,mentions_women/mentions_total,color=sexe_prenom)) + geom_point() + ylab("Masculinité")

d_bis = d %>% filter(citations_total > 100) %>% select(year,sexe_prenom,feminity_mentions,feminity_citations) %>% pivot_wider(names_from = sexe_prenom, values_from = c(feminity_mentions,feminity_citations), id_cols = c( year))

ggplot(d_bis,aes(year,feminity_citations_Women-feminity_citations_Men,color="Citations")) + geom_point() + geom_smooth(se=F)  + geom_point(aes(year,feminity_mentions_Women-feminity_mentions_Men,color="Mentions")) + geom_smooth(aes(year,feminity_mentions_Women-feminity_mentions_Men,color="Mentions"),se=F) + ylab("Gap between women and men journalists") + labs(color="Measure") + xlab("Date")
```

```{r}
d = filter(data,sexe_prenom %in% c("Women","Men"))


d = d %>% group_by(year,sexe_prenom) %>% summarise(
  n = n()
) 
d[d$year %in% missing_years,"n"] = NA

d_bis = d %>% pivot_wider(names_from = sexe_prenom, values_from = n)
d_bis$feminity = d_bis$Men/(d_bis$Women+d_bis$Men)

ggplot(d_bis,aes(year,feminity)) + geom_line(na.rm=T) + ylab("Masculinity of articles signatures") + labs(color="Journalist gender") +xlab("Date")
```
