まずは蝋の翼から。

学んだことを書きながら確認・整理するためのメモブログ。こういうことなのかな?といったことをふわっと書いたりしていますが、理解が浅いゆえに的はずれなことも多々あると思うのでツッコミ歓迎

ggplot2覚書⑥ theme詳細 凡例の設定

引き続き「Rグラフィッククックブック」。

過去記事は以下。

ggplot2覚書① 棒グラフ - まずは蝋の翼から。

ggplot2覚書② 散布図 - まずは蝋の翼から。

ggplot2覚書③ 注釈(解釈補助) - まずは蝋の翼から。

ggplot2覚書④ 軸について - まずは蝋の翼から。

ggplot2覚書⑤ 体裁(theme、テキストgeom)に関しての大枠 - まずは蝋の翼から。

凡例を消す

凡例を消すには、色々な方法があるが、themeを用いる場合はegend.position="none"にする。

# 基本プロット(凡例つき) 
g1 <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + 
  geom_boxplot()

g2 <- g1 + 
  theme(legend.position="none")

g1 | g2

f:id:chito_ng:20190223181932p:plain

また、位置の指定は同様にlegend.positionでおこなうことができる。
ベクトル指定c(x, y)の場合は直接位置の指定ができ、グラフ領域においてxは0が最左、1が最右、yは0が最下、1が最上となる0~0.5で指定する。このとき、凡例の中心部に対しての位置指定となっているがlegend.justificationで指定される位置を指定できる(デフォルトはc(0.5, 0.5))。

例えば、legend.justification = c(1,0), legend.position = c(1,0)だと、凡例の右下部分(justification)をグラフ領域の右下に位置指定(position)という意味合いになる。

g <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + 
  geom_boxplot() + scale_fill_brewer(palette="Pastel2")

g1 <- g +
  theme(legend.position="top")

g2 <- g +
  theme(legend.position=c(1,0)) #グラフ領域内の右下

g3 <- g +
  theme(legend.position=c(1,0), 
        legend.justification=c(1,0)) #legend.positionがどこを指すか

g1 | g2 | g3

f:id:chito_ng:20190223182924p:plain

凡例名を変える

軸やタイトルのとき同様にlabsで変更したい凡例項目(color, shape, fillなど)を指定して変える。
このとき、凡例が同じマッピングを使用している場合かつ同じ名前であれば凡例は結合される。

hw1 <- ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex)) +
  geom_point()

hw1 +
  labs(shape="Male/Female", colour="Male/Female")

f:id:chito_ng:20190223184318p:plain

凡例の順番を変える

凡例はデフォルトではfactorの場合レベル順、characterの場合はアルファベット順になる。
変更するとき、グラフの並びは変えないで凡例のみ変えることはできるが可読性が落ちるので基本的にデータをfactorにしてlevelを変えることで対応する方が良さそう。

sample_char <- c("B", "A", "C")
x   <- c("x1", "x2", "x3")
y   <- c(100,200,300)

d_char <- data.frame(sample = sample_char, x = x, y = y)

g1 <- ggplot(d_char, aes(x = x, y = y, fill = sample)) +
  geom_bar(stat = "identity", position = "dodge") + 
  labs(title = "char")

sample_factor <- c("B", "A", "C")

sample_factor <- as.factor(sample_char)

sample_factor #アルファベット順でlevel
# = >
# B A C
#Levels: A B C

d_factor <- data.frame(sample = sample_factor, x = x, y = y)

g2 <- ggplot(d_factor, aes(x = x, y = y, fill = sample)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "factor(default level = alphabet)") 


sample_factor2 <- factor(sample_factor, levels = c("B","A","C"))

sample_factor2 #指定順でlevel
# = >
# B A C
#Levels: B A C

d_factor2 <- data.frame(sample = sample_factor2, x = x, y = y)

g3 <- ggplot(d_factor2, aes(x = x, y = y, fill = sample)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "factor2(designated level)") 
  
g1 / g2 / g3

f:id:chito_ng:20190223220543p:plain

tidy的に使うにはmutateでlevel付きfactorに変換して処理した方がよさげ。

sample_char <- c("B", "A", "C")
x   <- c("x1", "x2", "x3")
y   <- c(100,200,300)

d_char <- data.frame(sample = sample_char, x = x, y = y)

d_char %>% 
  mutate(sample = factor(sample, levels = c("B","A","C"))) %>%
  ggplot(aes(x = x, y = y, fill = sample)) +
  geom_bar(stat = "identity", position = "dodge") 

f:id:chito_ng:20190223221044p:plain

色の設定

別記事参照
knknkn.hatenablog.com