---
title: "Exercise: Using `ggpubr` with the `ovarian` dataset"
author: "Valerio Licursi"
date: "2024-06-26"
format: html
theme: zephyr
---

#### Objective:
Learn how to visualize data and perform statistical tests using the `ggpubr` package.

#### Prerequisites:
- Basic knowledge of R and data frames
- `ggpubr` and `survival` packages installed (`install.packages("ggpubr")` and `install.packages("survival")`)

### Exercise Steps:

#### 1. Install and Load Necessary Packages
First, ensure you have the `ggpubr` and `survival` packages installed and loaded.

```{r}
# install.packages("ggpubr") # only if needed
# install.packages("survival")
library(ggpubr)
library(survival)
```

#### 2. Load the `ovarian` Dataset
Load and inspect the `ovarian` dataset.

```{r}
data(ovarian)
head(ovarian)
```

#### 3. Data Preparation
For this exercise, we will categorize the `age` variable and create a new factor variable for it.

```{r}
ovarian$age_group <- cut(ovarian$age, breaks = c(0, 50, 60, Inf), labels = c("<=50", "51-60", ">60"))
head(ovarian)
```

#### 4. Visualize the Data
Create a box plot to visualize the distribution of `futime` (survival time) across different `age_group`.

```{r}
ggboxplot(ovarian, x = "age_group", y = "futime", 
          color = "age_group", palette = "jco",
          add = "jitter") +
  stat_compare_means(method = "anova")
```

#### 5. Perform a t-test
Conduct a t-test to compare the mean survival time between two age groups (`<=50` and `>60`).

```{r}
t_test_result <- t.test(futime ~ age_group, data = ovarian, subset = age_group %in% c("<=50", ">60"))
t_test_result

# Add t-test result to the plot
ggboxplot(ovarian, x = "age_group", y = "futime", 
          color = "age_group", palette = "jco",
          add = "jitter") +
  stat_compare_means(method = "t.test", 
                     comparisons = list(c("<=50", ">60")),
                     label = "p.signif")
```

#### 6. Perform ANOVA
Conduct an ANOVA test to compare the mean survival time across all age groups.

```{r}
anova_result <- aov(futime ~ age_group, data = ovarian)
summary(anova_result)

# Add ANOVA result to the plot
ggboxplot(ovarian, x = "age_group", y = "futime", 
          color = "age_group", palette = "jco",
          add = "jitter") +
  stat_compare_means(method = "anova")
```

#### 7. Perform Tukey's HSD Test
If ANOVA shows significant differences, perform Tukey's HSD test for multiple comparisons.

```{r}
tukey_result <- TukeyHSD(anova_result)
tukey_result
```
```{r}
my_comparisons <- list( c("51-60", "<=50"), 
                        c(">60", "<=50"),
                        c(">60", "51-60")
                        )

# Display Tukey's HSD test result
ggboxplot(ovarian, x = "age_group", y = "futime", 
          color = "age_group", palette = "jco",
          add = "jitter") +
  stat_compare_means(comparisons = my_comparisons, label = "p.signif") 
```


### Additional Customizations:
- **Customize Plots**: Use different themes, colors, and labels to enhance your plots.
- **Save the Plot**: Use `ggsave()` to save your plot to a file.

```{r}
ggsave("ovarian_survival_plot.png")
```


### Using `tidyverse` for Data Transformation and Visualization

#### 1. Install and Load Necessary Packages
Ensure you have the `tidyverse`, `ggpubr`, and `survival` packages installed and loaded.

```{r}
library(tidyverse)
library(ggpubr)
library(survival)
library(ggsci) # for Scientific Journal color Palettes
```

#### 2. Load the `ovarian` Dataset
Load and inspect the `ovarian` dataset.

```{r}
data(ovarian)
head(ovarian)
```

#### 3. Data Preparation Using `tidyverse`
Use `mutate` and `cut` to categorize the `age` variable.

```{r}
ovarian <- ovarian %>%
  mutate(age_group = cut(age, breaks = c(0, 50, 60, Inf), labels = c("<=50", "51-60", ">60")))
head(ovarian)
```

#### 4. Visualize the Data with `ggpubr`
Create a box plot to visualize the distribution of `futime` (survival time) across different `age_group`.

```{r}
ovarian %>%
  ggplot(aes(x = age_group, y = futime, color = age_group)) +
  geom_boxplot() +
  geom_jitter(width = 0.2) +
  ggpubr::stat_compare_means(method = "anova") +
  scale_color_jco() +
  labs(title = "Survival Time by Age Group", x = "Age Group", y = "Survival Time (days)") +
  theme_pubr()
```

#### 5. Perform a t-test
Conduct a t-test to compare the mean survival time between two age groups (`<=50` and `>60`).

```{r}
t_test_result <- ovarian %>%
  filter(age_group %in% c("<=50", ">60")) %>%
  t.test(futime ~ age_group, data=.)
print(t_test_result)

# Add t-test result to the plot
ovarian %>%
  filter(age_group %in% c("<=50", ">60")) %>%
  ggplot(aes(x = age_group, y = futime, color = age_group)) +
  geom_boxplot() +
  geom_jitter(width = 0.2) +
  ggpubr::stat_compare_means(method = "t.test", label = "p.signif", comparisons = list(c("<=50", ">60"))) +
  scale_color_jco() +
  labs(title = "Survival Time by Age Group", x = "Age Group", y = "Survival Time (days)")
```

#### 6. Perform ANOVA
Conduct an ANOVA test to compare the mean survival time across all age groups.

```{r}
anova_result <- aov(futime ~ age_group, data = ovarian)
summary(anova_result)

# Add ANOVA result to the plot
ovarian %>%
  ggplot(aes(x = age_group, y = futime, color = age_group)) +
  geom_boxplot() +
  geom_jitter(width = 0.2) +
  ggpubr::stat_compare_means(method = "anova") +
  scale_color_jco() +
  labs(title = "Survival Time by Age Group", x = "Age Group", y = "Survival Time (days)")
```

#### 7. Perform Tukey's HSD Test
If ANOVA shows significant differences, perform Tukey's HSD test for multiple comparisons.

```{r}
tukey_result <- TukeyHSD(anova_result)
print(tukey_result)

my_comparisons <- list( c("51-60", "<=50"), 
                        c(">60", "<=50"),
                        c(">60", "51-60")
                        )

# Display Tukey's HSD test result
ovarian %>%
  ggplot(aes(x = age_group, y = futime, color = age_group)) +
  geom_boxplot() +
  geom_jitter(width = 0.2) +
  stat_compare_means(comparisons = my_comparisons, label = "p.signif") +
  scale_color_jco() +
  labs(title = "Survival Time by Age Group", x = "Age Group", y = "Survival Time (days)")
```

### Additional Customizations:
- **Customize Plots**: Use different themes, colors, and labels to enhance your plots.
- **Save the Plot**: Use `ggsave()` to save your plot to a file.

```{r}

```

### References:
- [ggpubr documentation](https://rpkgs.datanovia.com/ggpubr/)
- [tidyverse documentation](https://www.tidyverse.org/packages/)
- [survival package documentation](https://cran.r-project.org/web/packages/survival/survival.pdf)
- [R for Data Science by Hadley Wickham and Garrett Grolemund](https://r4ds.had.co.nz/)
