【问题标题】:How to fix a dynamic graph made in ggplot2?如何修复在ggplot2中制作的动态图?
【发布时间】:2019-06-05 02:54:49
【问题描述】:

我尝试使用数据框复制动态图:

library(tidyverse)
library(gganimate) #may need: devtools::install_github("dgrtwo/gganimate")
library(animation)

df <- data.table::fread(
  "Type       Place  Year  Month1 Month2 Units  Valor
  Segment1   City1  Year1   1      1    192345 1020
  Segment2   City2  Year2   2      14   284590 1597
  Segment3   City3  Year1   3      3    186435 3478
  Segment4   City4  Year3   4      28   269056 1259"
)

ggplot(data = df, aes(x = factor(Year), y = Valor, group = Type, colour = Type)) +
  geom_line(size = 1, show.legend = F) +
  scale_color_manual(values = c("#ff9933", "#006400", "#d11141", "#551A8B")) +
  scale_x_discrete(position = "bottom") +
  scale_y_continuous(labels = NULL)+
  labs(
    title = "NDF- SR",
    x = "Time", y = "Sales"
  ) +
  # geom_text(aes(label=scales::percent(Valor, accuracy = 1),
  # vjust= -2), show.legend = FALSE) +
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_dl(aes(label = value), method = "last.points") +
  transition_reveal(Year) +
  coord_cartesian(clip = "off") +
  ease_aes("cubic-in-out")

animate(p, fps = 7, width = 900, height = 600)
anim_save("election.gif", p)

出现的错误是:

错误:沿数据必须是整数、数字、POSIXct、日期、difftime、orhms
另外:警告消息:
1:在 min(cl[cl != 0]) 中:min 没有非缺失参数;返回 Inf
2:在 min(cl[cl != 0]) 中:min 没有非缺失参数;返回 Inf

【问题讨论】:

  • 你用什么函数来制作这些动画?另外,您发布的数据中没有变量名Valor

标签: r ggplot2 gganimate


【解决方案1】:

一些建议:

  • 在顶部添加library(directlabels)或致电directlabels::geom_dl
  • transition_reveal(readr::parse_number(Year)) + 因为transition_reveal 需要一个数字条目,而Year 是像“Year1”和“Year2”这样的字符数据。
  • p &lt;- ggplot(data = df... 以便 ggplot/gganimate 对象可以在下一步中进行动画处理。

很难知道这是否有效,因为这似乎不是完整的数据;您通常不会使用geom_line,每组只有一个数据点。也许您可以在问题中描述更多您正在寻找的内容,和/或添加更多数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多