使用gganimate和view_follow和geom_tile时,如何摆脱由coord_flip引起的轴闪烁? [英] How to get rid of axis flickering caused by coord_flip when using gganimate and view_follow and geom_tile?

查看:116
本文介绍了使用gganimate和view_follow和geom_tile时,如何摆脱由coord_flip引起的轴闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们的条形图竞赛的缩放比例为x-axis.完全从 answer 中提取代码href ="https://stackoverflow.com/users/6851825/jon-spring"> @Jon Spring 并添加最后一行(在动画行之前):

Let's say we have this bar chart race with a scaling x-axis. Taking the code exactly from this answer by @Jon Spring and adding the very final line (before the animate line):

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)

问题是轴上有闪烁.

我该如何解决?请注意,这似乎来自coord_flip 代码.

另请参见此处geom_bar时使用a>作为解决方案.

See also here for a solution when the code used geom_bar.

但是,就我而言,代码使用的是geom_tile.我该怎么办?

推荐答案

在受到此已发布的问题在github 上.正如您在问题中指出的那样,将coord_flip()与动画一起使用时,轴抖动显然是一个已知问题.

I think I found the answer, after being inspired by this posted issue on github. As you pointed out in your question, it's apparently a known issue that you get axis flickering when using coord_flip() with animations.

我尝试用geom_rect代替geom_tile,但这仍然会使您闪烁.

I tried geom_rect in place of geom_tile, but that still gets you flickering.

起作用的是geom_colh代替geom_tile!这来自ggstance包.这是代码:

What does work is geom_colh in place of geom_tile! This is from the ggstance package. Here's the code:

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()

所以要回顾一下更改的内容:

So to go over what was changed:

  • geom_colh代替geom_tile.为此,您需要ggstance程序包-我什至没有尝试geom_col,但我想您会对此感到困惑.

  • geom_colh used in place of geom_tile. You need the ggstance package for this - I didn't even try geom_col, but I imagine you're going to get flickering with that.

scale_y_reverse此调用包含代替scale_y_continuous的标签调用,因为您也想反转轴.

scale_y_reverse this call contains your labels call in place of scale_y_continuous, since you want to reverse the axis too. Probably would be best if you set your y aesthetic as country and then reordered for rank.... but eh, this works as you have it.

coord_cartesian(clip='off')coord_flip上的设置具有相同的作用.如果要对国家名称进行过度绘图,则需要这样做,以便使该文本超出"绘图区域.再次-如果您使用y=country会更好,但是再次...嗯,它可以工作.

coord_cartesian(clip='off') This serves the same purpose as the settings on your coord_flip. If you want to overplot the country names, you need this in order to get that text to go "outside" the plot area. Again - would be better if you used y=country, but again... eh, it works.

axis.line.y = element_blank()以便于查看-或者您可以保留它并在轴与列起点之间使用绘图区域边距进行播放.再次-嗯,它有效.

axis.line.y = element_blank() removed for easy viewing - or you can keep it and play with your plot area margins between the axis and the start of the columns. Again - eh, it works.

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

There may be other ways, but this seems to be a reasonable workaround. Nice graphic!

这篇关于使用gganimate和view_follow和geom_tile时,如何摆脱由coord_flip引起的轴闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆