使用gganimate和view_follow和geom_tile时如何摆脱由coord_flip引起的轴闪烁?

bil*_*999 6 r flicker ggplot2 gganimate

假设我们有这个带有缩放比例的条形图竞赛x-axis。从这个考虑的代码完全回答@乔恩春季和添加(有生线之前)非常最后一行:

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') +
    view_follow()

animate(p, fps = 25, duration = 20, width = 800, height = 600)
Run Code Online (Sandbox Code Playgroud)

问题是轴中有闪烁。

我怎样才能解决这个问题?请注意,这似乎来自coord_flip 代码

另请参阅此处以获取使用代码时的解决方案geom_bar

但是,就我而言,代码使用geom_tile. 我能做什么?

che*_*123 4

我想我在受到 github 上发布的问题的启发后找到了答案。正如您在问题中指出的那样,这显然是一个已知问题,即在使用coord_flip()动画时轴会闪烁。

我尝试geom_rect代替geom_tile,但这仍然会让你闪烁。

什么是工作geom_colh代替geom_tile!这是来自ggstance包装的。这是代码:

ggplot(gap, aes(y=rank, group = country, 
    fill = as.factor(country), color = as.factor(country))) +

geom_colh(aes(x=gdpPercap/2), width=0.9, alpha = 0.8, color = NA) +

geom_text(aes(x = 0, label = paste(country, " ")), vjust = 0.2, hjust = 1) +

scale_y_reverse(labels = scales::comma) +
guides(color = FALSE, fill = FALSE) +
coord_cartesian(clip='off') +

labs(title='{closest_state}', x = "GFP per capita", y = "") +
theme(
    plot.title = element_text(hjust = 0, size = 22),
    axis.ticks.y = element_blank(),
    axis.text.y  = element_blank(),
    plot.margin = margin(1,1,1,4, "cm"),
    axis.line.y = element_blank()) +

transition_states(year, transition_length = 4, state_length = 1) +
ease_aes('cubic-in-out') +
view_follow()
Run Code Online (Sandbox Code Playgroud)

让我们回顾一下更改的内容:

  • geom_colh用于代替geom_tile. 你需要ggstance这个包 - 我什至没有尝试过geom_col,但我想你会对此感到困惑。

  • scale_y_reverse此调用包含您的标签调用来代替scale_y_continuous,因为您也想反转轴。如果您将 y 审美设置为国家/地区,然后重新排序排名,可能会是最好的……但是,呃,这可以按您的方式工作。

  • coord_cartesian(clip='off') 这与您的coord_flip. 如果您想过度绘制国家/地区名称,则需要这样做才能使文本移至绘图区域“外部”。再说一遍 - 如果你使用的话会更好y=country,但是再说一次......呃,它有效。

  • axis.line.y = element_blank()删除以便于查看 - 或者您可以保留它并使用轴和列开头之间的绘图区域边距。再说一遍 - 呃,它有效。

在此输入图像描述

可能还有其他方法,但这似乎是一个合理的解决方法。不错的图形!

  • 现在我很好奇为什么某些几何体有效,而其他几何体却让你闪烁......如果你了解任何相关内容,请在这里发表评论,以便未来的 SO'ers 可以受益,我也可以满足我无尽的好奇心!:) (2认同)