---
title: "R_FBB_2025_Prac9.Rmd"
output: html_document
date: "2025-10-16"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


## libraries

::: font70

- library(tidyverse)

- library(corrplot)

- library(psych)

- library(ggcats)

- library(ggdogs)

- library(CatterPlots)

- library(ggimage)

- library(rsvg)

:::

```{r, echo = F, message=FALSE}
library(tidyverse)
library(corrplot)
library(psych)
library(ggcats)
library(ggdogs)
library(CatterPlots)
library(ggimage)
library(rsvg)
```

## ggcats

```{r eval = F}
install.packages("remotes")
remotes::install_github("R-CoderDotCom/ggcats@main")
library(ggcats)
```

```{r}
grid <- expand.grid(1:5, 3:1)
```

## ggcats

```{r, echo = F}
knitr::opts_chunk$set(fig.width=7, fig.height=4) 
```

::: font70

```{r}
df <- data.frame(x = grid[, 1],
                 y = grid[, 2],
                 image = c("nyancat", "bongo", "colonel", "grumpy", "hipster", "lil_bub",
                           "maru", "mouth", "pop", "pop_close",  "pusheen", "pusheen_pc",
                           "toast", "venus", "shironeko"))

ggplot(df) +
   geom_cat(aes(x, y, cat = image), size = 5) +
   geom_text(aes(x, y - 0.5, label = image), size = 2.5) +
   xlim(c(0.25, 5.5)) + ylim(c(0.25, 3.5)) + theme_bw()
```

:::

## ggcats

```{r}
iris$cat <- factor(iris$Species,
                   labels = c("pusheen", "toast", "venus"))
ggplot(iris, aes(Petal.Length, Petal.Width)) +
 geom_cat(aes(cat = cat), size = 4) + theme_bw()
```

## ggdogs

```{r, eval = F}
install.packages("remotes")
remotes::install_github("R-CoderDotCom/ggdogs@main")
library(ggdogs)
```

## ggdogs

```{r, echo = F}
knitr::opts_chunk$set(fig.width=7, fig.height=4) 
```

::: font70

```{r}
df <- data.frame(x = grid[, 1],
                 y = grid[, 2],
                 image = c("doge", "doge_strong", "chihuahua", "eyes", "gabe", "glasses",
                           "tail", "surprised", "thisisfine", "hearing", "pug", "ears",
                           "husky", "husky_2", "chilaquil"))
                           
ggplot(df) +
 geom_dog(aes(x, y, dog = image), size = 5) +
 geom_text(aes(x, y - 0.5, label = image), size = 2.5) +
 xlim(c(0.25, 5.5)) + ylim(c(0.25, 3.5)) + theme_bw()
```

::: 

## ggdogs 

```{r message=FALSE}
set.seed(1)
df <- data.frame(x = 1:10, y = rnorm(10))

ggplot(df, aes(x = x, y = y)) +
  geom_point(size = 3, color = 4) +
  geom_dog(aes(x = 7, y = -0.5), dog = "thisisfine", size = 5) +
  geom_label(aes(x = 7.75, y = -0.1, label = "This is fine")) + theme_bw()
```

## CatterPlot

```{r, eval = F}
install.packages("remotes")
remotes::install_github("Gibbsdavidl/CatterPlots")
library(CatterPlots)
```

## CatterPlot

```{r, echo = F}
knitr::opts_chunk$set(fig.width=7, fig.height=6) 
```

```{r}
x <- seq(0, 15, 0.5)
y <- sin(x)

obj <- catplot(x, y, cat = 2, catcolor = 2, type = "line")

cats(obj, 4.5, 0.8, cat = 3, catcolor = 3)
```

## Custom dots

```{r, echo = F}
knitr::opts_chunk$set(fig.width=6, fig.height=4) 
```

::: font70

```{r}
set.seed(123)

d <- data.frame(x = rnorm(10), y = rnorm(10), z=1:10,
  image = 'https://www.svgrepo.com/show/69771/dna.svg')

ggplot(d, aes(x, y)) + 
  geom_image(aes(image=image, color=z), size = 0.1) +
  scale_color_gradient(low='blue', high='red') +
  theme_bw()
```

## Загрузка данных 

```{r, echo = T, warning=FALSE, message=FALSE}
library(tidyverse)

members <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/members.csv')
expeditions <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/expeditions.csv')
peaks <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/peaks.csv')

```

```{r}
head(peaks)
colS <- colSums(is.na(peaks))
colS

colS %>% as_tibble() %>%
  mutate(colName = names(colS)) %>%
  mutate(value_pct = value / nrow(peaks)) %>%
  mutate(colName  = reorder(colName, desc(value_pct))) %>%
  ggplot() + 
  geom_col(aes(x = colName, y = value_pct), fill = 'gold', color = 'brown') + 
  ggtitle('Missing values in PEAKS dataset') + 
  xlab('Column names') + 
  ylab('Proportion of missing values') +
  theme_bw() + 
  scale_x_discrete(guide = guide_axis(n.dodge = 3)) + 
  theme(axis.text = element_text(size = 16, colour = 'black'),
        axis.title = element_text(size = 18), 
        plot.title = element_text(size = 20)) +
#        plot.title = element_text(size = 20, hjust = -0.7)) + 
  coord_flip() + 
  ylim(0, 1)
```

```{r}
head(peaks)
str(peaks)
ncol(peaks)
colS <- colSums(is.na(peaks))
colS
names(colS[colS > 0])

colS %>% as_tibble() %>%
  mutate(colName = names(colS)) %>%
  mutate(value_pct = value/nrow(peaks)) %>%
  mutate(colName = reorder(colName, value_pct)) %>%
  ggplot() + 
  geom_col(aes(x = colName, y = value_pct), fill = 'coral', color = 'black') + 
  theme_bw() +
  ggtitle("Missing values in PEAKS dataset") + 
  ylab("Proportion of missing values") + 
  xlab("Column names") +
  theme(title = element_text(size = 24),
        axis.text = element_text(size = 14, color = 'black'),
        axis.title = element_text(size = 20)) +
  #scale_x_discrete(guide = guide_axis(n.dodge = 2)) + 
  coord_flip() + 
  ylim(0, 0.6)
```

```{r}
head(iris)
```



```{r}

colS %>% as_tibble() %>%
  mutate(colName = names(colS)) %>%
  mutate(value_pct = value/nrow(peaks)) %>%
  mutate(colName = reorder(colName, value_pct)) %>%
  ggplot() + 
    geom_col(aes(x = colName, y = value_pct), fill = 'lightyellow', color = 'black') +
  #coord_flip() + 
  theme_bw() +
  ggtitle("Missing values in PEAKS dataset") + 
  ylab("Proportion of missing values") + 
  xlab("Column names") +
  theme(title = element_text(size = 24),
        axis.text = element_text(size = 14, color = 'black'),
        axis.title = element_text(size = 20)) + 
  #ylim(0, 0.6) +
  scale_x_discrete(guide = guide_axis(n.dodge = 2))
#  scale_y_continuous(expand = c(0, 0, 0.05, 0.05)) + 
  
```


```{r}
head(peaks)
peaks %>% filter(height_metres == max(height_metres))
peaks %>%
  ggplot() +
  geom_histogram(aes(x = height_metres)) +
  theme_bw() +
  annotate('curve', 
           x = 8500, y = 15,
           xend = 8780, yend = 2,
           curvature = -0.1, linewidth = 1.5,
           arrow = arrow(length = unit(3, 'mm')), color = 'red') + 
  annotate('text', x = 8450, y = 17, 
           label = 'Mount Everest', size = 10, angle = 50)
```

```{r}
d <- iris
d_bg <- iris[, -5]

ggplot(d, aes(x = Sepal.Width, fill = Species)) + 
  geom_histogram(data = d_bg, fill = 'grey', alpha = 0.5) +
  geom_histogram(color = 'black') + 
  facet_wrap(~Species) +
  guides(fill = F) +
  theme_bw()
```

```{r}
ggplot(d, aes(x = Sepal.Width, y = Sepal.Length, fill = Species)) +
  geom_point(data = d_bg, fill = "grey", alpha = .2, shape = 21, size = 4) +
  geom_point(shape = 21, size = 4, color = 'black') + 
  facet_wrap(~ Species) +
  guides(fill = FALSE) +
  theme_bw() +
  theme(axis.text = element_text(size = 14),
        axis.title.y = element_text(size = 20),
        title = element_text(size = 24),
        strip.text.x = element_text(size = 28)) + 
  xlim(0, 5)

```

```{r}
head(peaks)
filter(peaks, height_metres == max(height_metres))

peaks %>%
  ggplot() + 
  geom_histogram(aes(x = height_metres)) +
  theme_bw() +
  annotate('curve', 
           x = 8500, y = 15,
           xend = 8780, yend = 2,
           curvature = -0.3, linewidth = 1.5,
           arrow = arrow(length = unit(3, 'mm')), color = 'red') + 
  annotate('text', x = 8450, y = 17, 
           label = 'Mount Everest', size = 10, angle = 50)
```

