---
title: "Data Analysis FAIRness of Dental Research Data"
author: 'Uribe, Sofi-Mahmudi, Raittio, Maldupa and Vilne'
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  html_document:
    toc: yes
    toc_float:
      collapsed: no
      smooth_scroll: no
    code_folding: hide
  pdf_document:
    toc: yes
---

# General options
```{r global_options, include=FALSE}
knitr::opts_chunk$set(
  fig.width = 12,
  fig.height = 8,
  fig.path = 'Figs/',
  echo = FALSE,
  warning = FALSE,
  message = FALSE,
  out.width = "75%"
)
# Use include=TRUE for the chunks to be displayed
```

# Packages

```{r}
# if not installed, install 
# install.packages("pacman")
# library(pacman)

pacman::p_load(tidyverse, # for data wrangling and visualizations 
               janitor,   # for data cleaning and tables
               here,      # for files handling
               gtsummary, # for tables
               patchwork,   # for ggplot figures
               ggeasy,    # using attr labels in ggplot
               visdat,    # for the NAs
               DataExplorer, # for EDA
               scales,    # for ggplot2 scales
               labelled,  # change labels
               dataMaid   # for the codebook
               )
```

```{r}
theme_set(theme_minimal())
```

```{r}
here::here()
```


# Datasets

```{r}
open_data_all <- read_csv(here("02-data-dental-publications-fairness-2016-2021.csv"))
```




Select some columns for the analysis

```{r}
df <- open_data_all %>%
  select(
    doi_publication,
    title,
    journalTitle,
    pubYear,
    pubType,
    isOpenAccess,
    citedByCount,
    is_open_data,
    open_data_category,
    is_open_code,
    true_open_data,
    study_type,
    dataset_type,
    FAIR_level:FsF_R1.3_02D
  )
```


Remove the open_data_all
```{r}
rm(open_data_all)
```

Store local raw copy

```{r}
# write.csv(df,here("fairness_dental_research_raw_data.csv"))
```

add index number 
```{r}
df <- df %>% 
  rowid_to_column("id")
```


```{r}
names(df)
```
## Recoding, relabelling, relevelling and cleaning

Replace all the TRUE and FALSE for YES/NO

```{r}
# convert all logical to chr

df <- df %>%
  mutate_if(is.logical, as.character) 
  
```



## Labelling variables

```{r}
labelled::var_label(df$id) <- "ID of the publication"
labelled::var_label(df$title) <- "Publication title"
labelled::var_label(df$journalTitle) <- "Journal"
labelled::var_label(df$pubYear) <- "Pub Year"
labelled::var_label(df$pubType) <- "Pub Type"
labelled::var_label(df$isOpenAccess) <- "Open access pub"
labelled::var_label(df$citedByCount) <- "Citations"
labelled::var_label(df$is_open_data) <- "Open data"
labelled::var_label(df$open_data_category) <- "Open data cat"
labelled::var_label(df$is_open_code) <- "Open code"
labelled::var_label(df$true_open_data) <- "Open data*"
labelled::var_label(df$study_type) <- "Study type"
labelled::var_label(df$dataset_type) <- "Dataset type"
labelled::var_label(df$FAIR_level) <- "FAIR Level"
labelled::var_label(df$FAIRness) <- "FAIR Score"
labelled::var_label(df$Findability) <- "Findability"
labelled::var_label(df$Accesibility) <- "Accesibility"
labelled::var_label(df$Interoperability) <- "Interoperability"
labelled::var_label(df$Reusability) <- "Reusability"
labelled::var_label(df$Findability_level) <- "Findability Level"
labelled::var_label(df$Accesibility_level) <- "Accesibility Level"
labelled::var_label(df$Interoperability_level) <- "Interoperability Level"
labelled::var_label(df$Reusability_level) <- "Reusability Level"

## FAIR Items

labelled::var_label(df$'FsF_F1_01D') <- "F1-01D - Data is assigned a globally unique identifier"
labelled::var_label(df$'FsF_F1_02D') <- "F1-02D - Data is assigned a persistent identifier"
labelled::var_label(df$'FsF_F2_01M') <- "F2-01M - Metadata includes descriptive core elements (creator title data identifier publisher summary and keywords) to support data findability"
labelled::var_label(df$'FsF_F3_01M') <- "F3-01M - Metadata includes the identifier of the data it describes"
labelled::var_label(df$'FsF_F4_01M') <- "F4-01M - Metadata is offered in such a way that it can be retrieved"
labelled::var_label(df$'FsF_A1_01M') <- "A1-01M - Metadata contains access level and access conditions of the data"
labelled::var_label(df$'FsF_A1_03D') <- "A1-03D - Data is accessible through a standardized communication protocol"
labelled::var_label(df$'FsF_A1_02M') <- "A1-02M - Metadata is accessible through a standardized communication protocol"
labelled::var_label(df$'FsF_I1_01M') <- "I1-01M - Metadata is represented using a formal knowledge representation language"
labelled::var_label(df$'FsF_I1_02M') <- "I1-02M - Metadata uses semantic resources"
labelled::var_label(df$'FsF_I3_01M') <- "I3-01M - Metadata includes links between the data and its related entities"
labelled::var_label(df$'FsF_R1_01MD') <- "R1-01MD - Metadata specifies the content of the data"
labelled::var_label(df$'FsF_R1.1_01M') <- "R1.1-01M - Metadata includes license information under which data can be reused"
labelled::var_label(df$'FsF_R1.2_01M') <- "R1.2-01M - Metadata includes provenance information about data creation or generation"
labelled::var_label(df$'FsF_R1.3_01M') <- "R1.3-01M - Metadata follows a standard recommended by the target research community of the data"
labelled::var_label(df$'FsF_R1.3_02D') <- "R1.3-02D - Data is available in a file format recommended by the target research community"
```


## Recode variables

```{r}
df <- df %>% 
  relocate(is_open_data, .after = open_data_category)

```

```{r}
df <- df %>% 
  # reshape for easy handling
  pivot_longer(cols = is_open_data:true_open_data, 
               names_to = "openess", 
               values_to = "openess_values") %>% 
  # select(openess, openess_values) %>%  # just for test
  # change the values
  mutate(openess_values = case_when(
    openess_values == "TRUE" ~ "Yes", 
    TRUE ~ "No"
  )) %>% 
  # reshape to the original form
  pivot_wider(names_from = openess, 
              values_from = openess_values)
```

Change open access values

```{r}
df <- df %>% 
  mutate(isOpenAccess = case_when(
    isOpenAccess == "Y" ~ "Yes", 
    TRUE ~ "No"
  ))
```


Relevel variables

```{r}
df <- df %>% 
  pivot_longer(ends_with("_level"), 
               names_to = "var_level", 
               values_to = "var_values") %>% 
  mutate(var_values = fct_relevel(var_values, "incomplete", "initial", "moderate", "advanced")) %>% 
  pivot_wider(names_from = var_level, 
              values_from = var_values)
```



# EDA



Check the discrete variables
```{r}
df %>% 
  select(journalTitle:FAIR_level) %>% 
   select(-c(pubType, open_data_category)) %>%  
  DataExplorer::plot_bar()
```

For the open data papers

```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  select(journalTitle:FAIR_level) %>% 
  select(-c(pubType, open_data_category)) %>% 
  DataExplorer::plot_bar()
```




```{r}
summary(df)
```



## all data

### All articles
```{r}
df %>%
  mutate(journalTitle = fct_lump_prop(journalTitle, p = 0.03,
                                      other_level = "Other")) %>%
  group_by(journalTitle, true_open_data) %>%
  count(sort = T) %>%
  pivot_wider(names_from = true_open_data,
              values_from = n)
  
```

### Articles with data

```{r}
df %>% 
  tabyl(isOpenAccess, is_open_data) %>% 
  adorn_totals(where = c("row", "col"))
```


```{r}
df %>%
  filter(true_open_data == "Yes") %>% 
  
  # Use negative values to collapse the most common
  # the 25%
  mutate(journalTitle = fct_lump(journalTitle, other_level = "Other", prop = .025)) %>%
  group_by(journalTitle) %>%
  
  count(sort = T) 
```



# General



## How many papers do we have?
```{r}
n_distinct(df$title)
```


## How many are open access and how many are not?

Total papers
```{r}
dim(df)
```

## By open access status
```{r}
df %>% 
  janitor::tabyl(isOpenAccess) %>% 
  janitor::adorn_pct_formatting() %>% 
  adorn_totals(where = "row")
```
## How many have open data according to the package?

```{r}
df %>% 
  tabyl(isOpenAccess) %>%  
  janitor::adorn_pct_formatting() %>% 
  adorn_totals(where = "row") 
  
```
## Cross tabulation open access vs open data 


```{r}
df %>% 
  filter(isOpenAccess == "No") %>% 
  tabyl(isOpenAccess, is_open_data) %>%  
  adorn_totals(where = c("row", "col")) 
```

## Open data vs true open data in open access

```{r}
df %>% 
  filter(isOpenAccess == "Yes") %>% 
  tabyl(true_open_data, is_open_data)
```
Check with https://www.medcalc.org/calc/diagnostic_test.php

## any trend about open data by year?

```{r}
df %>% 
  filter(pubYear < 2022) %>% 
  select(pubYear, true_open_data) %>% 
  group_by(pubYear, true_open_data) %>% 
  count() %>% 
  ggplot(aes(x = pubYear, 
             y = n, 
             color = true_open_data, 
             group = true_open_data)) +
  geom_line() 
```


## Hoy many journals?

```{r}
df %>%
  group_by(isOpenAccess) %>%
  summarise(count = n_distinct(journalTitle))

```



## how many journal open access y non open access

```{r}
df %>% 
  select(journalTitle, isOpenAccess) %>% 
  gtsummary::tbl_summary(by = isOpenAccess, 
                         sort = all_categorical() ~ "frequency")
  
```



## TABLE 1

```{r}
df %>% 
  filter(!is.na(is_open_data)) %>% 
  mutate(journalTitle = fct_lump_prop(journalTitle, p = .01)) %>% 
  select(journalTitle, pubYear, isOpenAccess, true_open_data) %>% 
  gtsummary::tbl_summary(by = isOpenAccess, 
                         sort = all_categorical() ~ "frequency" ) %>% 
  gtsummary::add_overall() %>% 
  gtsummary::bold_labels() 
```




## TABLE 2

```{r}
df %>%
  filter(!is.na(is_open_data)) %>%
  mutate(journalTitle = fct_lump_prop(journalTitle, p = 0.008)) %>% 
  select(journalTitle, pubYear, true_open_data) %>% 
  gtsummary::tbl_summary(by = true_open_data, 
                         sort = all_categorical() ~ "frequency")
  
```
Now select only those with opendata, to fill the Table 2

```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  group_by(journalTitle) %>% 
  count() %>% 
  arrange(desc(n))
```
```{r}
df %>% 
  filter(journalTitle == "J Clin Periodontol") %>% 
  select(journalTitle, true_open_data) %>% 
  gtsummary::tbl_summary(by = true_open_data, 
                         sort = all_categorical() ~ "frequency")
```



Check the journals without open data

```{r}
df %>% 
  tabyl(true_open_data, isOpenAccess)
```




# FAIRness

## Overall FAIRness
```{r}
summary(df$FAIRness)
```
```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  summarise(n = n(), mean = mean(FAIRness), sd = sd(FAIRness))
```
## Overall FAIRness by domain
```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  pivot_longer(Findability:Reusability, 
               names_to = "FAIR_item", 
               values_to = "FAIR_values") %>%
  mutate(FAIR_item = fct_relevel(FAIR_item, "Findable", "Accesibility", "Interoperability", "Reusability")) %>% 
  group_by(FAIR_item) %>% 
  summarise(n = n(), mean = mean(FAIR_values), sd = sd(FAIR_values))
```



```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  ggplot(aes(x = FAIRness)) +
  xlim(0, 100) +
  ylim(0, 25) +
  geom_histogram(bins = 10, color = "Grey 70", fill = "Grey 50") + 
  labs(title = "Histogram of FAIRness across dental publications", 
       y = "n")
```

## Table 3 FAIRness by journal

```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>% 
  group_by(journalTitle) %>% 
  summarise(n = n(), FAIRmean = mean(FAIRness), sd = sd(FAIRness), min = min(FAIRness), max = max(FAIRness)) %>% 
  arrange(desc(FAIRmean))
```


### Table FAIRness by year

```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>% 
  group_by(pubYear) %>% 
  summarise(n = n(), FAIRmean = mean(FAIRness), sd = sd(FAIRness), min = min(FAIRness), max = max(FAIRness)) %>% 
  arrange(pubYear)
```




## SUPP FIG FAIRness by journal

```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  ggplot(aes(x = fct_reorder(journalTitle, FAIRness), 
             color = pubYear, 
             y = FAIRness 
             )) +
  geom_jitter(alpha = .5) +
  coord_flip() +
  ylim(0, 100) +
  labs(title = "FAIRness level by journal", 
       x = "Journal", 
       y = "FAIRness level (%)", 
       color = "Year")
```
Are differences by journal or year? 

```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  mutate(Journal = fct_lump_min(journalTitle, min = 3)) %>% 
  mutate(Journal = fct_relevel(Journal, "Prog Orthod")) %>% 
  with(glm(FAIRness ~ Journal + pubYear)) %>% 
  gtsummary::tbl_regression(exponentiate = F ) %>% 
  sort_p()
```


## SUPP FIG FAIRness by year
 
```{r}
df %>%
  mutate(id = "a") %>%
  
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 4)) %>% 
  ggplot(aes(
    x = as.factor(pubYear),
    y = FAIRness,
    # color = journalTitle,
    group = id
  )) +
  
  
  geom_point(position = position_jitter(width = .4), alpha = .3) +
  
  
  stat_summary(
    fun.y = mean,
    na.rm = TRUE,
    geom = "point",
    color = "dodgerblue",
    size = 4,
    shape = "diamond"
  ) +
  
  # 95 CI
  
  stat_summary(
    geom = "pointrange",
    fun.data = mean_cl_normal,
    fun.args = list(conf.int = .95), 
    color = "dodgerblue"
  ) +
  
  
  # stat_summary(fun.data = mean_cl_normal, na.rm =TRUE,
  #             geom = "errorbar", width = .2, color = "red") +
  
  # Average
  stat_summary(
    fun.y = mean,
    na.rm = TRUE,
    aes(group = 1),
    geom = "line",
    color = "dodgerblue",
    size = .75,
    shape = "diamond"
  ) +
  
  
  labs(title = "FAIRness (%) of dental research data by year",
       subtitle = "Mean and 95% confidence interval. Each point represents one article", 
       y = "FAIRness Compliance (%)",
       color = "Journal", 
       x = "Year") +
  ylim(0, 100)
  
  

```



## With FAIRness assessment

### Publications by year by journal with open data

```{r}
df %>%
  filter(true_open_data == "Yes") %>%
  mutate(Journal = fct_lump(journalTitle, n = 6)) %>%
  group_by(Journal, pubYear) %>%
  summarise(n = n()) %>%
  ggplot(aes(
    x = pubYear,
    shape = Journal,
    color = Journal,
    group = Journal,
    y = n
  )) +
  geom_jitter(alpha = 0.9) +
  scale_shape_manual(values = 0:8) +
  scale_y_log10() + 
  labs(title = "Publications by year by journal with open data", 
       y = "n", 
       x = "Year")
#  pivot_wider(names_from = pubYear, values_from = n )
```

```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  filter(!is.na(FAIRness)) %>% 
  mutate(Journal = fct_lump(journalTitle, n = 6)) %>%
  
  group_by(Journal, pubYear) %>% 
  summarise(FAIRness_mean = mean(FAIRness, na.rm = T)) %>% 
  mutate(FAIRness_mean = round(FAIRness_mean, digits = 1) ) %>% 
  pivot_wider(names_from = pubYear, values_from = FAIRness_mean ) 
  
```

## Analysis by FAIRness items

```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  filter(!is.na(FAIRness)) %>% 
  mutate(Journal = fct_lump(journalTitle, n = 6)) %>%
  select(Journal, pubYear, Findability:Reusability ) %>% 
  
  pivot_longer(Findability:Reusability, 
               names_to = "FAIR_item", 
               values_to = "FAIR_values") %>% 
  mutate(FAIR_item = fct_relevel(FAIR_item, "Findability", 
                                 "Accesibility", 
                                 "Interoperability",
                                 "Reusability")) %>% 
  
  ggplot(aes(x = FAIR_item, 
             y = FAIR_values)) + 
  geom_boxplot(width = .5, color = "dodgerblue") +
  geom_jitter(alpha = .1, color = "Grey 20", width = 0.2) +
  labs(title = "FAIR level by domain", 
       x = "Domain", 
       y = "FAIR value")
  
```
```{r}
df %>% 
  filter(true_open_data == "Yes") %>% 
  filter(!is.na(FAIRness)) %>% 
  mutate(Journal = fct_lump(journalTitle, n = 6)) %>%
  select(Journal, pubYear, Findability:Reusability ) %>% 
  
  pivot_longer(Findability:Reusability, 
               names_to = "FAIR_item", 
               values_to = "FAIR_values") %>% 

  mutate(FAIR_item = case_when(
    FAIR_item == "Findability" ~ "F", 
    FAIR_item == "Accesibility" ~ "A", 
    FAIR_item == "Interoperability" ~ "I", 
    TRUE ~ "R"
  )) %>% 
    mutate(FAIR_item = fct_relevel(FAIR_item, "F", 
                                 "A", 
                                 "I",
                                 "R")) %>% 
  ggplot(aes(x = FAIR_item, 
             y = FAIR_values)) + 
  geom_boxplot(width = .5, color = "dodgerblue") +
  geom_jitter(alpha = .1, color = "Grey 20", width = 0.2) +
  labs(title = "FAIR level by domain and Journal", 
       x = "Domain", 
       y = "FAIR value") + 
  facet_grid(. ~ Journal)
```


## Analysis by FAIR metrics in detail

## FIG 1 

```{r}
df %>%
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>%
  select(journalTitle,
         pubYear,
         Findability_level:Reusability_level) %>%
  pivot_longer(Findability_level:Reusability_level,
               names_to = "item",
               values_to = "value") %>%
  mutate(value = fct_relevel(value, "incomplete", "initial",
                             "moderate", "advanced")) %>%
  mutate(
    item = fct_relevel(
      item,
      "Findability_level", "Accesibility_level",
      "Interoperability_level", "Reusability_level"
    )) %>% 
  mutate(item = fct_recode(item, 
                           "Findability" = "Findability_level", 
                           "Accesibility" = "Accesibility_level", 
                           "Interoperability" = "Interoperability_level", 
                           "Reusability" = "Reusability_level")) %>% 
  
  
  # table for check
  # select(item, value) %>%
  # gtsummary::tbl_summary(by = item)
  
  
  group_by(item, value)  %>%
  count() %>% 
  pivot_wider(names_from = value, 
              values_from = n)
```


```{r}
df %>%
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>%
  select(journalTitle,
         pubYear,
         Findability_level:Reusability_level) %>%
  pivot_longer(Findability_level:Reusability_level,
               names_to = "item",
               values_to = "value") %>%
  mutate(value = fct_relevel(value, "incomplete", "initial",
                             "moderate", "advanced")) %>%
  mutate(
    item = fct_relevel(
      item,
      "Findability_level",
      "Accesibility_level",
      "Interoperability_level",
      "Reusability_level"
    )
  ) %>%
  mutate(
    item = fct_recode(
      item,
      "Findability" = "Findability_level",
      "Accesibility" = "Accesibility_level",
      "Interoperability" = "Interoperability_level",
      "Reusability" = "Reusability_level"
    )
  ) %>%
  
  
  # table for check
  # select(item, value) %>%
  # gtsummary::tbl_summary(by = item)
  
  
  group_by(item, value)  %>%
  count() %>%
  ggplot(aes(
    x = item,
    y = n,
    fill = forcats::fct_rev(value)
  )) +
  # geom_col() +
  geom_bar(position = "fill", stat = "identity") +
  # geom_col(position = position_stack(reverse = TRUE)) +
  scale_x_discrete(limits = rev) +
  
  
  # scale_fill_discrete(limits = rev) +
  scale_fill_manual(values = c("#51B364", "#8ACE7E", "#FFDA66", "#E03531")) +
  
  # scale_fill_brewer(palette = "RdYiGn") +
  
  labs(title =  "Figure 1\nSummary of FAIR levels by metric",
       fill = "FAIR Level",
       x = "FAIR Metric",
       y = "") +
  coord_flip() +
  scale_y_continuous(labels = scales::percent_format())  
```

Save the plot 
```{r}
ggsave(path = here("Figs"), 
       filename = "Fig1_fair_levels_by_metric.pdf",
       width = 5, height = 5, device='pdf', dpi=600)
```


### which dataset is more FAIR? Some examples

```{r}
df %>% 
  select(doi_publication, title, journalTitle, pubYear, FAIRness, FAIR_level) %>% 
  arrange(desc(FAIRness)) %>% 
  head()
```
```{r}
df %>% 
  filter(!is.na(FAIRness)) %>% 
  select(doi_publication, title, journalTitle, pubYear, FAIRness, FAIR_level) %>% 
  arrange(desc(FAIRness)) %>% 
  tail()
```


## FIG 2 Journal and Year

### FAIR Items per journal

Note that these are the LEVEL values, not the SCORES

Convert the scores to their ordinal meanings

```{r}
p2 <- df %>%
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>% 
  pivot_longer(FsF_F1_01D:FsF_R1.3_02D,
               names_to = "item",
               values_to = "value") %>%
  mutate(item = fct_inorder(item)) %>%
  mutate(value = as.character(value)) %>% 
  mutate(value = fct_recode(value,
      "Incomplete" = "0",
      "Initial"    = "1" ,
      "Moderate"   = "2",
      "Advanced"   = "3"
    )
  ) %>% 
  mutate(value = fct_relevel(value, 
                             "Incomplete", 
                             "Initial", 
                             "Moderate", 
                             "Advanced")) %>% 
  select(journalTitle, pubYear, item, value) %>% 
  group_by(journalTitle, item, value) %>% 
  count() %>% 
  ggplot(aes(x = journalTitle, 
             y = n, 
             fill = forcats::fct_rev(value))) +
  # geom_col() +
  geom_bar(position = "fill", stat = "identity") +
  # geom_col(position = position_stack(reverse = TRUE)) +
  scale_x_discrete(limits = rev) +
  

  # scale_fill_discrete(limits = rev) +
   scale_fill_manual(values = c( "#51B364", "#8ACE7E", "#FFDA66", "#E03531" )) + 
  
   # scale_fill_brewer(palette = "RdYiGn") +

    labs(title = "Summary of FAIR levels by Journal", 
      fill = "FAIR Level",
       x = "", 
       y = "") +
  coord_flip() + 
  # facet_grid(. ~ item ) +
  
  scale_y_continuous(labels = scales::percent_format())  
```
### FAIR Items per year

```{r}
p1 <- df %>%
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>% 
  pivot_longer(FsF_F1_01D:FsF_R1.3_02D,
               names_to = "item",
               values_to = "value") %>%
  mutate(item = fct_inorder(item)) %>%
  mutate(value = as.character(value)) %>% 
  mutate(value = fct_recode(value,
      "Incomplete" = "0",
      "Initial"    = "1" ,
      "Moderate"   = "2",
      "Advanced"   = "3"
    )
  ) %>% 
  mutate(value = fct_relevel(value, 
                             "Incomplete", 
                             "Initial", 
                             "Moderate", 
                             "Advanced")) %>% 
  select(journalTitle, pubYear, item, value) %>% 
  group_by(pubYear, item, value) %>% 
  count() %>% 
  ggplot(aes(x = as.factor(pubYear), 
             y = n, 
             fill = forcats::fct_rev(value))) +
  # geom_col() +
  geom_bar(position = "fill", stat = "identity") +
  # geom_col(position = position_stack(reverse = TRUE)) +
  scale_x_discrete(limits = rev) +
  

  # scale_fill_discrete(limits = rev) +
   scale_fill_manual(values = c( "#51B364", "#8ACE7E", "#FFDA66", "#E03531" )) + 
  
   # scale_fill_brewer(palette = "RdYiGn") +

    labs(title = "Summary of FAIR levels by Year", 
      fill = "FAIR Level",
       x = "Year", 
       y = "") +
  # coord_flip() + 
  # facet_grid(. ~ item ) +
  theme(legend.position = "none") +
  scale_y_continuous(labels = scales::percent_format()) 
```



```{r}
p1 + p2 + plot_annotation(tag_levels = 'A') + plot_annotation(title = "Figure 2")
```
### ggasve fig 2 

```{r}
ggsave(path = here("Figs"), 
       filename = "Fig2_year_journal.pdf",
       width = 9, height = 4, device='pdf', dpi=600)
```

```{r}
df %>%
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>% 
  pivot_longer(FsF_F1_01D:FsF_R1.3_02D,
               names_to = "item",
               values_to = "value") %>%
  mutate(item = fct_inorder(item)) %>%
  mutate(value = as.character(value)) %>% 
  mutate(value = fct_recode(value,
      "Incomplete" = "0",
      "Initial"    = "1" ,
      "Moderate"   = "2",
      "Advanced"   = "3"
    )
  ) %>% 
  mutate(value = fct_relevel(value, 
                             "Incomplete", 
                             "Initial", 
                             "Moderate", 
                             "Advanced")) %>% 
  select(journalTitle, pubYear, item, value) %>% 
  group_by(pubYear, item, value) %>% 
  count() %>% 
  ggplot(aes(x = as.factor(pubYear), 
             y = n, 
             fill = forcats::fct_rev(value))) +
  # geom_col() +
  geom_bar(position = "fill", stat = "identity") +
  # geom_col(position = position_stack(reverse = TRUE)) +
  scale_x_discrete(limits = rev) +
  

  # scale_fill_discrete(limits = rev) +
   scale_fill_manual(values = c( "#51B364", "#8ACE7E", "#FFDA66", "#E03531" )) + 
  
   # scale_fill_brewer(palette = "RdYiGn") +

    labs(title = "Summary of FAIR levels by Year", 
      fill = "FAIR Level",
       x = "Year", 
       y = "") +
  # coord_flip() + 
  # facet_grid(. ~ item ) +
  scale_y_continuous(labels = scales::percent_format())
```


## FIG 3 FAIR Maturity details

```{r}
p3 <- df %>%
  # reoirder and relabel the data
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>%
  pivot_longer(FsF_F1_01D:FsF_R1.3_02D,
               names_to = "item",
               values_to = "value") %>%
  mutate(item = fct_inorder(item)) %>%
  mutate(value = as.character(value)) %>%
  mutate(value = fct_recode(
    value,
    "Incomplete" = "0",
    "Initial"    = "1" ,
    "Moderate"   = "2",
    "Advanced"   = "3"
  )) %>%
  mutate(value = fct_relevel(value,
                             "Incomplete",
                             "Initial",
                             "Moderate",
                             "Advanced"))  %>%
  #########################
# relevel
mutate(
  item = fct_recode(
    item,
    "F1-01D - Data is assigned a globally unique identifier" = "FsF_F1_01D"	,
    "F1-02D - Data is assigned a persistent identifier" = "FsF_F1_02D"	,
    "F2-01M - Metadata includes descriptive core elements to support data findability" = "FsF_F2_01M"	,
    "F3-01M - Metadata includes the identifier of the data it describes" = "FsF_F3_01M"	,
    "F4-01M - Metadata is offered in such a way that it can be retrieved by machines" = "FsF_F4_01M"	,
    "A1-01M - Metadata contains access level and access conditions of the data" = "FsF_A1_01M"	,
    "A1-02M - Metadata is accessible through a standardized communication protocol" = "FsF_A1_03D"	,
    "A1-03D - Data is accessible through a standardized communication protocol" = "FsF_A1_02M"	,
    "I1-01M - Metadata is represented using a formal knowledge representation language" = "FsF_I1_01M"	,
    "I1-02M - Metadata uses semantic resources" = "FsF_I1_02M"	,
    "I3-01M - Metadata includes links between the data and its related entities" = "FsF_I3_01M"	,
    "R1-01MD - Metadata specifies the content of the data" = "FsF_R1_01MD"	,
    "R1.1-01M - Metadata includes license information under which data can be reused" = "FsF_R1.1_01M"	,
    "R1.2-01M - Metadata includes provenance information about data creation or generation" = "FsF_R1.2_01M"	,
    "R1.3-01M - Metadata follows a standard recommended by the target research community of the data" = "FsF_R1.3_01M"	,
    "R1.3-02D - Data is available in a file format recommended by the target research community" = "FsF_R1.3_02D"
  )
) %>%
  
  
  
  #########################
select(item, value) %>%
  group_by(item, value) %>%
  count() %>%
  
  
  
  ## The plot
  
  ggplot(aes(
    x = item,
    y = n,
    fill = forcats::fct_rev(value)
  )) +
  # geom_col() +
  geom_bar(position = "fill", stat = "identity") +
  # geom_col(position = position_stack(reverse = TRUE)) +
  scale_x_discrete(limits = rev) +
  
  
  # scale_fill_discrete(limits = rev) +
  scale_fill_manual(values = c("#51B364", "#8ACE7E", "#FFDA66", "#E03531")) +
  
  # scale_fill_brewer(palette = "RdYiGn") +
  
  labs(title = "FAIR maturity indicators",
       # fill = "FAIR Level",
       x = "FAIR Maturity Indicator",
       y = "") +
  # coord_flip() +
  # facet_grid(. ~ item ) +
  coord_flip() +
  theme(legend.position = "none") +
  scale_y_continuous(labels = scales::percent_format())
 
```

```{r}
p3
```

### ggsave fig 3

```{r}
ggsave(path = here("Figs"), 
       filename = "Fig3_fair_maturity.pdf",
       width = 10, height = 5, device='pdf', dpi=300)
```

```{r}
patch <- p1 | p2
```


```{r}
patch / p3 + plot_annotation(tag_levels = 'A')
```



```{r}
ggpubr::ggarrange(p1,                                                 # First row with scatter plot
          ggpubr::ggarrange(p2, p3, ncol = 2, labels = c("B", "C")), # Second row with box and dot plots
          nrow = 2, 
          labels = "A"                                        # Labels of the scatter plot
          ) 
          
```
```{r}
cowplot::ggdraw() +
  cowplot::draw_plot(p1, x = 0, y = .5, width = .5, height = .5) +
  cowplot::draw_plot(p2, x = .5, y = .5, width = .5, height = .5) +
  cowplot::draw_plot(p3, x = 0, y = 0, width = 1, height = 0.5) +
  cowplot::draw_plot_label(label = c("A", "B", "C"), size = 15,
                  x = c(0, 0.5, 0), y = c(1, 1, 0.5))
```

```{r}
ggsave(path = here("Figs"), 
       filename = "Fig_2_A_B_C.pdf",
       width = 15, height = 7, device='pdf', dpi=600)
```

Remove graphs

```{r}
rm(p1, p2, p3, patch)
```


## Tables to check


```{r}
df %>%
  # reoirder and relabel the data
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>%
  pivot_longer(FsF_F1_01D:FsF_R1.3_02D,
               names_to = "item",
               values_to = "value") %>%
  mutate(item = fct_inorder(item)) %>%
  mutate(value = as.character(value)) %>%
  mutate(value = fct_recode(
    value,
    "Incomplete" = "0",
    "Initial"    = "1" ,
    "Moderate"   = "2",
    "Advanced"   = "3"
  )) %>%
  mutate(value = fct_relevel(value,
                             "Incomplete",
                             "Initial",
                             "Moderate",
                             "Advanced"))  %>%
  #########################
## recode for the table

mutate(value = fct_collapse(value, 
                            adv_mod = c("Advanced", "Moderate"), 
                            low_inc = "Init_inc")) %>% 



# relevel
mutate(
  item = fct_recode(
    item,
    "F1-01D - Data is assigned a globally unique identifier" = "FsF_F1_01D"	,
    "F1-02D - Data is assigned a persistent identifier" = "FsF_F1_02D"	,
    "F2-01M - Metadata includes descriptive core elements to support data findability" = "FsF_F2_01M"	,
    "F3-01M - Metadata includes the identifier of the data it describes" = "FsF_F3_01M"	,
    "F4-01M - Metadata is offered in such a way that it can be retrieved by machines" = "FsF_F4_01M"	,
    "A1-01M - Metadata contains access level and access conditions of the data" = "FsF_A1_01M"	,
    "A1-02M - Metadata is accessible through a standardized communication protocol" = "FsF_A1_03D"	,
    "A1-03D - Data is accessible through a standardized communication protocol" = "FsF_A1_02M"	,
    "I1-01M - Metadata is represented using a formal knowledge representation language" = "FsF_I1_01M"	,
    "I1-02M - Metadata uses semantic resources" = "FsF_I1_02M"	,
    "I3-01M - Metadata includes links between the data and its related entities" = "FsF_I3_01M"	,
    "R1-01MD - Metadata specifies the content of the data" = "FsF_R1_01MD"	,
    "R1.1-01M - Metadata includes license information under which data can be reused" = "FsF_R1.1_01M"	,
    "R1.2-01M - Metadata includes provenance information about data creation or generation" = "FsF_R1.2_01M"	,
    "R1.3-01M - Metadata follows a standard recommended by the target research community of the data" = "FsF_R1.3_01M"	,
    "R1.3-02D - Data is available in a file format recommended by the target research community" = "FsF_R1.3_02D"
  )
) %>%
  
  
  
  #########################
select(item, value) %>%
  group_by(item, value) %>%
  count()  %>% 
  mutate(n = n/112*100) %>% 
  pivot_wider(names_from = value, 
              values_from = n) 
  

```



### levels by journal

```{r}
df %>% 
  filter(!is.na(is_open_data)) %>%
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>%
  select(journalTitle, 
         pubYear, 
         FsF_F1_01D:FsF_R1.3_02D) %>% 
  pivot_longer(FsF_F1_01D:FsF_R1.3_02D, 
               names_to = "item", 
               values_to = "value") %>% 
  mutate(item = fct_inorder(item))  %>% 
  group_by(journalTitle, item) %>% 
  summarise(mean = mean(value)) %>% 
  pivot_wider(names_from = item, 
              values_from = mean)
  
```
```{r}
df %>% 
  filter(!is.na(is_open_data)) %>%
  filter(true_open_data == "Yes") %>%
  mutate(journalTitle = fct_lump_min(journalTitle, min = 3)) %>%
  select(journalTitle, 
         pubYear, 
         FsF_F1_01D:FsF_R1.3_02D) %>% 
  pivot_longer(FsF_F1_01D:FsF_R1.3_02D, 
               names_to = "item", 
               values_to = "value") %>% 
  mutate(item = fct_inorder(item))  %>% 
  group_by(pubYear, item) %>% 
  summarise(mean = mean(value)) %>% 
  pivot_wider(names_from = item, 
              values_from = mean)
```


## Extras

## Open access by year

```{r}
df %>% 
  filter(pubYear < 2022) %>% 
  group_by(pubYear, isOpenAccess) %>% 
  count() %>% 
  ggplot(aes(x = pubYear,
             group = isOpenAccess, 
             color = isOpenAccess, 
             y = n)) + 
  geom_line() + 
  scale_color_manual(values=c('red','green'))
```
### open data by year

```{r}
df %>% 
  filter(pubYear < 2022) %>% 
  group_by(pubYear, true_open_data) %>% 
  count() %>% 
  ggplot(aes(x = pubYear,
             group = true_open_data, 
             color = true_open_data, 
             y = n)) + 
  geom_line() + 
  scale_color_manual(values=c('red','green'))
```

### Open access has more citations per year?

```{r}
df %>% 
  group_by(pubYear, isOpenAccess) %>% 
  summarise(mean = mean(citedByCount)) %>%  
  ggplot(aes(x = pubYear, 
             y = mean, 
             group = isOpenAccess,  
             color = isOpenAccess)) + 
  geom_line()
```



### Open data has more citations per year?
```{r}
df %>% 
  group_by(pubYear, true_open_data) %>% 
  summarise(mean = mean(citedByCount)) %>%  
  ggplot(aes(x = pubYear, 
             y = mean, 
             group = true_open_data,  
             color = true_open_data)) + 
  geom_line()
```
Let's check

```{r}
glm(citedByCount ~  pubYear +  true_open_data + isOpenAccess + true_open_data * isOpenAccess, 
    data = df) %>% 
  gtsummary::tbl_regression(exponentiate = T)
```



# Codebook

(run only once at the end of the analysis)
```{r}
# df %>% 
#  dataMaid::makeCodebook(file = "codebook.html")


```

```{r}
 sessionInfo() 
```


