ggplot2のscale_*()関数についてのまとめ

# はじめに

ggplot2では、データから、`stat_*()`関数や`geom_*()`関数で基本的なグラフのレイヤーを描き、`scale_*()`関数で軸の設定をします。
今回は、ggplot2における軸の設定についてまとめます。

**目次**

- [scale_*()](#scale_)
    - [scale_x_\*(), scale_y_\*()：x軸, y軸](#scale_x_-scale_y_x軸-y軸)
        - [scale_x_continuous(), scale_y_continuous()：連続値のx軸, y軸](#scale_x_continuous-scale_y_continuous連続値のx軸-y軸)
            - [引数name：軸のラベル](#引数name軸のラベル)
            - [引数breaks：軸の目盛り](#引数breaks軸の目盛り)
            - [引数minor_breaks：軸の補助目盛り](#引数minor_breaks軸の補助目盛り)
            - [引数labels：軸の目盛りのラベル](#引数labels軸の目盛りのラベル)
            - [引数limits：軸の範囲](#引数limits軸の範囲)
            - [引数expand：値の範囲の外に拡張される軸の幅](#引数expand値の範囲の外に拡張される軸の幅)
            - [引数trans：軸のスケール変換](#引数trans軸のスケール変換)
            - [引数position：軸の表示場所](#引数position軸の表示場所)
            - [引数guide：軸の凡例](#引数guide軸の凡例)
                - [guide_axis()：軸の凡例を設定する関数](#guide_axis軸の凡例を設定する関数)
                    - [引数n.dodge：軸のラベルを複数列にずらして表示](#引数ndodge軸のラベルを複数列にずらして表示)
                    - [引数angle：軸のラベルの表示の角度](#引数angle軸のラベルの表示の角度)
            - [引数sec.axis：第２軸](#引数secaxis第２軸)
        - [scale_x_discrete(), scale_y_discrete()：離散値のx軸, y軸](#scale_x_discrete-scale_y_discrete離散値のx軸-y軸)
            - [引数name：軸のラベル](#引数name軸のラベル-1)
            - [引数labels：軸の目盛りのラベル](#引数labels軸の目盛りのラベル-1)
            - [引数limits：軸の範囲](#引数limits軸の範囲-1)
            - [引数expand：値の範囲の外に拡張される軸の幅](#引数expand値の範囲の外に拡張される軸の幅-1)
            - [引数position：軸の表示場所](#引数position軸の表示場所-1)
            - [引数guide：軸の凡例](#引数guide軸の凡例-1)
                - [guide_axis()：軸の凡例を設定する関数](#guide_axis軸の凡例を設定する関数-1)
                    - [引数n.dodge：軸のラベルを複数列にずらして表示](#引数ndodge軸のラベルを複数列にずらして表示-1)
                    - [引数angle：軸のラベルの表示の角度](#引数angle軸のラベルの表示の角度-1)
        - [scale_x_binned(), scale_y_binned()：連続値のx軸, y軸のビン分割](#scale_x_binned-scale_y_binned連続値のx軸-y軸のビン分割)
            - [引数name：軸のラベル](#引数name軸のラベル-2)
            - [引数n.breaks, nice.breaks](#引数nbreaks-nicebreaks)
            - [引数show.limits](#引数showlimits)
            - [引数breaks：軸の目盛り](#引数breaks軸の目盛り-1)
            - [引数limits：軸の範囲](#引数limits軸の範囲-2)
            - [引数labels：軸の目盛りのラベル](#引数labels軸の目盛りのラベル-2)
            - [引数trans：軸のスケール変換](#引数trans軸のスケール変換-1)
    - [scale_color_\*(), scale_fill_\*(), scale_alpha_\*(), scale_size_\*(), scale_shape_\*(), scale_linetype_\*()：color軸, fill軸, alpha軸, shape軸, size軸, linetype軸](#scale_color_-scale_fill_-scale_alpha_-scale_size_-scale_shape_-scale_linetype_color軸-fill軸-alpha軸-shape軸-size軸-linetype軸)
        - [scale_*_continuous()：連続値のcolor軸など](#scale__continuous連続値のcolor軸など)
        - [scale_*_binned()：連続値のcolor軸などのビン分割](#scale__binned連続値のcolor軸などのビン分割)
        - [scale_*_discrete()：離散値のcolor軸など](#scale__discrete離散値のcolor軸など)
            - [引数name：軸のラベル](#引数name軸のラベル-3)
            - [引数breaks：軸の目盛り](#引数breaks軸の目盛り-2)
            - [引数labels：軸の目盛りのラベル](#引数labels軸の目盛りのラベル-3)
            - [引数limits：軸の範囲](#引数limits軸の範囲-3)
            - [引数trans：軸のスケール変換](#引数trans軸のスケール変換-2)
            - [引数guide：軸の凡例](#引数guide軸の凡例-2)
                - [guide_legend()：凡例を設定する関数](#guide_legend凡例を設定する関数)
                    - [引数reverse：凡例の軸の反転](#引数reverse凡例の軸の反転)
                    - [引数nrow, ncol, byrow：凡例を何行何列に表示するか](#引数nrow-ncol-byrow凡例を何行何列に表示するか)
                    - [引数title, title.position：凡例のタイトル](#引数title-titleposition凡例のタイトル)
                    - [引数label, label.position：凡例のラベル](#引数label-labelposition凡例のラベル)
                - [guide_colorbar()：カラーバーを設定する関数](#guide_colorbarカラーバーを設定する関数)
                    - [引数barwidth, barheiht：カラーバーの大きさ](#引数barwidth-barheihtカラーバーの大きさ)
                    - [引数ticks, ticks.colour, ticks.linewidth：カラーバーの目盛り](#引数ticks-tickscolour-tickslinewidthカラーバーの目盛り)
                - [guide_bins()](#guide_bins)
                - [guide_coloursteps()](#guide_coloursteps)
                    - [引数even.steps](#引数evensteps)
                    - [引数show.limits](#引数showlimits)
                    - [引数axis.arrow](#引数axisarrow)
        - [scale_*_manual()：離散値のcolor軸などの手動設定](#scale__manual離散値のcolor軸などの手動設定)
            - [引数values：手動設定したい値の指定](#引数values手動設定したい値の指定)
        - [scale_*_identity()：color軸などのデータの値をそのまま設定](#scale__identitycolor軸などのデータの値をそのまま設定)
    - [scale_color_\*(), scale_fill_\*()：color軸, fill軸](#scale_color_-scale_fill_color軸-fill軸)
        - [scale_color_gradient(), scale_fill_gradient()：連続値のcolor軸, fill軸](#scale_color_gradient-scale_fill_gradient連続値のcolor軸-fill軸)
        - [scale_color_steps(), scale_fill_steps()：連続値のcolor軸, fill軸のビン分割](#scale_color_steps-scale_fill_steps連続値のcolor軸-fill軸のビン分割)
        - [scale_color_gradient2(), scale_fill_gradient2()：連続値のcolor軸, fill軸](#scale_color_gradient2-scale_fill_gradient2連続値のcolor軸-fill軸)
        - [scale_color_steps2(), scale_fill_steps2()：連続値のcolor軸, fill軸のビン分割](#scale_color_steps2-scale_fill_steps2連続値のcolor軸-fill軸のビン分割)
        - [scale_color_gradientn(), scale_fill_gradientn()：連続値のcolor軸, fill軸](#scale_color_gradientn-scale_fill_gradientn連続値のcolor軸-fill軸)
        - [scale_color_stepsn(), scale_fill_stepsn()：連続値のcolor軸, fill軸のビン分割](#scale_color_stepsn-scale_fill_stepsn連続値のcolor軸-fill軸のビン分割)
        - [scale_color_distiller(), scale_fill_distiller()：連続値のcolor軸, fill軸](#scale_color_distiller-scale_fill_distiller連続値のcolor軸-fill軸)
        - [scale_color_fermenter(), scale_fill_fermenter()：連続値のcolor軸, fill軸のビン分割](#scale_color_fermenter-scale_fill_fermenter連続値のcolor軸-fill軸のビン分割)
        - [scale_color_continuous(), scale_fill_continuous()：連続値のcolor軸, fill軸](#scale_color_continuous-scale_fill_continuous連続値のcolor軸-fill軸)
        - [scale_color_binned(), scale_fill_binned()：連続値のcolor軸, fill軸のビン分割](#scale_color_binned-scale_fill_binned連続値のcolor軸-fill軸のビン分割)
        - [scale_color_viridis_c(), scale_fill_viridis_c()：連続値のcolor軸, fill軸](#scale_color_viridis_c-scale_fill_viridis_c連続値のcolor軸-fill軸)
        - [scale_colour_viridis_b(), scale_fill_viridis_b()：連続値のcolor軸, fill軸のビン分割](#scale_colour_viridis_b-scale_fill_viridis_b連続値のcolor軸-fill軸のビン分割)
        - [scale_color_brewer(), scale_fill_brewer()：離散値のcolor軸, fill軸](#scale_color_brewer-scale_fill_brewer離散値のcolor軸-fill軸)
        - [scale_color_grey(), scale_fill_grey()：離散値のcolor軸, fill軸](#scale_color_grey-scale_fill_grey離散値のcolor軸-fill軸)
        - [scale_color_hue(), scale_fill_hue()：離散値のcolor軸, fill軸](#scale_color_hue-scale_fill_hue離散値のcolor軸-fill軸)
        - [scale_color_discrete(), scale_fill_discrete()：離散値のcolor軸, fill軸](#scale_color_discrete-scale_fill_discrete離散値のcolor軸-fill軸)
        - [scale_color_viridis_d(), scale_fill_viridis_d()：離散値のcolor軸, fill軸](#scale_color_viridis_d-scale_fill_viridis_d離散値のcolor軸-fill軸)
    - [scale_shape_*()：shape軸](#scale_shape_shape軸)
        - [scale_shape_discrete(), scale_shape_binned()：離散値のshape軸, 連続値のshape軸のビン分割](#scale_shape_discrete-scale_shape_binned離散値のshape軸-連続値のshape軸のビン分割)
            - [引数solid：shapeの塗りつぶし](#引数solidshapeの塗りつぶし)
- [まとめ](#まとめ)
- [参考文献](#参考文献)


```R:R
library(tidyverse)
```


# scale_*()

ggplot2では、軸の設定は`scale_*()`関数で行います。


## scale_x_\*(), scale_y_\*()：x軸, y軸

x軸, y軸の設定をします。
連続値のときは`scale_*_continuous()`、離散値のときは`scale_*_discrete()`を使います。また、連続値をビン分割する場合は`scale_*_binned()`を使います。

- `scale_*_continuous()`：連続（continuous）値の場合
- `scale_*_discrete()`：離散（discrete）値の場合
- `scale_*_binned()`：連続（continuous）値の場合（ビン分割）


### scale_x_continuous(), scale_y_continuous()：連続値のx軸, y軸

x軸, y軸が連続値の場合、`scale_x_continuous()`, `scale_y_continuous()`で軸の設定を調整します。


#### 引数name：軸のラベル

`name`で軸のラベルに表示される文字列を設定できます。
`xlab()`, `ylab()`でも設定できます。また、`labs(x = ..., y = ...)`でも設定できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(name = "Sepal Length (cm)") +
  scale_y_continuous(name = "Sepal Width (cm)")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  xlab("Sepal Length (cm)") +
  ylab("Sepal Width (cm)")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  labs(x = "Sepal Length (cm)", y = "Sepal Width (cm)")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(name = "") +
  scale_y_continuous(name = "")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  xlab("") +
  ylab("")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  labs(x = "", y = "")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/62addd23-58ba-aa61-8492-c40d04ad8dde.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/fbf854a8-ab0e-8a76-0fde-e84d7e1b2c2a.png)

`NULL`にすると軸のラベルの表示自体がなくなります。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(name = NULL) +
  scale_y_continuous(name = NULL)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  xlab(label = NULL) +
  ylab(label = NULL)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  labs(x = NULL, y = NULL)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/89eda175-0f16-4024-163a-84e14887ca67.png)

なお、デフォルトでは`name = waiver()`で、軸の変数名がそのまま表示されます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous() +
  scale_y_continuous()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(name = waiver()) +  # デフォルト
  scale_y_continuous(name = waiver())    # デフォルト
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/312a84a0-3a8d-3014-97d2-511ccd237940.png)


#### 引数breaks：軸の目盛り

`breaks`で軸の目盛りを設定できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = 4:8) +
  scale_y_continuous(breaks = 2:5)

seq(4, 8, by = 0.5)
# [1] 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
seq(2, 5, by = 0.5)
# [1] 2.0 2.5 3.0 3.5 4.0 4.5 5.0
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.5)) +
  scale_y_continuous(breaks = seq(2, 5, by = 0.5))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2704b3d3-f015-abfa-d25f-885b0d0d1141.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/c254bf3e-2c35-c0d8-1eb5-aab221d793a3.png)

`NULL`とすると軸の目盛りの表示がなくなります。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = 2:5)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/797cb950-796c-c155-8937-779105abe1e8.png)

なお、`breaks`は等間隔でなくても構いません。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = c(4:8, range(iris$Sepal.Length)),
                     minor_breaks = NULL,
                     limits = c(4, 8)) +
  scale_y_continuous(breaks = c(2:5, range(iris$Sepal.Width)),
                     minor_breaks = NULL,
                     limits = c(2, 5))

c(4:8, range(iris$Sepal.Length))
# [1] 4.0 5.0 6.0 7.0 8.0 4.3 7.9
c(2:5, range(iris$Sepal.Width))
# [1] 2.0 3.0 4.0 5.0 2.0 4.4
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/6b15a2ef-2cf6-586d-9c69-04dcd1771147.png)
注）わかりやすいように`minor_breaks = NULL`で補助目盛りを削除し、`limits`で軸の範囲を指定してあります（`minor_breaks`、`limits`については以下の項を参照。）。


#### 引数minor_breaks：軸の補助目盛り

`minor_breaks`で軸の補助目盛り（軸の目盛りの間の目盛り）を設定できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = 4:8, minor_breaks = seq(4, 8, by = 0.2)) +
  scale_y_continuous(breaks = 2:5, minor_breaks = seq(2, 5, by = 0.2))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = 4:8, minor_breaks = NULL) +
  scale_y_continuous(breaks = 2:5, minor_breaks = NULL)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/776f087f-a4ea-005c-d684-973d4d45d3dd.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d32c41ec-8ed9-a4d7-e389-dc16d03f3d66.png)

`minor_breaks`も等間隔でなくても構いません。

```R:R
ggplot(data = data.frame(x = 0:10, y = 2^(0:10)), aes(x = x, y = y)) +
  geom_point() +
  scale_x_continuous(trans = "identity",
                     breaks = 0:10) +
  scale_y_continuous(trans = "log10",
                     breaks = 10^(0:3),
                     minor_breaks = unique(as.numeric(1:10 %o% 10 ^ (0:3))),
                     labels = c(1, 10, 100, 1000))
2^(0:10)
# [1]    1    2    4    8   16   32   64  128  256  512 1024
10^(0:3)
# [1]    1   10  100 1000
as.numeric(1:10 %o% 10 ^ (0:3))
#  [1]     1     2     3     4     5     6     7     8     9    10
# [11]    10    20    30    40    50    60    70    80    90   100
# [21]   100   200   300   400   500   600   700   800   900  1000
# [31]  1000  2000  3000  4000  5000  6000  7000  8000  9000 10000
unique(as.numeric(1:10 %o% 10 ^ (0:3)))
#  [1]     1     2     3     4     5     6     7     8     9
# [10]    10    20    30    40    50    60    70    80    90
# [19]   100   200   300   400   500   600   700   800   900
# [28]  1000  2000  3000  4000  5000  6000  7000  8000  9000
# [37] 10000
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/6e2655ad-c170-e128-15b1-0c57fd24fccd.png)
注）`trans`については以下の項を参照。
参考：https://ggplot2-book.org/scale-position.html#minor-breaks


#### 引数labels：軸の目盛りのラベル

`labels`で軸の目盛りのラベルを指定できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = 4:8,
                     labels = str_c(4:8, "(cm)")) +
  scale_y_continuous(breaks = 2:5,
                     labels = str_c(2:5, "(cm)"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/cfc29d5d-a8df-bac4-3667-ff184bd04a78.png)


#### 引数limits：軸の範囲

`limits`で軸の範囲を指定できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(limits = c(4, 8)) +
  scale_y_continuous(limits = c(2, 4.5))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  xlim(c(4, 8)) +
  ylim(c(2, 4.5))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/4b929461-73b4-efa1-d2dd-d960fcd90cbe.png)


ここまでを全部合わせて

```R:R
x_width <- 1.0
x_min <- floor(min(iris$Sepal.Length))    # 4
x_max <- ceiling(max(iris$Sepal.Length))  # 8
x_major_breaks <- seq(x_min, x_max, by = x_width)
x_major_breaks
# [1] 4 5 6 7 8
x_minor_breaks <- seq(x_min, x_max, by = x_width / 4)
x_minor_breaks
# [1] 4.00 4.25 4.50 4.75 5.00 5.25 5.50 5.75 6.00 6.25 6.50 6.75 7.00 7.25 7.50 7.75 8.00
y_width <- 0.5
y_min <- floor(min(iris$Sepal.Width) * 2) / 2   # 2
y_max <- ceiling(max(iris$Sepal.Width) * 2) / 2 # 4.5
y_major_breaks <- seq(y_min, y_max, by = y_width)
y_major_breaks
# [1] 2.0 2.5 3.0 3.5 4.0 4.5
y_minor_breaks <- seq(y_min, y_max, by = y_width / 2)
y_minor_breaks
# [1] 2.00 2.25 2.50 2.75 3.00 3.25 3.50 3.75 4.00 4.25 4.50
str_c(format(x_major_breaks, nsmall = 0), " cm")
sprintf("%d cm", x_major_breaks)
x_labels <- sprintf("%d cm", x_major_breaks)
x_labels
# [1] "4 cm" "5 cm" "6 cm" "7 cm" "8 cm"
str_c(format(y_major_breaks, nsmall = 1), " cm")
sprintf("%.1f cm", y_major_breaks)
y_labels <- sprintf("%.1f cm", y_major_breaks)
y_labels
# [1] "2.0 cm" "2.5 cm" "3.0 cm" "3.5 cm" "4.0 cm" "4.5 cm"
x_limits <- c(x_min, x_max)
x_limits
# [1] 4 8
y_limits <- c(y_min, y_max)
y_limits
# [1] 2.0 4.5
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = x_major_breaks, minor_breaks = x_minor_breaks,
                     labels = x_labels,
                     limits = x_limits) +
  scale_y_continuous(breaks = y_major_breaks, minor_breaks = y_minor_breaks,
                     labels = y_labels,
                     limits = y_limits)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2c6a6050-2474-0719-a8f4-b17298ba916d.png)


#### 引数expand：値の範囲の外に拡張される軸の幅

`expand`で値の範囲の外に拡張される軸の幅を指定できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(expand = expansion(mult = 0)) +
  scale_y_continuous(expand = expansion(mult = 0))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(expand = expansion(add = 0)) +
  scale_y_continuous(expand = expansion(add = 0))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/c33bfc81-96f5-3b2f-1b57-75f50bd02743.png)

デフォルトでは、連続値の場合は値の範囲の5%外側までとなっています。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous() +
  scale_y_continuous()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(expand = expansion(mult = 0.05)) + # デフォルト（値の範囲の5%）
  scale_y_continuous(expand = expansion(mult = 0.05))   # デフォルト（値の範囲の5%）
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(expand = expansion(add = diff(range(iris$Sepal.Length))*0.05)) +
  scale_y_continuous(expand = expansion(add = diff(range(iris$Sepal.Width))*0.05))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/312a84a0-3a8d-3014-97d2-511ccd237940.png)

```R:R
range(iris$Sepal.Length)
# [1] 4.3 7.9
diff(range(iris$Sepal.Length))
# [1] 3.6
diff(range(iris$Sepal.Length))*0.05
# [1] 0.18
min(iris$Sepal.Length) - diff(range(iris$Sepal.Length))*0.05
# [1] 4.12
max(iris$Sepal.Length) + diff(range(iris$Sepal.Length))*0.05
# [1] 8.08

range(iris$Sepal.Width)
# [1] 2.0 4.4
diff(range(iris$Sepal.Width))
# [1] 2.4
diff(range(iris$Sepal.Width))*0.05
# [1] 0.12
min(iris$Sepal.Width) - diff(range(iris$Sepal.Width))*0.05
# [1] 1.88
max(iris$Sepal.Width) + diff(range(iris$Sepal.Width))*0.05
# [1] 4.52
```


#### 引数trans：軸のスケール変換

`trans`で軸のスケール変換を行うことができます。
デフォルトでは`"identity"`で恒等変換です（何も変換されません。）。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(trans = "identity") +  # デフォルト
  scale_y_continuous(trans = "identity")    # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(trans = "log10") +  # 常用対数変換
  scale_y_continuous(trans = "sqrt")     # sqrt変換
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_log10() +
  scale_y_sqrt()

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(trans = "reverse") +  # 軸の反転
  scale_y_continuous(trans = "reverse")    # 軸の反転
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_reverse() +
  scale_y_reverse()
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/312a84a0-3a8d-3014-97d2-511ccd237940.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/dbe78398-019c-c6e8-28d7-22c0946cf62d.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/234d8ae6-6785-52a2-04cc-72a180667502.png)


#### 引数position：軸の表示場所

`position`で軸を表示する場所を設定します。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous() +
  scale_y_continuous()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(position = "bottom") +  # デフォルト
  scale_y_continuous(position = "left")      # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(position = "top") +
  scale_y_continuous(position = "right")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/c33bfc81-96f5-3b2f-1b57-75f50bd02743.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/23071fde-cfea-3f7b-f873-bf90c3e8c0fd.png)


#### 引数guide：軸の凡例

`guide`で軸の凡例を設定します。デフォルトでは通常の軸が表示されます。
また、これは`guides()`でも設定できます。`scale_x_continuous(guide = ...)`と`guides(x = ...)`は同じです。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(guide = guide_axis()) +  # デフォルト
  scale_y_continuous(guide = guide_axis())    # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(guide = "axis") +  # デフォルト
  scale_y_continuous(guide = "axis")    # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  guides(x = guide_axis()) +  # デフォルト
  guides(y = guide_axis())    # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  guides(x = "axis") +  # デフォルト
  guides(y = "axis")    # デフォルト
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/c33bfc81-96f5-3b2f-1b57-75f50bd02743.png)

`guide`に`NULL`か`"none"`を指定すると、軸が表示されなくなります。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(guide = NULL) +
  scale_y_continuous(guide = NULL)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(guide = "none") +
  scale_y_continuous(guide = "none")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  guides(x = "none") +
  guides(y = "none")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/b6bbc6a2-f43c-e87c-af8f-dbf5578453d3.png)

さらに、`guide_axis()`関数で詳細を設定できます。


##### guide_axis()：軸の凡例を設定する関数

`scale_x_continuous(guide = guide_axis(... ))`と`guides(x = guide_axis(... ))`は同じです。


###### 引数n.dodge：軸のラベルを複数列にずらして表示

通常、軸のラベルは一列に表示されますが、重なったり近すぎたりして見づらい場合などずらして表示したいときがあります。`n.dodge`で複数列にずらして表示できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.2), minor_breaks = NULL)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.2), minor_breaks = NULL,
                     guide = guide_axis(n.dodge = 1))  # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.2), minor_breaks = NULL) +
  guides(x = guide_axis(n.dodge = 1))  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.2), minor_breaks = NULL,
                     guide = guide_axis(n.dodge = 2))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.2), minor_breaks = NULL) +
  guides(x = guide_axis(n.dodge = 2))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.2), minor_breaks = NULL,
                     guide = guide_axis(n.dodge = 3))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.4), minor_breaks = NULL,
                     labels = sprintf("%.1fcm", seq(4, 8, by = 0.4))) +
  guides(x = guide_axis(n.dodge = 2))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/01cb0d4a-34d4-3fc5-bba2-395393e94296.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a2e7173b-e903-89f9-5af5-9144dbb13267.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/1fe1b2be-3da4-329a-6c45-bc3e41bc0203.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a0baf95e-e535-9579-6285-fe2327395c1e.png)


###### 引数angle：軸のラベルの表示の角度

`angle `で軸のラベルの表示の角度を指定できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.2), minor_breaks = NULL,
                     labels = sprintf("%.1f cm", seq(4, 8, by = 0.2)),
                     guide = guide_axis(angle = 90))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(breaks = seq(4, 8, by = 0.2), minor_breaks = NULL,
                     labels = sprintf("%.1f cm", seq(4, 8, by = 0.2))) +
  guides(x = guide_axis(angle = 90))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/b81ae69a-4ac7-c20a-2205-78e839f86662.png)


#### 引数sec.axis：第２軸

`sec.axis`で第２軸の設定ができます。
`sec.axis`に`dup_axis()`を指定すると、第１軸と同じ軸が複製されます。
新たな第２軸を作成するには、`sec_axis()`関数を使えます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(sec.axis = dup_axis()) +  # 軸の複製
  scale_y_continuous(sec.axis = dup_axis())    # 軸の複製
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(sec.axis = sec_axis(trans = ~ ., name = derive())) +
  scale_y_continuous(sec.axis = sec_axis(trans = ~ ., name = derive()))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  guides(x.sec = guide_axis(title = "Sepal.Length"),
         y.sec = guide_axis(title = "Sepal.Width"))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_continuous(name = "Sepal Length (cm)",
                     sec.axis = sec_axis(trans = ~ . / 100, name = "Sepal Length (m)")) +
  scale_y_continuous(name = "Sepal Width (cm)",
                     sec.axis = sec_axis(trans = ~ . * 10,  name = "Sepal Width (mm)"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/9360ff96-58e7-cc89-bfa6-30f6d5479974.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e44f59bf-b1d7-1229-74d0-46d6f87723f1.png)


```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  facet_grid(cols = vars(Species)) +
  scale_y_continuous(sec.axis = dup_axis())

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  facet_wrap(facets = vars(Species), ncol = 2) +
  scale_y_continuous(sec.axis = dup_axis())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/95263ab0-8853-df97-a3af-ddfa480c17dc.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0e55eb6e-803f-fd2c-d482-793823380f5b.png)



（注）`scales`ライブラリの`demo_continuous()`関数で連続値の軸のスケーリングのデモンストレーションを見ることもできます。

```R:R
library(scales)
demo_continuous(iris$Sepal.Length)

demo_continuous(iris$Sepal.Length, breaks = seq(4, 8, by = 0.5))

demo_continuous(iris$Sepal.Length, breaks = 4:8, minor_breaks = seq(4, 8, by = 0.2))

demo_continuous(iris$Sepal.Length, breaks = 4:8, minor_breaks = NULL)

demo_continuous(iris$Sepal.Length, breaks = 4:8, labels = str_c(4:8, "(cm)"))

demo_continuous(iris$Sepal.Length, breaks = 4:8, limits = c(4, 8))

demo_continuous(iris$Sepal.Length, breaks = 4:8, expand = c(0, 0))

demo_continuous(iris$Sepal.Length, breaks = 4:8, trans = "reverse")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/86cf3cab-2ef8-620e-777f-8887af9ee3b0.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/61261a57-53d2-f224-9373-efcc89b06c51.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d04ebc2f-8337-aa59-4dd6-fdafbcf9a14a.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/11d29e1d-ef6a-20a5-1303-364671826e66.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/cdf8df13-cdbf-dc39-5ebf-ea40396328a1.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/67f7b02e-7a99-4d8d-d334-97bfcb9161e0.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/76ffc7b9-cad4-527a-f7c3-04e75b02519f.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/53856309-b248-9911-f362-378a33bbfc87.png)




### scale_x_discrete(), scale_y_discrete()：離散値のx軸, y軸

x軸, y軸が離散値の場合、`scale_x_discrete()`, `scale_y_discrete()`で軸の設定を調整します。

#### 引数name：軸のラベル

`scale_x_continuous()`, `scale_x_continuous()`と同じく、`scale_x_discrete()`, `scale_y_discrete()`でも`name`で軸のラベルに表示される文字列を設定できます。
`xlab()`, `ylab()`でも設定できます。

```R:R
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(name = "Species Name") +
  scale_y_continuous(name = "Sepal Length (cm)")
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  xlab("Species Name") +
  ylab("Sepal Length (cm)")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/472a460c-767a-014f-34bd-c1f260d21045.png)


#### 引数labels：軸の目盛りのラベル

`scale_x_continuous()`, `scale_x_continuous()`と同じく、`scale_x_discrete()`, `scale_y_discrete()`でも`labels`で軸の目盛りのラベルを指定できます。

```R:R
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(labels = str_c("Iris ", str_to_title(unique(iris$Species)))) +
  scale_y_continuous(labels = str_c(4:8, "(cm)"))

unique(iris$Species)
# [1] setosa     versicolor virginica 
# Levels: setosa versicolor virginica
str_c("Iris ", str_to_title(unique(iris$Species)))
# [1] "Iris Setosa"     "Iris Versicolor" "Iris Virginica" 

ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(labels = c("setosa" = "Se", "versicolor" = "Ve", "virginica"  = "Vi")) +
  scale_y_continuous(labels = str_c(4:8, "(cm)"))

c("setosa" = "Se", "versicolor" = "Ve", "virginica"  = "Vi")
#     setosa versicolor  virginica 
#       "Se"       "Ve"       "Vi" 

ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(labels = abbreviate) +
  scale_y_continuous(labels = str_c(4:8, "(cm)"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/18b61a0a-038a-d614-000b-f37d8d7850a5.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/796a644e-94fb-55ac-204e-397ddbc543e3.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/c6795742-3560-1d2f-ff16-cb30e2e54b4a.png)


#### 引数limits：軸の範囲

`scale_x_continuous()`, `scale_x_continuous()`と同じく、`scale_x_discrete()`, `scale_y_discrete()`でも`limits`で軸の範囲を指定できます（表示するすべての値をベクトルで与えます。順序も反映されます。）。

```R:R
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(limits = c("setosa", "versicolor", "virginica", "unknown")) +
  scale_y_continuous(limits = c(4, 8))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/1c7f7547-88fe-6ebd-6f33-4fc09dbfc4f0.png)

これを用いると軸の反転もできます。連続値の場合と違って、`trans = "reverse"`で反転することはできません。

```R:R
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(limits = c("virginica", "versicolor", "setosa"))
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(limits = rev(unique(iris$Species)))
rev(unique(iris$Species))
# [1] virginica  versicolor setosa    
# Levels: setosa versicolor virginica
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/9184bebe-8a50-4937-c10f-dd75bf50dd25.png)


#### 引数expand：値の範囲の外に拡張される軸の幅

`scale_x_continuous()`, `scale_x_continuous()`と同じく、`scale_x_discrete()`, `scale_y_discrete()`でも`expand`で値の範囲の外に拡張される軸の幅を指定できます。
離散値の場合、デフォルトは単位の0.6倍外側となっています。

```R:R
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete() +
  scale_y_continuous()
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(expand = expansion(add = 0.6)) +    # デフォルト（単位の0.6倍）
  scale_y_continuous(expand = expansion(mult = 0.05))  # デフォルト（値の範囲の5%）

ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(expand = expansion(add = 0)) +
  scale_y_continuous(expand = expansion(add = 0))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/38311e25-1a76-3df5-d8ee-e062e2a54e35.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/4bd64d4a-8f09-1167-cc58-ea96a0b751f2.png)


#### 引数position：軸の表示場所

`scale_x_continuous()`, `scale_x_continuous()`と同じく、`scale_x_discrete()`, `scale_y_discrete()`でも`position`で軸の表示場所をを指定できます。

```R:R
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete() +
  scale_y_continuous()
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(position = "bottom") +  # デフォルト
  scale_y_continuous(position = "left")    # デフォルト

ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(position = "top") +
  scale_y_continuous(position = "right")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/38311e25-1a76-3df5-d8ee-e062e2a54e35.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a114f432-ad2c-342a-6f79-b26521a8d95e.png)


#### 引数guide：軸の凡例

`scale_x_continuous()`, `scale_x_continuous()`と同じく、`scale_x_discrete()`, `scale_y_discrete()`でも`guide`で軸の凡例を設定します。デフォルトでは通常の軸が表示されます。

```R:R
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(guide = guide_axis()) +  # デフォルト
  scale_y_continuous(guide = guide_axis())  # デフォルト
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(guide = "axis") +  # デフォルト
  scale_y_continuous(guide = "axis")  # デフォルト
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  guides(x = guide_axis()) +  # デフォルト
  guides(y = guide_axis())    # デフォルト
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  guides(x = "axis") +  # デフォルト
  guides(y = "axis")    # デフォルト
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/38311e25-1a76-3df5-d8ee-e062e2a54e35.png)


##### guide_axis()：軸の凡例を設定する関数

`scale_x_discrete(guide = guide_axis(... ))`と`guides(x = guide_axis(... ))`は同じです。


###### 引数n.dodge：軸のラベルを複数列にずらして表示

`scale_x_continuous()`, `scale_x_continuous()`と同じく、`scale_x_discrete()`, `scale_y_discrete()`の中でも`guide_axis()`関数の引数`n.dodge`で複数列にずらして表示できます。

```R:R
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(guide = guide_axis(n.dodge = 1)) +  # デフォルト
  scale_y_continuous(guide = guide_axis(n.dodge = 1))  # デフォルト
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  guides(x = guide_axis(n.dodge = 1)) +  # デフォルト
  guides(y = guide_axis(n.dodge = 1))    # デフォルト

ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(labels = str_c("Iris Species : ", unique(iris$Species)),
                   guide = guide_axis(n.dodge = 2))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/38311e25-1a76-3df5-d8ee-e062e2a54e35.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a98647ac-43cb-8942-b6a2-d836bd23f844.png)


#### 引数angle：軸のラベルの表示の角度

`scale_x_continuous()`, `scale_x_continuous()`と同じく、`scale_x_discrete()`, `scale_y_discrete()`の中でも`guide_axis()`関数の引数`angle`で軸のラベルの表示の角度を指定できます。

```R:R
ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(labels = str_c("Iris Species : ", unique(iris$Species)),
                   guide = guide_axis(angle = 10))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/b7abfd3e-eb6f-70a1-bc3d-33436246645f.png)



### scale_x_binned(), scale_y_binned()：連続値のx軸, y軸のビン分割

x軸, y軸が連続値の場合、連続値をビンで区分します。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_jitter() +
  scale_x_binned() +
  scale_y_binned()
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a29fa1f0-3c5b-6aab-c02e-a136546561d2.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned()
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/b11fcd67-1e68-22a1-20a3-8e42ebe05b40.png)


#### 引数name：軸のラベル

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_jitter() +
  scale_x_binned(name = "Sepal Length (cm)") +
  scale_y_binned(name = "Sepal Width (cm)")

ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(name = "Sepal Length (cm)")
```


#### 引数n.breaks, nice.breaks

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_jitter() +
  scale_x_binned(n.breaks = 4) +
  scale_y_binned(n.breaks = 5)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_jitter() +
  scale_x_binned(n.breaks = 4, nice.breaks = TRUE) +
  scale_y_binned(n.breaks = 5, nice.breaks = TRUE)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_jitter() +
  scale_x_binned(n.breaks = 4, nice.breaks = FALSE) +
  scale_y_binned(n.breaks = 5, nice.breaks = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0823fe1a-c4f3-55a8-c544-61caa1607b9c.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d924d480-2ab8-1f22-847f-9c8923615747.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(n.breaks = 4)
ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(n.breaks = 4, nice.breaks = TRUE)

ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(n.breaks = 4, nice.breaks = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/9f82ec56-802b-db45-a128-a5d4872808f9.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0413018c-6c42-3451-68f8-a7bba06dbd5b.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e6a4bde3-5aaa-aa16-af3b-fe15d2307377.png)


#### 引数show.limits

```R:R
ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(breaks = 4:8)
ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(breaks = 4:8,
                 show.limits = FALSE)

ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(breaks = 4:8,
                 show.limits = TRUE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/df877e43-55d7-2fd7-7653-5148c8694a13.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/7dc085d0-6def-4ea7-eda3-eae6e88243c7.png)


#### 引数breaks：軸の目盛り

```R:R
ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(breaks = 4:8)
ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(breaks = 4:8,
                 show.limits = FALSE)

ggplot(data = iris, aes(x = Sepal.Length)) +
  geom_bar(width = 0.9) +
  scale_x_binned(breaks = 4:8,
                 show.limits = TRUE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a3f7e77c-6f19-5b8f-06bf-e5d4a5a5e359.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ed0a8eb0-01d0-a71b-1451-3fa30616bab3.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_jitter() +
  scale_x_binned(breaks = 4:8,
                 show.limits = TRUE) +
  scale_y_binned(breaks = 2:5,
                 show.limits = TRUE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e356018b-7948-9f0e-345b-1e83fc2d3be1.png)


#### 引数limits：軸の範囲

````R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_jitter() +
  scale_x_binned(limits = c(4, 8)) +
  scale_y_binned(limits = c(2, 5))
````
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/6b0ede4f-a8db-c8eb-e8ba-7420783d08e9.png)


#### 引数labels：軸の目盛りのラベル

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_jitter() +
  scale_x_binned(breaks = 4:8,
                 limits = c(4, 8),
                 labels = str_c(4:8, "(cm)")) +
  scale_y_binned(breaks = 2:5,
                 limits = c(2, 5),
                 labels = str_c(2:5, "(cm)"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/bd15b27e-e34b-f065-fbc9-a10b069a328b.png)


#### 引数trans：軸のスケール変換

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_jitter() +
  scale_x_binned(trans = "log10") +
  scale_y_binned(trans = "sqrt")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/39ac4f57-af6f-f432-52e6-00425932efed.png)



## scale_color_\*(), scale_fill_\*(), scale_alpha_\*(), scale_size_\*(), scale_shape_\*(), scale_linetype_*()：color軸, fill軸, alpha軸, shape軸, size軸, linetype軸

ggplot2では、x軸・y軸の2次元以外に、color[^1]軸, fill軸なども`aes()`に指定することで設定できます。
これらの軸は、デフォルトでは、連続値のcolor軸はカラーバー表示、それ以外は通常の凡例表示になります。

[^1]: ggplot2のカラーの綴りは正確にはイギリス式の"colour"ですが、"color"と書いても基本的には動きます（一部例外があります。脚注2.参照。）。以下では"color"で統一します。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Petal.Width))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a3c517da-2b45-f441-b326-b30a96a44c40.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0940429f-ca04-8481-62c0-81cae7790443.png)


### scale_\*\_continuous()：連続値のcolor軸など
### scale_\*\_discrete()：離散値のcolor軸など
### scale_\*_binned()：連続値のcolor軸などのビン分割

x軸・y軸と同様に、color軸などの設定も、連続値か離散値かによって`scale_*_continuous()`, `scale_*_discrete()`（連続値をビン分割する場合は`scale_*_binned()`）を使います。

- `scale_*_continuous()`：連続（continuous）値の場合
- `scale_*_discrete()`：離散（discrete）値の場合
- `scale_*_binned()`：連続（continuous）値の場合（ビン分割）


#### 引数name：軸のラベル

`scale_x_continuous()`, `scale_x_discrete()`, `scale_x_binned()`などと同様です。`labs()`でも設定できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_x_continuous(name = "Sepal Length (cm)") +
  scale_y_continuous(name = "Sepal Width (cm)") +
  scale_color_continuous(name = "Petal Length (cm)")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  labs(x = "Sepal Length (cm)",
       y = "Sepal Width (cm)",
       color = "Petal Length (cm)")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_x_continuous(name = "Sepal Length (cm)") +
  scale_y_continuous(name = "Sepal Width (cm)") +
  scale_color_binned(name = "Petal Length (cm)")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_discrete(name = "SPECIES")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  labs(color = "SPECIES")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/56b9d0d2-cfff-6cc2-ef10-d99256a068e6.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ab9067f1-3200-97c9-53dc-daedf6e2519a.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e205c3c7-62e1-801c-4b33-2743b835fc9b.png)


#### 引数breaks：軸の目盛り

軸の変数が連続値の場合、`scale_x_continuous()`と同様に軸の目盛りを設定できます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_x_continuous(breaks = seq(4, 8, by = 0.5)) +
  scale_y_continuous(breaks = seq(2, 5, by = 0.5)) +
  scale_color_continuous(breaks = seq(0, 7, by = 0.5))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2ba698bf-142d-7313-7228-1bd132ac4cfa.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_x_continuous(breaks = NULL) +
  scale_y_continuous(breaks = NULL) +
  scale_color_continuous(breaks = NULL)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/6780522b-ebc6-faa6-d1cc-77fba2ca3370.png)

また、`breaks`は等間隔でなくてもかまいません。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(breaks = c(1, 2, 4, 8))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/56861288-5eeb-daf5-832e-5d0ec823691b.png)


#### 引数labels：軸の目盛りのラベル

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_x_continuous(breaks = 4:8,
                     labels = str_c(4:8, "(cm)")) +
  scale_y_continuous(breaks = 2:5,
                     labels = str_c(2:5, "(cm)")) +
  scale_color_continuous(breaks = 1:7,
                         labels = str_c(1:7, "(cm)"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/edb064bb-f9d5-e42a-3916-1878d188c4a9.png)


#### 引数limits：軸の範囲

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_x_continuous(limits = c(4, 8)) +
  scale_y_continuous(limits = c(2, 4.5)) +
  scale_color_continuous(limits = c(1, 7))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/4572e9ba-ce13-33be-c71f-0f46901eecbc.png)


#### 引数trans：軸のスケール変換

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(trans = "sqrt")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(trans = "reverse")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a7818c10-4dd3-681d-6af2-2236f73a09d1.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/629d00ce-8e1e-a259-50e9-64874831476f.png)


#### 引数guide：軸の凡例

凡例の表示の設定です。
デフォルトでは、連続値のcolor軸はカラーバー表示`guide = "colorbar"`、それ以外は通常の凡例表示`guide = "legend"`です。
`scale_*_discrete(guide = ...)`, `scale_*_continuous()`と`guides(* = ...)`は同じです。また、`guides()`の中身はまとめても書けます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width)) +
  scale_shape_discrete(guide = "legend") +  # デフォルト
  scale_color_discrete(guide = "legend") +  # デフォルト
  scale_size_continuous(guide = "legend") + # デフォルト
  scale_alpha_continuous(guide = "legend")  # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width)) +
  guides(shape = "legend") +  # デフォルト
  guides(color = "legend") +  # デフォルト
  guides(size  = "legend") +  # デフォルト
  guides(alpha = "legend")    # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width)) +
  guides(shape = "legend",  # デフォルト
         color = "legend",  # デフォルト
         size  = "legend",  # デフォルト
         alpha = "legend")  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Petal.Width)) +
  scale_shape_discrete(guide = "legend") +   # デフォルト
  scale_color_continuous(guide = "colorbar") # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Petal.Width)) +
  guides(shape = "legend") + # デフォルト
  guides(color = "colorbar") # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Petal.Width)) +
  guides(shape = "legend",   # デフォルト
         color = "colorbar") # デフォルト
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a3c517da-2b45-f441-b326-b30a96a44c40.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0940429f-ca04-8481-62c0-81cae7790443.png)

連続値のcolor軸も通常の通常の凡例表示`guide = "legend"`にできます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(guide = "legend")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = "legend")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d49cd6d9-3592-8b9a-7927-ba4c4b8b3232.png)


`guide = "none"`か`guide = FALSE`とすれば、凡例が表示されなくなります。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width)) +
  scale_shape_discrete(guide = "none") +
  scale_color_discrete(guide = "none") +
  scale_size_continuous(guide = NULL) +
  scale_alpha_continuous(guide = NULL)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width)) +
  guides(shape = "none") +
  guides(color = "none") +
  guides(size  = "none") +
  guides(alpha = "none")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width)) +
  guides(shape = "none",
         color = "none",
         size  = "none",
         alpha = "none")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/03cd2bad-b922-8836-5008-ea1e052f36e8.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width)) +
  guides(shape = "legend",
         color = "legend",
         size  = "none",
         alpha = "none")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width)) +
  guides(size  = "none",
         alpha = "none")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/415680a3-2a34-705b-c8de-f943a870002a.png)


さらに、`guide_legend()`関数, `guide_colorbar()`関数で詳細を設定できます。

##### guide_legend()：凡例を設定する関数

`scale_***_discrete(guide = guide_legend(... ))`と`guides(*** = guide_legend(... ))`は同じです。
`scale_***_continuous(guide = guide_legend(... ))`と`guides(*** = guide_legend(... ))`は同じです。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_discrete()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_discrete(guide = "legend")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_discrete(guide = guide_legend())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = "legend")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a1b639e9-4ebf-c2af-4218-158b968ac20b.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(guide = "legend")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(guide = guide_legend())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = "legend")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_legend())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/47fc1e51-dc0b-efe1-720d-0e678ce94af5.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(aes(fill = Petal.Length)) +
  guides(fill = "legend")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(aes(fill = Petal.Length)) +
  guides(fill = guide_legend())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/30b7640c-6286-3139-5582-592348b2fb17.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  scale_fill_continuous(guide = "legend")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  scale_fill_continuous(guide = guide_legend())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  guides(fill = "legend")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  guides(fill = guide_legend())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d10350cd-68b7-a338-8bfb-7d7c352faeea.png)


###### 引数reverse：凡例の軸の反転

凡例の軸の表示を反転させます。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend(reverse = FALSE))  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend(reverse = TRUE))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a1b639e9-4ebf-c2af-4218-158b968ac20b.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/7948950e-df02-78b8-1317-85ff96cbea8d.png)


```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_legend())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_legend(reverse = FALSE))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_legend(reverse = TRUE))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2ad445a6-e880-3414-cf8c-d471361773cb.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d7085cd5-cb06-aa05-1917-a8fcbac87084.png)


###### 引数nrow, ncol, byrow：凡例を何行何列に表示するか

凡例の表示を何行、何列で表示するかを指定します。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_legend(nrow = 3))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_legend(ncol = 3))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_legend(ncol = 2, byrow = TRUE))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/f0e6e627-8e7d-4697-1556-76f3e5bd965d.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/f7b09930-8023-ad90-e328-0b544e67413a.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/1cb9a268-d72b-170e-07f3-074604e375d6.png)


###### 引数title, title.position：凡例のタイトル

凡例のタイトルの設定です。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species,
                 color = Species,
                 size  = Petal.Width,
                 alpha = Petal.Width)) +
  guides(shape = guide_legend(title = "SPECIES"),
         color = guide_legend(title = "SPECIES"),
         size  = guide_legend(title = "PETAL WIDTH"),
         alpha = guide_legend(title = "PETAL WIDTH"))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend(title = "SPECIES",
                              title.position = "bottom"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0e6edeab-3de3-dfe4-2cd4-ab642af7833e.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/6a038808-e263-661c-be40-725509a03fed.png)


###### 引数label, label.position：凡例のラベル

凡例のラベルの設定です。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend(title = "", label = FALSE))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend(label.position = "left"))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend(title = "SPECIES:",
                              title.position = "top",
                              label.position = "top",
                              direction = "horizontal"))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend(title = "SPECIES:",
                              title.position = "top",
                              label.position = "top",
                              direction = "horizontal"))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  guides(color = guide_legend(title = "species:",
                              title.position = "top",
                              label.position = "bottom",
                              label.hjust = 0,
                              label.vjust = 1,
                              label.theme = element_text(angle = -90, size = 10),
                              direction = "horizontal"))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_discrete(guide = guide_legend(title = "species:",
                                            title.position = "top",
                                            label.position = "bottom",
                                            label.hjust = 0,
                                            label.vjust = 1,
                                            label.theme = element_text(angle = -90, size = 10),
                                            direction = "horizontal"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a3296258-964d-c2b0-11ed-9aec7be71490.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e1bc1f98-63f1-94a5-5427-3953e6daee73.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2ed78ef6-5dd6-9bad-6f0e-a34cec9c3383.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/819abb51-3d40-cbad-810e-6837343a5482.png)


##### guide_colorbar()：カラーバーを設定する関数

`scale_color_continuous(guide = guide_colorbar(... ))`と`guides(color = guide_colorbar(... ))`は同じです。
`scale_fill_continuous(guide = guide_colorbar(... ))`と`guides(fill = guide_colorbar(... ))`は同じです。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(guide = "colorbar")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(guide = guide_colorbar())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = "colorbar")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_colorbar())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d0d43232-0185-fd1e-9779-b05b3ec98047.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(aes(fill = Petal.Length))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(aes(fill = Petal.Length)) +
  scale_fill_continuous(guide = "colorbar")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(aes(fill = Petal.Length)) +
  scale_fill_continuous(guide = guide_colorbar())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(aes(fill = Petal.Length)) +
  guides(fill = "colorbar")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(aes(fill = Petal.Length)) +
  guides(fill = guide_colorbar())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/573f8753-381c-b52a-8029-19bc4c039f00.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  scale_fill_continuous(guide = "colorbar")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  scale_fill_continuous(guide = guide_colorbar())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  guides(fill = "colorbar")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  guides(fill = guide_colorbar())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/5b00eec3-6603-270a-5c04-9746f5360f81.png)


###### 引数barwidth, barheiht：カラーバーの大きさ

カラーバーの幅と高さの設定です。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_colorbar(barwidth = 2, barheight = 10))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/59594e49-63f6-389d-8ea6-90fcc5fc3973.png)


###### 引数ticks, ticks.colour, ticks.linewidth：カラーバーの目盛り

カラーバーの目盛りの設定です。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_colorbar(ticks = TRUE,            # デフォルト
                                ticks.colour = "white",  # デフォルト
                                ticks.linewidth = 0.5))  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_colorbar(ticks = TRUE,
                                ticks.colour = "orange",
                                ticks.linewidth = 3))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_colorbar(ticks = FALSE))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ad9cbe5a-e92b-14d2-3abb-72884e0b985c.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d0134e83-10d1-2df7-55a0-5d608029e565.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/7fc3fb2d-0d1a-03c8-b752-2fcd40b04e7b.png)


##### guide_bins()

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  guides(fill = guide_bins())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_bin2d(stat = "bin2d", binwidth = 0.5) +
  guides(fill = "bins")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/079ebd41-57dc-dd72-a7df-b7efb446cea5.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 4, 6)) +
  guides(color = "bins")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 4, 6)) +
  guides(color = guide_bins())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/5c53e2b7-3f51-5143-8620-be3a63a44888.png)


##### guide_coloursteps()

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 4, 6))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 4, 6)) +
  guides(color = "coloursteps")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 4, 6)) +
  guides(color = guide_coloursteps())
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0116e74a-22dd-1be9-6d9f-252b3e781b5f.png)


###### 引数even.steps

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 3, 5),
                     guide = guide_colorsteps())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 3, 5),
                     guide = "coloursteps")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 3, 5)) +
  guides(color = "coloursteps")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 3, 5)) +
  guides(color = guide_colorsteps())

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 3, 5),
                     guide = guide_colorsteps(even.steps = FALSE))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 3, 5)) +
  guides(color = guide_colorsteps(even.steps = FALSE))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 3, 5),
                     guide = guide_colorsteps(show.limits = TRUE, even.steps = FALSE))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 3, 5)) +
  guides(color = guide_colorsteps(show.limits = TRUE, even.steps = FALSE))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/55a33e20-0fef-6a82-9dbb-7100e0b80047.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/88735c4c-7dd1-6af7-7052-2784e66bf9ea.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e156a1a5-5efb-737d-7296-ec487d9c96f4.png)


###### 引数show.limits

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 4, 6),
                     guide= guide_bins())
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 4, 6)) +
  guides(color = guide_bins())

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 4, 6),
                     guide = guide_bins(show.limits = TRUE))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(breaks = c(2, 4, 6)) +
  guides(color = guide_bins(show.limits = TRUE))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/010faed1-2c9a-c641-930a-7595980042c7.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/4936cabe-19e5-a0f5-daaa-baa906a418ec.png)


###### 引数axis.arrow

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  guides(color = guide_bins(axis.arrow = arrow(length = unit(0.5, "npc"), ends = "both"),
                            show.limits = TRUE))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  theme(legend.position = "bottom") +
  guides(color = guide_bins(axis.arrow = arrow(length = unit(0.5, "npc"), ends = "both"),
                            show.limits = TRUE))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/94b372f1-896a-e69f-266b-eb7aae34ee9c.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/323ce37b-87c0-6700-95b3-a3a076eacf1c.png)


### scale_*_manual()：離散値のcolor軸などの手動設定

`scale_*_discrete()`ではcolorなどが自動的に割り振られますが、これを直接手動（マニュアル）で設定したい場合には`scale_*_manual()`を使います。

#### 引数values：手動設定したい値の指定

`values`に設定したい値からなるベクトルを与えることで設定します。
名前付きベクトルの形で与えると、名前に対してベクトルの値が設定されます。

```R:R
c("setosa" = "red", "versicolor" = "green", "virginica" = "blue")
#     setosa versicolor  virginica 
#      "red"    "green"     "blue"

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_manual(values = c("red", "green", "blue"))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_manual(values = c("setosa"     = "red",
                                "versicolor" = "green",
                                "virginica"  = "blue"))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_manual(values = c("setosa"     = 2,  # red
                                "versicolor" = 3,  # green
                                "virginica"  = 4)) # blue
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_manual(values = c("versicolor" = "green",
                                "virginica"  = "blue",
                                "setosa"     = "red"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0824f41e-b080-8e43-5143-dc4af399a862.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(alpha = Species, size = Species)) +
  scale_alpha_manual(values = c("setosa"     = 3/5,
                                "versicolor" = 2/5,
                                "virginica"  = 1/5)) +
  scale_size_manual(values = c("setosa"     = 2,
                               "versicolor" = 4,
                               "virginica"  = 6))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/9e51706f-c977-360f-daa6-b70f58f41ac9.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species)) +
  scale_shape_manual(values = c("setosa"     = 0,
                                "versicolor" = 1,
                                "virginica"  = 2))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e019a086-a7b2-20ee-b8a5-b692a97ebf1f.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_path(aes(linetype = Species)) +
  scale_linetype_manual(values = c("setosa"     = "dotted",
                                   "versicolor" = "solid",
                                   "virginica"  = "dashed"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/18624f02-ec00-20da-5065-9ac8bdd3ed90.png)


### scale_*_identity()：color軸などのデータの値をそのまま設定

`scale_*_discrete()`ではcolorなどが自動的に割り振られますが、これをデータの値そのままに設定したい場合には`scale_*_identity()`を使います。

まず、データを用意しておきます。irisデータに次の列を追加しています。
・col列：color軸に設定したい色
・lty列：linetype軸に設定したい線の種類
・s列  ：size軸に設定したいサイズ
・a列  ：alpha軸に設定したい透過度

```R:R
iris_2 <- iris %>% as_tibble() %>% 
  mutate(col = case_when(Species == "setosa"     ~ "red",
                         Species == "versicolor" ~ "green",
                         Species == "virginica"  ~ "blue"),
         lty = case_when(Species == "setosa"     ~ "dotted",
                         Species == "versicolor" ~ "solid",
                         Species == "virginica"  ~ "dashed"),
         s = round(Petal.Length),
         a = (8 - s) / 10) %>% 
  print()
# # A tibble: 150 x 9
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species col   lty        s     a
#           <dbl>       <dbl>        <dbl>       <dbl> <fct>   <chr> <chr>  <dbl> <dbl>
#  1          5.1         3.5          1.4         0.2 setosa  red   dotted     1   0.7
#  2          4.9         3            1.4         0.2 setosa  red   dotted     1   0.7
#  3          4.7         3.2          1.3         0.2 setosa  red   dotted     1   0.7
#  4          4.6         3.1          1.5         0.2 setosa  red   dotted     2   0.6
#  5          5           3.6          1.4         0.2 setosa  red   dotted     1   0.7
#  6          5.4         3.9          1.7         0.4 setosa  red   dotted     2   0.6
#  7          4.6         3.4          1.4         0.3 setosa  red   dotted     1   0.7
#  8          5           3.4          1.5         0.2 setosa  red   dotted     2   0.6
#  9          4.4         2.9          1.4         0.2 setosa  red   dotted     1   0.7
# 10          4.9         3.1          1.5         0.1 setosa  red   dotted     2   0.6
# # ... with 140 more rows
```

color軸の変数に色の値が入ったcol列を指定して`scale_color_identity()`を使います。
なお、`scale_color_identity()`はデフォルトで`guide = "none"`（凡例を表示しない）ですので、`guide = "legend"`として凡例を表示しています。
また、`geom_point(aes(color = col)) + scale_color_identity()`は、`geom_point(aes(color = I(col)))`と同じです。この`I()`はcol列の値をそのままcolor軸に設定する（identity）ものです。
変数として`aes`に入れずに、`geom_point()`の引数`color`にデータと同じ個数の値を指定しても同じです（`color = iris_2$col`）。

```R:R
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = col)) +
  scale_color_identity(guide = "legend")

ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = col)) +
  scale_color_identity(guide = "none")  # デフォルト
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = col)) +
  scale_color_identity()
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = I(col)))
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(color = iris_2$col)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d8e9c86a-6971-09f8-6409-24d385c77678.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/5a5fbaf9-e183-7e03-2c65-8ab8958395c3.png)

```R:R
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_path(aes(linetype = lty)) +
  scale_linetype_identity(guide = "legend")

ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_path(aes(linetype = lty)) +
  scale_linetype_identity(guide = "none")
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_path(aes(linetype = lty)) +
  scale_linetype_identity()
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_path(aes(linetype = I(lty)))
# ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
#   geom_path(linetype = iris_2$lty)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/82eee765-a811-761e-d2f8-9114e226d848.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/17ffe98b-1ea5-8e9a-da7a-24a7b445f1b3.png)

```R:R
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(size = s)) +
  scale_size_identity(guide = "legend")

ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(size = s)) +
  scale_size_identity(guide = "none")
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(size = s)) +
  scale_size_identity()
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(size = I(s)))
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(size = iris_2$s)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e1345916-6c92-d062-a12c-a9eecddce846.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0d78b8a3-6735-f859-926d-1a310b4faddd.png)

```R:R
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(alpha = a)) +
  scale_alpha_identity(guide = "legend")

ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(alpha = a)) +
  scale_alpha_identity(guide = "none")
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(alpha = a)) +
  scale_alpha_identity()
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(alpha = I(a)))
ggplot(data = iris_2, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(alpha = iris_2$a)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e4c46ecb-b8b1-c05b-d954-db0b734fe16e.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/1f138087-724a-ec8a-ac5a-f4b79c65ecaf.png)



## scale_color_\*(), scale_fill_\*()：color軸, fill軸

引数`name`は共通して使えます。

色を反転するには、`scale_*_gradient()`のように色を直接指定するものは指定する色を反転すればよく、`scale_*_distiller()`のようにカラーパレットを指定するものは`direction = -1`で反転できます。また、`scale_*_continuous()`等は`trans = "reverse"`で反転できます。


### scale_color_gradient(), scale_fill_gradient()：連続値のcolor軸, fill軸
### scale_color_steps(), scale_fill_steps()：連続値のcolor軸, fill軸のビン分割

2色 (low-high) のグラデーションの色スケールです。

- `scale_*_gradient()`：連続値のcolor軸, fill軸をグラデーションで
- `scale_*_steps()`：連続値のcolor軸, fill軸をビン分割してグラデーションで

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradient()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradient(low = "#132B43", high = "#56B1F7")  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradient(low = "#56B1F7", high = "#132B43")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradient(low = "green", high = "red")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ad9cbe5a-e92b-14d2-3abb-72884e0b985c.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/0e1c2ec1-82de-a2fa-2317-c75386ff88ba.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/bff8c065-197a-7ece-01fc-1d3b732ca578.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_steps()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_steps(low = "#132B43", high = "#56B1F7")  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_steps(low = "green", high = "red")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/380cd3d9-210e-4ac7-2bd3-d174f9e3c10e.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/410b011b-c488-44e9-0e7f-4d64cf9d90fa.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(stat = "density_2d", contour = FALSE, n = 50, aes(fill = ..density..))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(stat = "density_2d", contour = FALSE, n = 50, aes(fill = ..density..)) +
  scale_fill_gradient()

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(stat = "density_2d", contour = FALSE, n = 50, aes(fill = ..density..)) +
  scale_fill_steps()

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(stat = "density_2d", contour = FALSE, n = 50, aes(fill = ..density..)) +
  scale_fill_steps(breaks = seq(0, 0.5, by = 0.05))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ff348cbd-ba21-b8a9-2b57-def3c13b690b.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d85e828d-9e71-66f8-2e0d-1ae5833afe34.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/48dbdb7e-4da7-cebd-4467-2d6ba373a7cb.png)


### scale_color_gradient2(), scale_fill_gradient2()：連続値のcolor軸, fill軸
### scale_color_steps2(), scale_fill_steps2()：連続値のcolor軸, fill軸のビン分割

3色 (low-mid-high) のグラデーションの色スケールです。

- `scale_*_gradient2()`：連続値のcolor軸, fill軸をグラデーションで
- `scale_*_steps2()`：連続値のcolor軸, fill軸をビン分割してグラデーションで

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradient2(low = "blue", mid = "lightgray", high = "red",
                        midpoint = 4)

library(scales)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradient2(low = muted("blue"), mid = "white", high = muted("red"),
                        midpoint = 4)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e4535d07-787e-7643-1c71-0e2504d4e767.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/7ad04f14-53cf-200c-38c1-76eed8f90864.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_steps2(low = "blue", mid = "lightgray", high = "red",
                     midpoint = 4)

library(scales)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_steps2(low = muted("blue"), mid = "white", high = muted("red"),
                     midpoint = 4)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ed2471f7-85c1-3d0d-6452-7ffe744708c1.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/3b59d055-3438-4b20-b031-20c192d19464.png)


### scale_color_gradientn(), scale_fill_gradientn()：連続値のcolor軸, fill軸
### scale_color_stepsn(), scale_fill_stepsn()：連続値のcolor軸, fill軸のビン分割

n色のグラデーションの色スケールです。

- `scale_*_gradientn()`：連続値のcolor軸, fill軸をグラデーションで
- `scale_*_stepsn()`：連続値のcolor軸, fill軸をビン分割してグラデーションで

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradientn(colors = rainbow(7))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradientn(colors = rev(rainbow(7)))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradientn(colors = heat.colors(7))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradientn(colors = cm.colors(7))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradientn(colors = terrain.colors(7))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_gradientn(colors = topo.colors(7))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/fc1e5008-5153-6a60-5118-05d038c3b55b.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/3f740898-3b0d-a6a5-a3b0-c37745d797f4.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e80e2041-1535-c170-897c-88b552b3c817.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/44c77b48-29f7-ff65-7689-1c9484477612.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/df3557a3-3826-0047-2e4d-d36cf793e949.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/5e9ac6ea-2fe6-ce55-65c4-de4d3b1fc5cb.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_stepsn(colors = rainbow(7))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_stepsn(colors = heat.colors(7))
````
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/835ce09d-bb78-83a7-fd5e-a2b059d5add0.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/8b8578f0-bd78-125b-391a-1b1968774cea.png)


### scale_color_distiller(), scale_fill_distiller()：連続値のcolor軸, fill軸
### scale_color_fermenter(), scale_fill_fermenter()：連続値のcolor軸, fill軸のビン分割

- `scale_*_distiller()`：連続値のcolor軸, fill軸をColorBrewerのパレットで
- `scale_*_fermenter()`：連続値のcolor軸, fill軸をビン分割してColorBrewerのパレットで

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_distiller()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_distiller(palette = "Blues")  # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_distiller(palette = "Blues", direction = -1)  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_distiller(palette = "Blues", direction = 1)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_distiller(palette = "Greens")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_distiller(palette = "Set1")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_distiller(palette = "Spectral")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e8c0d8ac-93f6-c932-7dd5-80025e5b5baa.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/1df56ab6-f2b8-4098-ebab-c4cf7d576586.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/063fc84d-1c5b-29cf-1935-0cb880a35da5.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/7ae97254-5b1e-4db6-9534-7f7decf607ee.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d8f8d46d-9489-2ace-ce22-fe493d5a67c8.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_fermenter()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_fermenter(palette = "Blues")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_fermenter(palette = "Blues", direction = -1)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_fermenter(palette = "Greens")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/c0c5d624-2169-97b1-565c-3305834f5b58.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/6998cded-0722-34a2-1026-ea2a7c25f57a.png)


### scale_color_continuous(), scale_fill_continuous()：連続値のcolor軸, fill軸
### scale_color_binned(), scale_fill_binned()：連続値のcolor軸, fill軸のビン分割

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(type = "gradient")  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_continuous(type = "viridis")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c()
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ad9cbe5a-e92b-14d2-3abb-72884e0b985c.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a94d9a4b-de3a-fe3a-2704-e83fd0887973.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(type = "gradient")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_binned(type = "viridis")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_colour_viridis_b()
# ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
#   geom_point(aes(color = Petal.Length)) +
#   scale_color_viridis_b()
```
注）`scale_color_viridis_c()`と`scale_color_viridis_d()`はあるが、`scale_color_viridis_b()`だけなぜか定義されていない。
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/08db7c7f-bfff-23c7-ca5b-1a3f3b613f62.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2a1b1146-db0b-93bf-c77e-411bb2fb9eb2.png)


### scale_color_viridis_c(), scale_fill_viridis_c()：連続値のcolor軸, fill軸
### scale_colour_viridis_b(), scale_fill_viridis_b()：連続値のcolor軸, fill軸のビン分割

- `scale_*_viridis_c()`：連続値のcolor軸, fill軸をviridisLiteのパレットで
- `scale_*_viridis_b()`：連続値のcolor軸, fill軸をビン分割してviridisLiteのパレットで

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "D")  # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "viridis")  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "A")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "magma")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "B")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "inferno")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "C")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "plasma")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "E")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(option = "cividis")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(alpha = 0.5)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_color_viridis_c(direction = -1)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a94d9a4b-de3a-fe3a-2704-e83fd0887973.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/866d9caa-6d82-efda-047a-4c64b672be86.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/07d8a039-98c3-fc9d-a4b3-d1ceab2b28d1.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/4026479c-8372-1be5-2805-836b94ef9e89.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/80b9beb8-6564-963a-b96b-36db1dfa105b.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/8d6f0ac1-9318-5888-e161-e619eb92679b.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/9546f852-acc0-6b85-a7e9-3d816fbfe8eb.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_colour_viridis_b()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Petal.Length)) +
  scale_colour_viridis_b(option = "D")  # デフォルト
```
注）`scale_color_viridis_c()`と`scale_color_viridis_d()`はあるが、`scale_color_viridis_b()`だけなぜか定義されていない[^2]。
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2a1b1146-db0b-93bf-c77e-411bb2fb9eb2.png)

[^2]: ggplot2の"colour"は"color"と書いても基本的には動きますが、`scale_color_viridis_b()`だけは例外のようです。）

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(stat = "density_2d", contour = FALSE, n = 50, aes(fill = ..density..)) +
  scale_fill_viridis_c()

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_tile(stat = "density_2d", contour = FALSE, n = 50, aes(fill = ..density..)) +
  scale_fill_viridis_b()
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/4881bde6-ed45-54f2-8a4e-3287b366e7ad.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/9de79f48-9619-8c0a-c691-2f0e2da13f4c.png)


### scale_color_brewer(), scale_fill_brewer()：離散値のcolor軸, fill軸

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer(palette = "Blues")
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer(palette = 1)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer(palette = 1, direction = 1)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer(palette = "Blues", direction = -1)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer(palette = "Greens")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer(palette = "Set1")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer(palette = "Set2")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer(palette = "Set3")

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_brewer(palette = "Accent")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/6c767d1b-b40c-1440-88e1-ef04ad6d78c8.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/8062295f-1edb-a4f4-d584-cb84f144e1e6.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/b0d62fa1-a09c-e187-340c-ac2c7f4dbcff.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/877ed52d-e0d0-f871-090d-72e4da003ffa.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/d8e682c9-21b2-22dd-1390-115a503f3796.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/9ea223c1-a04a-d6e7-08b3-37e0a7860299.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/175188fc-4d7f-e839-072e-80f376048129.png)


### scale_color_grey(), scale_fill_grey()：離散値のcolor軸, fill軸

- `scale_*_grey()`：離散値のcolor軸, fill軸をグレースケールで

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_grey()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_grey(start = 0.2, end = 0.8)  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_grey(start = 0.8, end = 0.2)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/299336a5-2f9c-44d4-0832-e2232b180e16.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e4c2ef9b-25f1-1715-c5cb-5b7fdb550ce4.png)


### scale_color_hue(), scale_fill_hue()：離散値のcolor軸, fill軸

- `scale_*_hue()`：離散値のcolor軸, fill軸をデフォルト色スケールで

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(h = c(0, 360) + 15,
                  c = 100,
                  l = 65,
                  direction = 1)  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(direction = -1)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(l = 40, c = 30)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(l = 70, c = 30)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(l = 70, c = 150)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(l = 80, c = 150)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(h = c(0, 90))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(h = c(90, 180))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(h = c(180, 270))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_hue(h = c(270, 360))

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = factor(Petal.Length))) +
  scale_color_hue()
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a1b639e9-4ebf-c2af-4218-158b968ac20b.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2d8cf9d9-c7ff-ed73-1db1-9cc74db78b9b.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/10759e10-fbfd-bee9-1c25-9d2cecf82517.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/02ff2eb9-8e5d-3e33-aac2-ecf2c0b8853e.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/c6dc60a6-c941-6574-cb83-543a8235b927.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/4ce741c7-a2ab-924e-20f9-e14ec147fa8c.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/05f9ecc8-1c7c-da09-6228-c964cabc4026.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/b311a572-a1d6-0678-5ad6-4a51ec0afcc0.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/3fd1bbc6-95c2-6b61-1abe-2029859cfedc.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2c67dde4-a977-75ab-3bb7-10052c828061.png)


### scale_color_discrete(), scale_fill_discrete()：離散値のcolor軸, fill軸

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species))
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_discrete()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_discrete(type = getOption("ggplot2.discrete.colour"))
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a1b639e9-4ebf-c2af-4218-158b968ac20b.png)


### scale_color_viridis_d(), scale_fill_viridis_d()：離散値のcolor軸, fill軸

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_viridis_d()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(color = Species)) +
  scale_color_viridis_d(alpha = 1, begin = 0, end = 1, direction = 1,
                        option = "D")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/8578b4ad-e0bf-dc47-afd5-1862ca7924ca.png)



## scale_shape_\*()：shape軸

### scale_shape_discrete(), scale_shape_binned()：離散値のshape軸, 連続値のshape軸のビン分割

#### 引数solid：shapeの塗りつぶし

shapeを塗りつぶすかどうかを指定します。デフォルトは`solid = TRUE`で塗りつぶします。

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species)) +
  scale_shape_discrete()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species)) +
  scale_shape_discrete(solid = TRUE)  # デフォルト

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Species)) +
  scale_shape_discrete(solid = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/138a5f1a-53d2-3265-0cef-d1fec854a507.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/17a6d377-e08e-93ba-984e-e456ef0a0cdd.png)

```R:R
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Petal.Length)) +
  scale_shape_binned()  # デフォルト
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Petal.Length)) +
  scale_shape_binned(solid = TRUE)

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(shape = Petal.Length)) +
  scale_shape_binned(solid = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/301c9ad5-dc37-888d-4123-5e18cbdd857c.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/1725c497-cd42-256e-75f3-91aed7de3dcf.png)



# まとめ

`scale_*()`関数を一覧にしておきます。

|軸|連続値|連続値のビン分割|離散値|離散値の手動設定|値そのまま|
|:-:|:-|:-|:-|:-|:-|
|x|scale_x_<BR>continuous()|scale_x_<BR>binned()|scale_x_<BR>discrete()|||
|y|scale_y_<BR>continuous()|scale_y_<BR>binned()|scale_y_<BR>discrete()|||
|color|scale_color_<BR>continuous()|scale_color_<BR>binned()|scale_color_<BR>discrete()|scale_color_<BR>manual()|scale_color_<BR>identity()|
|colour|scale_colour_<BR>continuous()|scale_colour_<BR>binned()|scale_colour_<BR>discrete()|scale_colour_<BR>manual()|scale_colour_<BR>identity()|
|fill|scale_fill_<BR>continuous()|scale_fill_<BR>binned()|scale_fill_<BR>discrete()|scale_fill_<BR>manual()|scale_fill_<BR>identity()|
|alpha|scale_alpha_<BR>continuous()|scale_alpha_<BR>binned()|scale_alpha_<BR>discrete()|scale_alpha_<BR>manual()|scale_alpha_<BR>identity()|
|size|scale_size_<BR>continuous()|scale_size_<BR>binned()|scale_size_<BR>discrete()|scale_size_<BR>manual()|scale_size_<BR>identity()|
|shape||scale_shape_<BR>binned()|scale_shape_<BR>discrete()|scale_shape_<BR>manual()|scale_shape_<BR>identity()|
|linetype||scale_linetype_<BR>binned()|scale_linetype_<BR>discrete()|scale_linetype_<BR>manual()|scale_linetype_<BR>identity()|

color軸, colour軸, fill軸にはさらに次の関数もあります。引数も書いておきます。

||連続値|連続値のビン分割|離散値|
|:-|:-|:-|:-|
|（再掲）|scale_*_<BR>continuous(type, <BR><b>trans = "reverse"</b>, )|scale_*_<BR>binned(type, <BR><b>trans = "reverse"</b>, )|scale_*_<BR>discrete(type, <BR><b>trans = "reverse"</b>, )|
|2色のグラデーション<BR>(low-high)|scale_*_<BR>gradient(low, high, )|scale_*_<BR>steps(low, high, )||
|3色のグラデーション<BR>(low-mid-high)|scale_*_<BR>gradient2(low, mid, high,<BR> midpoint, )|scale_*_<BR>steps2(low, high, mid,<BR> midpoint, )||
|n色のグラデーション|scale_*_<BR>gradientn(colors, )|scale_*_<BR>stepsn(colors, )||
|グレースケール|||scale_*_<BR>grey(start, end, )|
|色相hue|||scale_*_<BR>hue(h, c, l, <BR><b>direction = -1</b>, )|
|ColorBrewerパレット|scale_*_<BR>distiller(palette, <BR><b>direction = -1</b>, )|scale_*_<BR>fermenter(palette, <BR><b>direction = -1</b>, )|scale_*_<BR>brewer(palette, <BR><b>direction = -1</b>, )|
|viridisLiteパレット|scale_*_<BR>viridis_c(option, <BR><b>direction = -1</b>, )|scale_*_<BR>viridis_b(option, <BR><b>direction = -1</b>, )|scale_*_<BR>viridis_d(option, <BR><b>direction = -1</b>, )|
注）<b>太字</b>は色の反転



# 参考文献
- https://ggplot2.tidyverse.org/
    - https://ggplot2.tidyverse.org/reference/index.html
- https://github.com/tidyverse/ggplot2
    - https://github.com/rstudio/cheatsheets/blob/master/data-visualization-2.1.pdf
- https://cran.r-project.org/web/packages/ggplot2/index.html
    - https://cran.r-project.org/web/packages/ggplot2/readme/README.html
- https://rstudio.cloud/learn/cheat-sheets
- https://ggplot2-book.org/index.html
    - https://ggplot2-book.org/scales.html 
- https://r-graphics.org/
- https://r4ds.had.co.nz/data-visualisation.html
    - https://r4ds.had.co.nz/graphics-for-communication.html
- https://ropensci.github.io/plotly/ggplot2/index.html
- https://heavywatal.github.io/rstats/ggplot2.html
- https://yukiyanai.github.io/jp/classes/stat2/contents/R/intro-to-ggplot2.html
- https://www.jaysong.net/RBook/
    - https://www.jaysong.net/RBook/visualization1.html
    - https://www.jaysong.net/RBook/visualization2.html
    - https://www.jaysong.net/RBook/visualization3.html
    - https://www.jaysong.net/RBook/visualization4.html
- https://kazutan.github.io/fukuokaR11/intro_ggplot2.html
- https://triadsou.hatenablog.com/entry/20100528/1275042816
- https://qiita.com/swathci/items/b36e493ea78b03db0981
- https://qiita.com/swathci/items/b08496d863bca4b479b3