r - 动画排序条形图,条形相互超越

标签 r animation ggplot2 data-visualization gganimate

编辑:关键字是“条形图竞赛”

您将如何从 Jaime Albella 复制此图表在 R 中?

查看 visualcapitalist.com 上的动画或点击twitter (提供多个引用以防万一出现故障)。

enter image description here

我将其标记为 ggplot2gganimate 但任何可以从 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))

编辑:

约翰·默多克的另一个很酷的例子:

Most populous cities from 1500 to 2018

最佳答案

编辑:添加样条插值以实现更平滑的过渡,而不会使排名变化发生得太快。代码在底部。

enter image description here

<小时/>

我改编了我的答案 to a related question 。我喜欢使用 geom_tile 作为动画条,因为它允许您滑动位置。

在您添加数据之前,我已对此进行了研究,但碰巧的是,我使用的 gapminder 数据密切相关。

enter image description here

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)
<小时/>

对于顶部更平滑的版本,我们可以在绘图步骤之前添加一个步骤来进一步插入数据。插值两次可能很有用,一次以粗粒度确定排名,另一次则用于更精细的细节。如果排名计算得太精细,则条形图交换位置的速度会太快。

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)

关于r - 动画排序条形图,条形相互超越,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53162821/

相关文章:

r - 在 R 中绘制 map - 仅显示外部边界

css - 我希望 .tabcontent 在 0.3 秒内从 0 中心显示为全尺寸动画

swift - 如何将 Xcode Assets 文件夹中的图像数组添加到 ViewController?

r - 具有透明背景但字体可见的ggrepel标签

r - 将额外参数传递给 stat 函数

r - 可以有条件地计算 dplyr::summarize() 的不同部分吗?

r - 在数字序列中查找 "valleys"

r - 剪接R中的bquote

android - 如何在 GridView 中的不同图标之间平滑移动焦点

r - ggplot 使图例符号变细