Skip to content

Commit

Permalink
[add]コースのテーマカラーに従って作図のカラーテーマも変更
Browse files Browse the repository at this point in the history
  • Loading branch information
uribo committed Jul 7, 2022
1 parent 4a9b427 commit 43d20cb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
3 changes: 2 additions & 1 deletion intro.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ p +
#| fig-cap: ペンギンの体の大きさの関係を種名別に比較する
#| warning: false
#| dev: ragg_png
source("scripts/color_palette.R")
p +
geom_point(aes(color = species)) +
scale_color_discrete(name = "種名",
scale_colour_tokupon(name = "種名",
labels = c("アデリーペンギン", "ヒゲペンギン", "ジェンツーペンギン"))
```

Expand Down
21 changes: 20 additions & 1 deletion scripts/color_palette.R
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
scales::show_col(colours = c("#030A10", "#FF9400", "#0072B2", "#43b284", "#dd5129", "#8C8984"))
course_colors <- c("#FF9400", "#0072B2", "#43b284", "#dd5129", "#030A10", "#8C8984", "gray80")

theme_tokupon_course <- function() {
course_theme <- ggplot2::theme()
course_theme <-
list(course_theme, scale_color_manual(values = course_colors))
course_theme <-
list(course_theme, scale_fill_manual(values = course_colors))
course_theme
}

tokupon_course_pal <- function() {
scales::manual_pal(course_colors)
}

scale_colour_tokupon <- function(...) ggplot2::discrete_scale("colour", "tokuopon", tokupon_course_pal(), ...)

scale_fill_tokupon <- function(...) ggplot2::discrete_scale("fill", "tokuopon", tokupon_course_pal(), ...)

# scales::show_col(colours = course_colors)
24 changes: 18 additions & 6 deletions summary_statistics.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ library(palmerpenguins)
library(patchwork)
```

```{r}
# 作図のための関数
source("scripts/color_palette.R")
```

## データの位置を示す代表値: 平均値、中央値、最頻値

数値の傾向を捉えるには、その値が分布する位置を考えることが重要になります。
Expand Down Expand Up @@ -509,18 +514,23 @@ p
#| dev: ragg_png
#| code-fold: true
p +
geom_vline(xintercept = mean(penguins$body_mass_g, na.rm = TRUE)) +
geom_vline(xintercept = mean(penguins$body_mass_g, na.rm = TRUE),
color = course_colors[3]) +
geom_vline(xintercept = median(penguins$body_mass_g, na.rm = TRUE),
color = "red") +
geom_vline(xintercept = as.numeric(names(which.max(table(penguins$body_mass_g))))) +
color = course_colors[2]) +
geom_vline(xintercept = as.numeric(names(which.max(table(penguins$body_mass_g)))),
color = course_colors[1]) +
geom_label(aes(4400, 20),
label = "平均値",
color = course_colors[3],
show.legend = FALSE) +
geom_label(aes(4050, 50),
label = "中央値",
color = course_colors[2],
show.legend = FALSE) +
geom_label(aes(3600, 80),
label = "最頻値",
color = course_colors[1],
show.legend = FALSE)
```

Expand All @@ -541,14 +551,14 @@ p +
p1 <-
penguins |>
ggplot(aes(body_mass_g)) +
geom_histogram(bins = 2) +
geom_histogram(bins = 2, fill = course_colors[1]) +
ylab("Frequency") +
xlab("Body mass (g)") +
labs(title = "ペンギンの体重のヒストグラム。階級数2")
p2 <-
penguins |>
ggplot(aes(body_mass_g)) +
geom_histogram(bins = 30) +
geom_histogram(bins = 30, fill = course_colors[2]) +
ylab("Frequency") +
xlab("Body mass (g)") +
labs(title = "ペンギンの体重のヒストグラム。階級数30")
Expand Down Expand Up @@ -633,9 +643,11 @@ df_zoo |>
mutate(body_length_median = median(body_length_cm)) |>
ungroup() |>
mutate(taxon = forcats::fct_reorder(taxon, body_length_median)) |>
ggplot(aes(taxon, body_length_cm)) +
ggplot(aes(taxon, body_length_cm, color = taxon)) +
geom_boxplot() +
coord_flip() +
scale_colour_tokupon() +
guides(color = "none") +
labs(title = "動物データの分類群ごとの体長の箱ヒゲ図")
```

Expand Down
51 changes: 27 additions & 24 deletions visualization.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@
title: "グラフの作成"
---

```{r}
#| include: false
library(dplyr)
library(ggplot2)
library(patchwork)
library(palmerpenguins)
```

```{r}
#| include: false
source("data-raw/zoo.R")
source("data-raw/zoo_conservation.R")
```


[データをグラフに表現する](data.qmd#データをグラフに表現する)では、ヒストグラムと箱ヒゲ図を紹介しました。

データの可視化が重要
Expand All @@ -30,6 +22,26 @@ source("data-raw/zoo.R")

グラフの良し悪しを判断できるようになるためには、数多くのグラフを見ること、審美眼を養うこと、そしてデータ表現について批判できるようになるが重要です。

章の中で利用するパッケージを次のコマンドで読み込みます。

```{r}
#| message: false
library(dplyr)
library(ggplot2)
library(patchwork)
library(palmerpenguins)
library(sf)
library(rnaturalearth)
library(rnaturalearthhires)
```

また次のコマンドの実行は作図の塗り分けを行うための関数を読み込むための処理です。

```{r}
source("scripts/color_palette.R")
```



## 探索的データ解析

Expand Down Expand Up @@ -97,8 +109,8 @@ anscombe_long |>
group_by(set) |>
group_map(
~ ggplot(.x, aes(x, y)) +
geom_point(color = "orange") +
geom_smooth(method = lm, se = FALSE)) |>
geom_point(color = course_colors[1]) +
geom_smooth(method = lm, se = FALSE, color = course_colors[2])) |>
wrap_plots(ncol = 2)
```

Expand Down Expand Up @@ -213,6 +225,7 @@ df_zoo |>
filter(!is.na(body_length_cm)) |>
ggplot(aes(name, body_length_cm, fill = taxon)) +
geom_bar(stat = "identity") +
scale_fill_tokupon() +
xlab(NULL) +
ylab("体長 (cm)") +
labs(title = "とくしま動物園で飼育される動物の標準的な体長")
Expand All @@ -230,6 +243,7 @@ df_zoo |>
filter(!is.na(body_length_cm)) |>
ggplot(aes(forcats::fct_reorder(name, body_length_cm), body_length_cm, fill = taxon)) +
geom_bar(stat = "identity") +
scale_fill_tokupon() +
coord_flip() +
xlab(NULL) +
ylab("体長 (cm)") +
Expand Down Expand Up @@ -283,6 +297,7 @@ df_zoo |>
mutate(prop = n / sum(n) * 100) |>
ggplot(aes(x = "", y = prop, fill = taxon)) +
geom_bar(stat = "identity", width = 1) +
scale_fill_tokupon() +
coord_polar("y")
```

Expand All @@ -308,6 +323,7 @@ df_zoo |>
arrange(taxon) |>
ggplot(aes(x = "", y = prop, fill = taxon)) +
geom_bar(stat = "identity", width = 1) +
scale_fill_tokupon() +
coord_polar("y", start = 0) +
guides(fill = guide_legend(reverse = TRUE)) +
theme(axis.text = element_blank())
Expand All @@ -321,19 +337,6 @@ df_zoo |>

<!-- スノウのコレラの話 -->

```{r}
#| include: false
source("data-raw/zoo_conservation.R")
```

```{r}
#| message: false
library(sf)
library(rnaturalearth)
library(rnaturalearthhires)
library(dplyr)
```

```{r}
df_countries <-
countrycode::codelist |>
Expand Down

0 comments on commit 43d20cb

Please sign in to comment.