使用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?
假设我们有一个缩放 x-axis
的条形图竞赛。完全从此 by @Jon Spring 中获取代码并添加最后一行(在动画行之前):
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)
问题是轴上有闪烁。
How can I fix this? Note that it appears that this derives from the coord_flip
code.
当代码使用 geom_bar
时,另请参阅 以获取解决方案。
However, in my case, the code is using geom_tile
. What can I do?
我想我找到了答案,受到 this posted issue on 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()
回顾一下更改的内容:
geom_colh
用来代替 geom_tile
。为此,您需要 ggstance
包 - 我什至没有尝试 geom_col
,但我想您会对此感到困惑。
scale_y_reverse
此调用包含代替 scale_y_continuous
的标签调用,因为您也想反转轴。如果您将您的审美设置为国家/地区,然后重新排序以获得排名,这可能是最好的……但是,嗯,这就像您拥有的那样有效。
coord_cartesian(clip='off')
这与 coord_flip
上的设置具有相同的目的。如果你想覆盖国家名称,你需要这个才能让文本进入 "outside" 绘图区域。再次 - 如果您使用 y=country
会更好,但是再次......嗯,它有效。
axis.line.y = element_blank()
已删除以便于查看 - 或者您可以保留它并使用轴和列开始之间的绘图区域边距。再次 - 嗯,它有效。
可能还有其他方法,但这似乎是一个合理的解决方法。漂亮的图形!
假设我们有一个缩放 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)
问题是轴上有闪烁。
How can I fix this? Note that it appears that this derives from the
coord_flip
code.
当代码使用 geom_bar
时,另请参阅
However, in my case, the code is using
geom_tile
. What can I do?
我想我找到了答案,受到 this posted issue on 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()
回顾一下更改的内容:
geom_colh
用来代替geom_tile
。为此,您需要ggstance
包 - 我什至没有尝试geom_col
,但我想您会对此感到困惑。scale_y_reverse
此调用包含代替scale_y_continuous
的标签调用,因为您也想反转轴。如果您将您的审美设置为国家/地区,然后重新排序以获得排名,这可能是最好的……但是,嗯,这就像您拥有的那样有效。coord_cartesian(clip='off')
这与coord_flip
上的设置具有相同的目的。如果你想覆盖国家名称,你需要这个才能让文本进入 "outside" 绘图区域。再次 - 如果您使用y=country
会更好,但是再次......嗯,它有效。axis.line.y = element_blank()
已删除以便于查看 - 或者您可以保留它并使用轴和列开始之间的绘图区域边距。再次 - 嗯,它有效。
可能还有其他方法,但这似乎是一个合理的解决方法。漂亮的图形!