# 22章 ggplot2でコミュニケーションのためのグラフ作成

### 22.0 ライブラリの読み込み

```
library("tidyverse")
library("modelr")
library("lubridate")
```

### 22.1 はじめに

{% hint style="info" %}
練習問題ありません。
{% endhint %}

### 22.2 ラベル

#### 練習問題1 燃費データで`title`、`subtitle`、`caption`、`x`、`y`、および`colour`のラベルをカスタマイズしなさい。

`labs()`を使うことで、ラベル情報を補足できます。

```
ggplot(mpg, aes(fct_reorder(class, hwy), hwy)) +
  geom_boxplot() +
  labs(
    title = "Compact Cars have > 10 Hwy MPG than Pickup Trucks",
    subtitle = "Comparing the median highway mpg in each class",
    caption = "Data from fueleconomy.gov",
    x = "Car Class",
    y = "Highway Miles per Gallon"
  ) + 
  theme_classic()
```

![](/files/-LjBf6aiuLf1K4qUPZbO)

#### 練習問題2 大きなエンジンを搭載した軽量のスポーツカーが含まれているため、`geom_smooth()`が上向きに歪んでいる。モデリングツールを使用して、より適切なモデルを表示しなさい。

車のクラスによって、燃費と排気量は変わるはずです。そのためクラスごとに回帰直線を引いてみます。その結果として、関係の強さの程度が変わります。関係がない車のクラスもいくつか見られます。

```
ggplot(mpg, aes(displ, hwy, col = class)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    title = "Fuel Efficiency Mostly Varies by Car Class",
    subtitle = "Subcompact caries fuel efficiency varies by engine size",
    caption = "Data from fueleconomy.gov",
    y = "Highway Miles per Gallon",
    x = "Engine Displacement"
  )
```

![](/files/-LjBfXe9XeuR-5UGM50y)

#### 練習問題3 先月作成した探索的なグラフィックを使用して、他の人が理解しやすいように有益なタイトルを追加しなさい。

お任せします。

### 22.3 アノテーション

#### 練習問題1 `geom_text()`プロットの四隅にテキストを配置するには、無限の位置で使用します。

```
label <- tribble(
  ~displ, ~hwy, ~label, ~vjust, ~hjust,
  Inf, Inf, "Top right", "top", "right",
  Inf, -Inf, "Bottom right", "bottom", "right",
  -Inf, Inf, "Top left", "top", "left",
  -Inf, -Inf, "Bottom left", "bottom", "left"
)

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_text(data = label, aes(label = label, vjust = vjust, hjust = hjust)
```

![](/files/-LjBh0DsSsewAW6ZhXmO)

#### 練習問題2 `annotate()`のドキュメントを読んで、`tibble`を作成せずにテキストラベルをプロットに追加するにはどうすればよいか。

```
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  annotate("text",
           x = Inf, y = Inf,
           label = "Increasing engine size is related to \ndecreasing fuel economy.", vjust = "top", hjust = "right"
  )
```

![](/files/-LjBh_6RVR49aWybeTaZ)

#### 練習問題3 `geom_text()`はファセットとどのように作用しあうのか？単一のファセットにラベルを追加する方法はあるか。どのようにして各ファセットに異なるラベルを付けられるのか。

ファセットにラベルを描画するには、`label`の`class`に設定します。

```
label <- tibble(
  displ = Inf,
  hwy = Inf,
  class = "2seater",
  label = "Increasing engine size is \nrelated to decreasing fuel economy."
)

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_text(aes(label = label),
    data = label, vjust = "top", hjust = "right",
    size = 2
  ) +
  facet_wrap(~class)
```

![](/files/-LjBi-dbSb0foK0AZkgu)

各ファセットにラベルを描画するには、同じく`label`に`class`を設定します。

```
label <- tibble(
  displ = Inf,
  hwy = Inf,
  class = unique(mpg$class),
  label = str_c("Label for ", class)
)

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_text(aes(label = label),
    data = label, vjust = "top", hjust = "right",
    size = 3
  ) +
  facet_wrap(~class)
```

![](/files/-LjBiMl5bSq7ejTVAIuF)

#### 練習問題4 `geom_label()`の背景ボックスの外観を制御するための引数は何か？

* `label.padding`：ラベルの周囲のパディング
* `label.r`：角の丸みの量
* `label.size`：ラベル枠の大きさ

#### 練習問題5 `arrow()`の4つの引数は何ですか？どうやって動くのぁ？最も重要なオプションを説明する一連のプロットを作成しなさい。

* `angle` ：矢の角度
* `length` ：矢の長さ
* `ends`：矢を引く線の端
* `type`：`"open"`または`"close"`：矢が閉じた三角形か開いた三角形か

### 22.4 スケール

#### 練習問題1 次のコードがデフォルトのスケールを上書きしないのはなぜか？

この色付けは`color`ではなく`fill`で設定します。なので、`scale_fill_gradient()`で設定します。

```
df <- tibble(x = rnorm(10000), y = rnorm(10000)) 

ggplot(df, aes(x, y)) + 
  geom_hex() + 
  scale_colour_gradient(low = "white", high = "red") + 
  coord_fixed()
```

![](/files/-LjBjayRXo0j3j4cJIii)

```
ggplot(df, aes(x, y)) +
  geom_hex() +
  scale_fill_gradient(low = "white", high = "red") +
  coord_fixed()
```

![](/files/-LjBjswBY3QwkIWqmMeH)

#### 練習問題2 すべてのスケールの最初の引数はスケールのラベルです。`labs()`を使うのと同じです。

```
g1 <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  geom_smooth(se = FALSE) +
  labs(
    x = "Engine displacement (L)",
    y = "Highway fuel economy (mpg)",
    colour = "Car type"
  )

g2 <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  geom_smooth(se = FALSE) +
  scale_x_continuous("Engine displacement (L)") +
  scale_y_continuous("Highway fuel economy (mpg)") +
  scale_colour_discrete("Car type")

gridExtra::grid.arrange(g1, g2, ncol = 2)
```

![](/files/-LjBkc56k4MeXq9WFRwf)

#### 練習問題3 次のように大統領任期の表示を変更します。

* 上記の2つの変形を組み合わせる。
* y軸の表示を改善。
* 大統領の名前で各用語にラベルを付ける。
* 有益なプロットラベルを追加。
* 配置は4年ごとに特徴づける。

```
fouryears <- lubridate::make_date(seq(year(min(presidential$start)),
  year(max(presidential$end)),
  by = 4
), 1, 1)

presidential %>%
  mutate(
    id = 33 + row_number(),
    name_id = fct_inorder(str_c(name, " (", id, ")"))
  ) %>%
  ggplot(aes(start, name_id, colour = party)) +
  geom_point() +
  geom_segment(aes(xend = end, yend = name_id)) +
  scale_colour_manual("Party", values = c(Republican = "red", Democratic = "blue")) +
  scale_y_discrete(NULL) +
  scale_x_date(NULL,
    breaks = presidential$start, date_labels = "'%y",
    minor_breaks = fouryears
  ) +
  ggtitle("Terms of US Presdients",
    subtitle = "Roosevelth (34th) to Obama (44th)"
  ) +
  theme(
    panel.grid.minor = element_blank(),
    axis.ticks.y = element_blank()
  )
```

![](/files/-LjBkyQ6fcNP2xCT9iyR)

#### 練習問題4 次のプロットの凡例を見やすくするために、`override.aes`を使用しなさい。

`override.aes()`を使えば、`alpha`で透過した凡例をコントロールできます。

```
ggplot(diamonds %>% sample_n(10000), aes(carat, price, col = cut)) +
  geom_point(alpha = 0.2) +
  theme(legend.position = "bottom") +
  guides(colour = guide_legend(nrow = 1, override.aes = list(alpha = 1)))
```

![](/files/-LjBm0khawhu72zP8P6R)

### 22.5 ズーミング

{% hint style="info" %}
練習問題はありません
{% endhint %}

### 22.6 テーマ

{% hint style="info" %}
練習問題はありません
{% endhint %}

### 22.7 プロットを保存する

{% hint style="info" %}
練習問題はありません
{% endhint %}

### 22.8 もっと知る

{% hint style="info" %}
練習問題はありません
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sugiaki1989.gitbook.io/r-for-data-science-answers/chapter22-report.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
