gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:42:38 25 4
gpt4 key购买 nike

假设我们有这个带有缩放比例的条形图竞赛 x-axis .正是从这个 answer 中取出代码来自 @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.



另见 here用于代码使用时的解决方案 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 ,因为您也想反转轴。如果您将 y 美学设置为国家,然后重新排序以进行排名,那可能是最好的。
  • coord_cartesian(clip='off')这与您的 coord_flip 上的设置具有相同的用途。 .如果您想对国家/地区名称进行过度绘制,则需要这样做才能使该文本“超出”绘图区域。再次 - 如果您使用 y=country 会更好,但再次......呃,它有效。
  • axis.line.y = element_blank()删除以便于查看 - 或者您可以保留它并使用轴和列开头之间的绘图区域边距。再次 - 嗯,它有效。

  • enter image description here

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

    关于r - 使用gganimate和view_follow和geom_tile时如何摆脱由coord_flip引起的轴闪烁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61151056/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com