
レシピ
10.3
凡例の項目順を変える
257
グラフ領域の内部に凡例を置く場合、凡例をそれ以外と区別するために不透明な境界線を付けたほ
うが良いこともあります(図10-4左)。
pg_plot +
theme(legend.position = c(.85, .2)) +
theme(legend.background = element_rect(fill = "white", colour = "black"))
プロットに溶け込むように、要素の周りの境界線を取り除くこともできます(図10-4右)。
pg_plot +
theme(legend.position = c(.85, .2)) +
theme(legend.background = element_blank()) + #
全体の境界線を削除
theme(legend.key = element_blank()) #
各項目の周りにある境界線を削除
図10-4 左:不透明な背景と外枠を持つ凡例 右:背景と外枠なしの凡例
レシピ
10.3
凡例の項目順を変える
問題
凡例の項目順を変えたい。
解決策
スケールの
limits
を望みの順に指定します(図10-5)。
#
基本プロットを作成
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
geom_boxplot()
pg_plot