アニメーション化された並べ替えられた棒グラフで、棒が互いに追い越し合う様子を見る 質問する

アニメーション化された並べ替えられた棒グラフで、棒が互いに追い越し合う様子を見る 質問する

編集: キーワードは「棒グラフレース」です

このチャートを再現するにはどうすればいいでしょうか?ハイメ・アルベラR では?

アニメーションを見るビジュアルキャピタリストまたはツイッター(壊れた場合に備えて、複数の参照を示します)。

ここに画像の説明を入力してください

私はこれをタグ付けしますggplot2そしてgganimateただし、R から生成できるものはすべて関連します。

データ(感謝https://github.com/datasets/gdp

gdp <- read.csv("https://raw.github.com/datasets/gdp/master/data/gdp.csv")
# remove irrelevant aggregated values
words <- scan(
  text="world income only total dividend asia euro america africa oecd",
  what= character())
pattern <- paste0("(",words,")",collapse="|")
gdp  <- subset(gdp, !grepl(pattern, Country.Name , ignore.case = TRUE))

編集:

ジョン・マードックのもう一つの素晴らしい例:

1500年から2018年までの人口が最も多かった都市

ベストアンサー1

編集: ランクの変化が速くなりすぎないように、よりスムーズな遷移のためにスプライン補間を追加しました。コードは下部にあります。

ここに画像の説明を入力してください


私は自分の答えを適応させました関連する質問geom_tile位置をスライドさせることができるので、アニメーションバーに使用するのが好きです。

私はあなたがデータを追加する前にこれに取り組みましたが、偶然にもgapminder私が使用したデータは密接に関連しています。

ここに画像の説明を入力してください

library(tidyverse)
library(gganimate)
library(gapminder)
theme_set(theme_classic())

gap <- gapminder %>%
  filter(continent == "Asia") %>%
  group_by(year) %>%
  # The * 1 makes it possible to have non-integer ranks while sliding
  mutate(rank = min_rank(-gdpPercap) * 1) %>%
  ungroup()

p <- ggplot(gap, aes(rank, group = country, 
                     fill = as.factor(country), color = as.factor(country))) +
  geom_tile(aes(y = gdpPercap/2,
                height = gdpPercap,
                width = 0.9), alpha = 0.8, color = NA) +

  # text in x-axis (requires clip = "off" in coord_*)
  # paste(country, " ")  is a hack to make pretty spacing, since hjust > 1 
  #   leads to weird artifacts in text spacing.
  geom_text(aes(y = 0, label = paste(country, " ")), vjust = 0.2, hjust = 1) +

  coord_flip(clip = "off", expand = FALSE) +
  scale_y_continuous(labels = scales::comma) +
  scale_x_reverse() +
  guides(color = FALSE, fill = FALSE) +

  labs(title='{closest_state}', x = "", y = "GFP per capita") +
  theme(plot.title = element_text(hjust = 0, size = 22),
        axis.ticks.y = element_blank(),  # These relate to the axes post-flip
        axis.text.y  = element_blank(),  # These relate to the axes post-flip
        plot.margin = margin(1,1,1,4, "cm")) +

  transition_states(year, transition_length = 4, state_length = 1) +
  ease_aes('cubic-in-out')

animate(p, fps = 25, duration = 20, width = 800, height = 600)

上部のより滑らかなバージョンでは、プロット手順の前にデータをさらに補間する手順を追加できます。補間は 2 回行うと便利です。1 回目は大まかな粒度で順位を決定し、2 回目はより細かい詳細を決定します。順位の計算が細かすぎると、バーの位置がすぐに入れ替わります。

gap_smoother <- gapminder %>%
  filter(continent == "Asia") %>%
  group_by(country) %>%
  # Do somewhat rough interpolation for ranking
  # (Otherwise the ranking shifts unpleasantly fast.)
  complete(year = full_seq(year, 1)) %>%
  mutate(gdpPercap = spline(x = year, y = gdpPercap, xout = year)$y) %>%
  group_by(year) %>%
  mutate(rank = min_rank(-gdpPercap) * 1) %>%
  ungroup() %>%

  # Then interpolate further to quarter years for fast number ticking.
  # Interpolate the ranks calculated earlier.
  group_by(country) %>%
  complete(year = full_seq(year, .5)) %>%
  mutate(gdpPercap = spline(x = year, y = gdpPercap, xout = year)$y) %>%
  # "approx" below for linear interpolation. "spline" has a bouncy effect.
  mutate(rank =      approx(x = year, y = rank,      xout = year)$y) %>%
  ungroup()  %>% 
  arrange(country,year)

次に、プロットではいくつかの変更された行が使用されますが、それ以外は同じです。

p <- ggplot(gap_smoother, ...
  # This line for the numbers that tick up
  geom_text(aes(y = gdpPercap,
                label = scales::comma(gdpPercap)), hjust = 0, nudge_y = 300 ) +
  ...
  labs(title='{closest_state %>% as.numeric %>% floor}', 
   x = "", y = "GFP per capita") +
...
transition_states(year, transition_length = 1, state_length = 0) +
enter_grow() +
exit_shrink() +
ease_aes('linear')

animate(p, fps = 20, duration = 5, width = 400, height = 600, end_pause = 10)

おすすめ記事