開始使用免費開始

視覺化差異:Batman 與 Star Wars

在上一個練習中,你已建立 colors_joined。現在你要建立長條圖,每個顏色(name)一個長條,顯示分數差異。

由於因子與視覺化不在本課範圍內,我們已替你做了一些前處理:以下是建立影片中所用 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 作為長條的 fill 顏色。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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")
編輯並執行程式碼