視覚属性の変更

ggplot2 を使ったデータ可視化入門

Rick Scavetta

Founder, Scavetta Academy

位置調整

重なりの調整

  • identity
  • dodge
  • stack
  • fill
  • jitter
  • jitterdodge
  • nudge
ggplot2 を使ったデータ可視化入門

position = "identity" (デフォルト)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point()

ggplot2 を使ったデータ可視化入門

position = "identity" (デフォルト)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "identity")

ggplot2 を使ったデータ可視化入門

position = "jitter"

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter")

ggplot2 を使ったデータ可視化入門

position_jitter()

posn_j <- position_jitter(0.1)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 col = Species)) +
  geom_point(position = posn_j)

ggplot2 を使ったデータ可視化入門

position_jitter()

posn_j <- position_jitter(0.1, 
                          seed = 136)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = posn_j)
  • 位置調整のための引数を設定する
  • プロット間・レイヤー間の一貫性

ggplot2 を使ったデータ可視化入門

スケール関数

  • scale_x_*()
  • scale_y_*()
  • scale_color_*()
    • scale_colour_*() もあります。
  • scale_fill_*()
  • scale_shape_*()
  • scale_linetype_*()
  • scale_size_*()
ggplot2 を使ったデータ可視化入門

スケール関数

  • scale_x_continuous()
  • scale_y_*()
  • scale_color_discrete()
    • 別の表記として、scale_colour_*() も使えます。
  • scale_fill_*()
  • scale_shape_*()
  • scale_linetype_*()
  • scale_size_*()
ggplot2 を使ったデータ可視化入門

scale**()

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length") +
  scale_color_discrete("Species")

ggplot2 を使ったデータ可視化入門

limits 引数

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2,8)) +
  scale_color_discrete("Species")

ggplot2 を使ったデータ可視化入門

breaks 引数

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2, 8), 
                     breaks = seq(2, 8, 3)) +
  scale_color_discrete("Species")

ggplot2 を使ったデータ可視化入門

expand 引数

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2, 8), 
                     breaks = seq(2, 8, 3),
                     expand = c(0, 0)) +
  scale_color_discrete("Species")

ggplot2 を使ったデータ可視化入門

labels 引数

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2, 8), 
                     breaks = seq(2, 8, 3),
                     expand = c(0, 0), 
                     labels = c("Setosa", 
                               "Versicolor", 
                               "Virginica")) +
  scale_color_discrete("Species")

ggplot2 を使ったデータ可視化入門

labs()

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) +
  geom_point(position = "jitter") +
  labs(x = "Sepal Length", 
       y = "Sepal Width", 
       color = "Species")

ggplot2 を使ったデータ可視化入門

具体的な使い方

ggplot2 を使ったデータ可視化入門

Preparing Video For Download...