gpt4 book ai didi

r - 通过 gganimate : conflict of view_follow & coord_flip 的动画条形图

转载 作者:行者123 更新时间:2023-12-04 10:38:26 24 4
gpt4 key购买 nike

我想用 gganim 制作一个动画条形图包裹。条形图的坐标应该通过 coord_flip 翻转(即水平条)和 x 轴应根据条的长度通过 view_follow 变得灵活.

考虑以下示例数据:

# Create example data
df <- data.frame(ordering = c(rep(1:3, 2), 3:1, rep(1:3, 2)),
year = factor(sort(rep(2001:2005, 3))),
value = round(runif(15, 0, 100)),
group = rep(letters[1:3], 5))

如果我创建一个没有 coord_flip 的动画条形图,一切正常:
library("gganimate")
library("ggplot2")

# Create animated ggplot without coord_flip
ggp <- ggplot(df, aes(x = ordering, y = value)) +
geom_bar(stat = "identity", aes(fill = group)) +
transition_states(year, transition_length = 2, state_length = 0) +
view_follow(fixed_x = TRUE) # +
# coord_flip()
ggp

enter image description here

但是,如果我添加 coord_flip ,轴无缘无故地左右移动:
# Create animated ggplot with coord_flip
ggp2 <- ggplot(df, aes(x = ordering, y = value)) +
geom_bar(stat = "identity", aes(fill = group)) +
transition_states(year, transition_length = 2, state_length = 0) +
view_follow(fixed_x = TRUE) +
coord_flip()
ggp2

enter image description here

问题:如何翻转条形图的轴并启用灵活轴?

最佳答案

您可能需要考虑 geom_barh来自 ggstance 包,而不是 geom_bar + coord_flip :

library(ggstance)

ggplot(df, aes(y = ordering, x = value)) +
geom_barh(stat = "identity", aes(fill = group)) +
transition_states(year, transition_length = 2, state_length = 0) +
view_follow(fixed_y = TRUE)

animated plot

关于r - 通过 gganimate : conflict of view_follow & coord_flip 的动画条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54646652/

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