差を可視化する:Batman と Star Wars
前の演習では、colors_joined を作成しました。ここでは各色(name)ごとに 1 本のバーを持つ棒グラフを作り、比率の差を表示します。
このコースの範囲を超える要素(factor)と可視化に関する処理は、こちらで一部済ませてあります。以下は、動画で使用する colors_joined テーブルを作成したコードです。
colors_joined <- batman_colors %>%
full_join(star_wars_colors, by = "color_id", suffix = c("_batman", "_star_wars")) %>%
replace_na(list(total_batman = 0, total_star_wars = 0)) %>%
inner_join(colors, by = c("color_id" = "id")) %>%
mutate(difference = fraction_batman - fraction_star_wars,
total = total_batman + total_star_wars) %>%
filter(total >= 200) %>%
mutate(name = fct_reorder(name, difference))
この演習はコースの一部です
dplyr で行うデータの結合
演習の手順
colors_joinedテーブルを使って棒グラフを作成し、Batman と Star Wars テーマで目立つ色を表示します。バーの色分けはそれぞれのnameによって行ってください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Create a bar plot using colors_joined and the name and difference columns
ggplot(___, aes(___, ___, fill = ___)) +
geom_col() +
coord_flip() +
scale_fill_manual(values = color_palette, guide = "none") +
labs(y = "Difference: Batman - Star Wars")