10

我有一个时间序列的数据,其中我在 y 轴上绘制疾病的诊断率,在 x 轴上绘制DIAG_RATE_65_PLUS地理组以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 产生什么:

绝地求生图

我现在有两个问题:

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

2) gganimate 中是否有一个选项,其中组在条形图中每个月“转移”到正确的排名?

我的评论查询的情节:

https://i.stack.imgur.com/s2UPw.gif https://i.stack.imgur.com/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,标签不对齐并四处移动。

在此处输入图像描述

更改上面的代码hjust=1

在此处输入图像描述

@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')
4

2 回答 2

12

为了补充@eipi10 的出色答案,我认为这是一个值得更换geom_bar以获得更大灵活性的案例。geom_bar通常对于离散类别来说是相当方便的,但它并没有让我们充分利用gganimate'ssilly-smooth 动画的荣耀。

例如,使用geom_tile,我们可以重新创建与 geom_bar 相同的外观,但在 x 轴上具有流体运动。这有助于保持对每个条的视觉跟踪,并查看哪些条的顺序变化最多。我认为这很好地解决了您问题的第二部分。

在此处输入图像描述

为了完成这项工作,我们可以在数据中添加一个新列,显示每个月应该使用的顺序。我们将此订单保存为双精度数,而不是整数(使用* 1.0)。这将允许gganimate在位置 1 和位置 2 之间设置动画时在位置 1.25 放置一个条形图。

df2 <- df %>%
  group_by(ACH_DATEyearmon) %>%
  mutate(ordering = min_rank(DIAG_RATE_65_PLUS) * 1.0) %>%
  ungroup() 

现在我们可以以类似的方式绘图,但使用geom_tile代替geom_bar. 我想NAME在顶部和轴上都显示,所以我使用了两个geom_text具有不同 y 值的调用,一个在零处,一个在条形的高度。vjust让我们使用文本行单位垂直对齐每个。

这里的另一个技巧是关闭剪辑coord_cartesian,这让底部文本位于绘图区域下方,进入 x 轴文本通常所在的位置。

p <- df2 %>%
  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") +
  # text on top of bars
  geom_text(aes(y = DIAG_RATE_65_PLUS, label = NAME), vjust = -0.5) +
  # text in x-axis (requires clip = "off" in coord_cartesian)
  geom_text(aes(y = 0, label = NAME), vjust = 2) +
  coord_cartesian(clip = "off", expand = FALSE) +

  labs(title='{closest_state}', x = "") +
  theme(plot.title = element_text(hjust = 1, size = 22),
        axis.ticks.x = element_blank(),
        axis.text.x  = element_blank()) + 

  transition_states(ACH_DATEyearmon, 
                    transition_length = 2, state_length = 1) +
  ease_aes('cubic-in-out')

animate(p, nframes = 300, fps = 20, width = 400, height = 300)

回到您的第一个问题,这是我fill = "gray60"geom_tile通话中删除后制作的彩色版本。我按照NAME2017 年 8 月的顺序对类别进行了排序,因此它们看起来像您所描述的那样是按顺序排列的。

可能有更好的方法来进行排序,但我通过加入df2一个只有 2017 年 8 月排序的表来做到这一点。

在此处输入图像描述

Aug_order <- df %>%
  filter(ACH_DATEyearmon == "Aug 2017") %>%
  mutate(Aug_order = min_rank(DIAG_RATE_65_PLUS) * 1.0) %>%
  select(NAME, Aug_order)

df2 <- df %>%
  group_by(ACH_DATEyearmon) %>%
  mutate(ordering = min_rank(DIAG_RATE_65_PLUS) * 1.0) %>%
  ungroup() %>%
  left_join(Aug_order) %>%
  mutate(NAME = fct_reorder(NAME, -Aug_order))
于 2018-10-04T17:27:44.090 回答
8

条形排序由 完成ggplot且不受gganimate. 这些条是根据DIAG_RATE_65_PLUS每个 内的总和进行排序的ACH_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_PLUS每个的总和NAME。注意 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)) 

在此处输入图像描述

您可以在下面看到,当我们明确地求和并按总和排序时,排序是相同DIAG_RATE_65_PLUSNAME

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

现在我们要创建一个动画,NAME分别DIAG_RATE_65_PLUS为每个ACH_DATEyearmon. 为此,我们首先生成一个名为的新列order,用于设置我们想要的顺序:

df = df %>% 
  arrange(ACH_DATEyearmon, DIAG_RATE_65_PLUS) %>% 
  mutate(order = 1:n())

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

请注意,我们使用orderx 变量,但随后我们运行scale_x_continuous将 x-labels 更改为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")

在此处输入图像描述

如果关闭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')

在此处输入图像描述

更新:回答你的问题......

要按给定月份的值排序,请将数据转换为按该月排序的级别的因子。要绘制旋转图,而不是coord_flip,我们将使用包中geom_barh的 (horizo​​ntal bar plot) ggstance。请注意,我们必须在 and 中切换 y 和 x,aes并且view_follow()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")

在此处输入图像描述

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

于 2018-10-04T00:34:27.363 回答