【问题标题】:R : geom_text() goes wrong with gganimateR:geom_text() 与 gganimate 出错
【发布时间】:2021-11-08 08:52:38
【问题描述】:

geom_text() 函数在 gganimate 中没有正确显示。 gganimate 可以正确打印线图的标签,但不能正确打印条形图的标签。请问我该如何解决这个问题?还有其他方法可以使用 gganimate 在我的绘图上打印值吗?

library(tidyr)
library(dplyr)
library(ggplot2)
library(gganimate)

tramway_df <- data.frame(
  stop_order = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17),
  stop_id = c(9943, 5041, 5361, 2902, 2903, 5872, 5802, 5803, 5804, 5805, 
               5710, 5711, 5712, 1643, 5714, 521, 529),
  stop_name = c("DA VINCI", "DA VINCI", "BORDET STATION", "VAN CUTSEM", "FONSON", "PAIX", 
            "TILLEUL", "HELMET", "FOYER SCHAERBEEK.", "WAELHEM", "VERBOEKHOVEN", "PAVILLON",
            "RUBENS", "LIEDTS", "THOMAS", "GARE DU NORD", "ROGIER"),
  stop_in = c(27, 102, 153, 141, 190, 317, 379, 438, 328, 214, 278, 298, 154, 144, 162, 185, 0),
  stop_out = c(0, 7, 3, 10, 22, 37, 81, 122, 143, 168, 291, 230, 149, 301, 412, 707, 821),
  stop_charge = c(27, 121, 271, 402, 571, 851, 1149, 1465, 1650, 1696, 1682, 1745, 1750, 1592, 1342, 821, 0)
)

plot <- tramway_df %>% 
  gather(movement, value, stop_in:stop_out) %>% 
  mutate(stop_id = factor(stop_id, levels = unique(stop_id)),
         value = ifelse(movement == "stop_out", -value, value)) %>% 
  ggplot(aes(x = stop_id)) +
  geom_col(aes(y = value, fill = movement), position = "dodge", width = 1) +
  geom_point(aes(y = stop_charge), color = "green") + 
  geom_line(aes(y = stop_charge, group=1), color = "green") + 
  geom_text(aes(label = value, y = value), hjust = 1, vjust = 0) +
  geom_text(aes(label = stop_charge, y = stop_charge), hjust = 1, vjust = 1) +
  scale_x_discrete(breaks = tramway_df$stop_id,
                   labels = tramway_df$stop_name) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 60, hjust = 1, vjust = 1))

animation <- plot + transition_reveal(stop_order)

animate(animation, height = 500, width = 800)

【问题讨论】:

    标签: r ggplot2 gganimate


    【解决方案1】:

    通过添加position = position_dodge2(width = 0.9, preserve = "single") 和更多参数,它可以正确打印。您可能需要更多参数来定位条形图的文本。

    plot <- tramway_df %>% 
      gather(movement, value, stop_in:stop_out) %>% 
      mutate(stop_id = factor(stop_id, levels = unique(stop_id)),
             value = ifelse(movement == "stop_out", -value, value)) %>% 
      ggplot(aes(x = stop_id)) +
      geom_col(aes(y = value, fill = movement), position = "dodge", width = 1) +
      geom_point(aes(y = stop_charge), color = "green") + 
      geom_line(aes(y = stop_charge, group=1), color = "green") + 
      geom_text(position = position_dodge2(width = 0.9, preserve = "single"), aes(label = value, y = value + 30 * sign(value), fill = movement, hjust = 0.5)) +
      geom_text(aes(label = stop_charge, y = stop_charge), hjust = 1, vjust = 1) +
      scale_x_discrete(breaks = tramway_df$stop_id,
                       labels = tramway_df$stop_name) +
      theme_bw() +
      theme(axis.text.x = element_text(angle = 60, hjust = 1, vjust = 1))
    
    animation <- plot + transition_reveal(stop_order)
    
    animate(animation, height = 500, width = 800)
    

    【讨论】:

    • 你好@Park,我试图让红色条形图的标签有点高,但没有任何结果。请你帮助我好吗 ?再次感谢!
    • @salwa 在geom_text(position =..... 行,尝试将vjust 改大。
    • 您好@Park,当我更改vjust值以仅更正红色图表上的文本位置时,问题是蓝色条形图的标签位置也移动了,我不想要它!不知道最后有没有解决办法!再次感谢您的帮助!
    • @salwa 我也想知道......如果我找到解决方案,我会告诉你!
    • @salwa 在aes(label = value,....,更改y = value + 30 * sign(value) 有效!我将在上面编辑代码和绘图。
    猜你喜欢
    • 2018-12-28
    • 1970-01-01
    • 2023-03-17
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多