gpt4 book ai didi

r - gganimate如何订购有序的酒吧时间序列?

转载 作者:行者123 更新时间:2023-12-04 05:16:07 25 4
gpt4 key购买 nike

我有一个时间序列数据,其中在y轴DIAG_RATE_65_PLUS上绘制疾病的诊断率,在x轴NAME上以简单的条形图比较地理区域。我的时间变量是ACH_DATEyearmon,如标题所示,动画在循环播放。

df %>% ggplot(aes(reorder(NAME, DIAG_RATE_65_PLUS), DIAG_RATE_65_PLUS)) +
geom_bar(stat = "identity", alpha = 0.66) +
labs(title='{closest_state}') +
theme(plot.title = element_text(hjust = 1, size = 22),
axis.text.x=element_blank()) +
transition_states(ACH_DATEyearmon, transition_length = 1, state_length = 1) +
ease_aes('linear')

我已经重新排序 NAME,所以它按 DIAG_RATE_65_PLUS排名。

氨基甲酸酯产生的物质:

gganimate plot

我现在有两个问题:

1)gganimate如何精确地对数据重新排序?总体上会有一些总体上的重新排序,但是每个月都没有 DIAG_RATE_65_PLUS从最小到最大完美地对组进行排序的框架。理想情况下,我希望完美订购最后一个月“2018年8月”。前几个月所有的x轴都可以基于“2018年8月”订购的 NAME进行设置。

2)gganimate中是否有选项,每个组在条形图中每个月都会“转移”到正确的位置?

我的评论查询的情节:

/image/s2UPw.gif
/image/Z1wfd.gif

@JonSpring
    df %>%
ggplot(aes(ordering, group = NAME)) +
geom_tile(aes(y = DIAG_RATE_65_PLUS/2,
height = DIAG_RATE_65_PLUS,
width = 0.9), alpha = 0.9, fill = "gray60") +
geom_hline(yintercept = (2/3)*25, linetype="dotdash") +
# text in x-axis (requires clip = "off" in coord_cartesian)
geom_text(aes(y = 0, label = NAME), hjust = 2) + ## trying different hjust values
theme(plot.title = element_text(hjust = 1, size = 22),
axis.ticks.y = element_blank(), ## axis.ticks.y shows the ticks on the flipped x-axis (the now metric), and hides the ticks from the geog layer
axis.text.y = element_blank()) + ## axis.text.y shows the scale on the flipped x-axis (the now metric), and hides the placeholder "ordered" numbers from the geog layer
coord_cartesian(clip = "off", expand = FALSE) +
coord_flip() +
labs(title='{closest_state}', x = "") +
transition_states(ACH_DATEyearmon,
transition_length = 2, state_length = 1) +
ease_aes('cubic-in-out')

使用 hjust=2时,标签未对齐且无法移动。

enter image description here

hjust=1更改以上代码

enter image description here

@ eipi10
df %>% 
ggplot(aes(y=NAME, x=DIAG_RATE_65_PLUS)) +
geom_barh(stat = "identity", alpha = 0.66) +
geom_hline(yintercept=(2/3)*25, linetype = "dotdash") + #geom_vline(xintercept=(2/3)*25) is incompatible, but geom_hline works, but it's not useful for the plot
labs(title='{closest_state}') +
theme(plot.title = element_text(hjust = 1, size = 22)) +
transition_states(ACH_DATEyearmon, transition_length = 1, state_length = 50) +
view_follow(fixed_x=TRUE) +
ease_aes('linear')

最佳答案

条形排序由ggplot完成,不受gganimate影响。根据每个DIAG_RATE_65_PLUSACH_DATEyearmon的总和对这些小节进行排序。下面,我将说明这些条的排序方式,然后提供代码来创建动画图,并在每帧中按从低到高的顺序进行所需的排序。

要查看条形的顺序,首先让我们创建一些假数据:

library(tidyverse)
library(gganimate)
theme_set(theme_classic())

# Fake data
dates = paste(rep(month.abb, each=10), 2017)

set.seed(2)
df = data.frame(NAME=c(replicate(12, sample(LETTERS[1:10]))),
ACH_DATEyearmon=factor(dates, levels=unique(dates)),
DIAG_RATE_65_PLUS=c(replicate(12, rnorm(10, 30, 5))))

现在,让我们绘制一个条形图。条形是每个 DIAG_RATE_65_PLUSNAME的总和。请注意x轴 NAME值的顺序:
df %>% 
ggplot(aes(reorder(NAME, DIAG_RATE_65_PLUS), DIAG_RATE_65_PLUS)) +
geom_bar(stat = "identity", alpha = 0.66) +
labs(title='{closest_state}') +
theme(plot.title = element_text(hjust = 1, size = 22))

enter image description here

您可以在下面看到,当我们通过 DIAG_RATE_65_PLUS显式将 NAME求和并按总和排序时,排序是相同的:
df %>% group_by(NAME) %>% 
summarise(DIAG_RATE_65_PLUS = sum(DIAG_RATE_65_PLUS)) %>%
arrange(DIAG_RATE_65_PLUS)

   NAME DIAG_RATE_65_PLUS
1 A 336.1271
2 H 345.2369
3 B 346.7151
4 I 350.1480
5 E 356.4333
6 C 367.4768
7 D 368.2225
8 F 368.3765
9 J 368.9655
10 G 387.1523


现在,我们要创建一个动画,为每个 NAMEDIAG_RATE_65_PLUSACH_DATEyearmon分别进行排序。为此,我们首先生成一个名为 order的新列,该列设置所需的顺序:
df = df %>% 
arrange(ACH_DATEyearmon, DIAG_RATE_65_PLUS) %>%
mutate(order = 1:n())

现在我们创建动画。 transition_states为每个 ACH_DATEyearmon生成框架。 view_follow(fixed_y=TRUE)仅显示当前 ACH_DATEyearmon的x值,并为所有帧保持相同的y轴范围。

请注意,我们将 order用作x变量,然后运行 scale_x_continuous将x-label更改为 NAME值。我将这些标签包括在绘图中,以便您可以看到它们随每个 ACH_DATEyearmon的变化而变化,但是您当然可以像在示例中所做的那样在实际绘图中将其删除。
p = df %>% 
ggplot(aes(order, DIAG_RATE_65_PLUS)) +
geom_bar(stat = "identity", alpha = 0.66) +
labs(title='{closest_state}') +
theme(plot.title = element_text(hjust = 1, size = 22)) +
scale_x_continuous(breaks=df$order, labels=df$NAME) +
transition_states(ACH_DATEyearmon, transition_length = 1, state_length = 50) +
view_follow(fixed_y=TRUE) +
ease_aes('linear')

animate(p, nframes=60)

anim_save("test.gif")

enter image description here

如果关闭 view_follow(),则可以看到“整个”图的样子(当然,可以通过在 transition_states行之前停止代码来查看完整的非动画图)。
p = df %>% 
ggplot(aes(order, DIAG_RATE_65_PLUS)) +
geom_bar(stat = "identity", alpha = 0.66) +
labs(title='{closest_state}') +
theme(plot.title = element_text(hjust = 1, size = 22)) +
scale_x_continuous(breaks=df$order, labels=df$NAME) +
transition_states(ACH_DATEyearmon, transition_length = 1, state_length = 50) +
#view_follow(fixed_y=TRUE) +
ease_aes('linear')

enter image description here

更新:要回答您的问题...

要按给定月份的值进行排序,请将数据转换为具有该月份排序水平的因子。为了绘制旋转图,我们将使用 coord_flip包中的 geom_barh(水平条形图)代替 ggstance。请注意,我们必须在 aesview_follow()中切换y和x,并且y轴 NAME值的顺序现在是恒定的:
library(ggstance)

# Set NAME order based on August 2017 values
df = df %>%
arrange(DIAG_RATE_65_PLUS) %>%
mutate(NAME = factor(NAME, levels=unique(NAME[ACH_DATEyearmon=="Aug 2017"])))

p = df %>%
ggplot(aes(y=NAME, x=DIAG_RATE_65_PLUS)) +
geom_barh(stat = "identity", alpha = 0.66) +
labs(title='{closest_state}') +
theme(plot.title = element_text(hjust = 1, size = 22)) +
transition_states(ACH_DATEyearmon, transition_length = 1, state_length = 50) +
view_follow(fixed_x=TRUE) +
ease_aes('linear')

animate(p, nframes=60)
anim_save("test3.gif")

enter image description here

对于平稳的过渡,@ JonSpring的答案似乎处理得很好。

关于r - gganimate如何订购有序的酒吧时间序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52623722/

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