R言語による医療データ分析

R言語によるデータ分析のオンラインコースを中心に、さまざまなデータ分析について記載してあります。

050-052 ggplotでの色の指定

まとめ一覧

  • 演習問題で作ったグラフを塗り分けて、より「意味を感じられる」グラフを作ってみましょう。
library(ggplot2)
  • Q1: ダイヤモンドの重さと値段の関係を、散布図で描画してください。
ggplot(data = diamonds) + 
  geom_point(mapping = aes(x = carat, y = price))
  • 何となく右肩上がりですが、同じ重さでも、値段に相当な開きがありそうです。カットが影響している???
ggplot(data = diamonds) + 
  geom_point(mapping = aes(x = carat, 
                           y = price, 
                           color = cut))
  • ??? 透明度は?
ggplot(data = diamonds) +
  geom_point(mapping = aes(x = carat,
                           y = price,
                           color = clarity))
  • なんか意味ありそうですね。 色もみて見ましょう
ggplot(data = diamonds) + 
  geom_point(mapping = aes(x = carat,
                           y = price,
                           color = color))
  • こっちも関係ありそう。

  • 補足、mappingは、実はggplotの中に記載してもOKで、+以降のgeom関数にその効果は続きます。なので、うえの例のようにほぼ同じグラフをたくさん書いて探索的なデータ可視化を行う場合は、次のように書いてもOKです

gg <- ggplot(data = diamonds, mapping = aes(x = carat,
                                            y = price))

gg + geom_point()
gg + geom_point(mapping = aes(color = cut))
gg + geom_point(mapping = aes(color = clarity))
  • さらに、argumentが指定されていない場合、ヘルプファイルの順番通りに記載されていると解釈されるため、data= mapping=部分は省略できるため、
gg <- ggplot(diamonds, aes(carat, price))
gg + geom_point()
gg + geom_point(aes(color = cut))

という風に簡略できます。

  • Q2: ダイヤモンドの色と値段の関係を、箱ひげ図で描画してください。
library(ggplot2)

ggplot(data =diamonds) +
  geom_boxplot(mapping = aes(x = color, y = price))
  • これも、色と値段に何かしらの関係がありそうです。他の変数で色分けしてみましょう
ggplot(data = diamonds) +
    geom_boxplot(mapping = aes(x = color, 
                               y = price, 
                               color = cut))
 
ggplot(data = diamonds) +
   geom_boxplot(mapping = aes(x = color,
                              y = price,
                              color = clarity))
  • colorでなくてfillを使うと、
ggplot(data = diamonds) +
  geom_boxplot(mapping = aes(x = color, 
                             y = price, 
                             fill = cut))

ggplot(data = diamonds) +
  geom_boxplot(mapping = aes(x = color,
                             y = price,
                             fill = clarity))
  • こんな書き方もあります
gg <- ggplot(diamonds, aes(color, price))

gg + geom_boxplot(aes(color=cut))
gg + geom_boxplot(aes(color=clarity))

gg + geom_boxplot(aes(fill=cut))
gg + geom_boxplot(aes(fill=clarity))

gg + geom_boxplot(aes(fill=clarity, color = clarity))         
  • Q3: ダイヤモンドの透明度と色の関係を、何らかの形で描画してください
gdia <- ggplot(data = diamonds)

gdia + geom_count(aes(clarity, color))
gdia + geom_jitter(aes(clarity, color))

gdia + geom_jitter(aes(clarity, color, color = cut))
gdia + geom_jitter(aes(clarity, color, color = clarity))

gdia + geom_jitter(aes(clarity, color, color = carat)) 
  • Q4: ダイヤモンドの値段の分布をヒストグラムにして描画してください
gdia <- ggplot(data = diamonds)

gdia + geom_histogram(aes(price))

gdia + geom_histogram(aes(price, color = cut)) #???
gdia + geom_histogram(aes(price, fill = cut))
gdia + geom_histogram(aes(price, fill = clarity))
  • Q5: ダイヤモンドのカットの質が分類毎に、このデータセットに何件ずつあるのかを描画してください。
gdia + geom_bar(aes(cut))

gdia + geom_bar(aes(cut, fill = clarity))
gdia + geom_bar(aes(cut, fill = price)) #これはだめです

gdia + geom_bar(aes(cut, fill = clarity))  #これを種類毎に分けたい場合はどうすればいでしょうか?
  • positionというオプションがあるgeom_XXXでは、次のようなことができます。
gdia + geom_bar(mapping = aes(cut, fill=clarity), 
                position = "dodge") 
   #dodgeはよけるという意味です。
gdia + geom_bar(aes(cut, fill=clarity),
                position = "stack")
gdia + geom_histogram(aes(price, fill = clarity), 
                      position = "dodge")
  • Q6: 米国の失業者数の推移を何らかの形で描画してください。
economics
geco <- ggplot(economics) 

geco + geom_line(aes(date, unemploy,color = pop))

geco + geom_line(mapping = aes(date,unemploy),
                 color = "red",
                 size = 1.5,
                 linetype = "dashed")

まとめ一覧