【问题标题】:gganimate "Error in order(ind) : argument 1 is not a vector"gganimate “顺序错误(ind):参数 1 不是向量”
【发布时间】:2021-08-04 16:34:01
【问题描述】:

我正在尝试使用 gganimate 包为一段时间内的性能图制作动画。我可以为使用geom_point() 的其他ggplots 设置动画,但这个会引发我无法弄清楚的错误。

我在下面做了一个可行的例子,它抛出了同样的错误:

library(tidyr)
library(ggplot2)
library(gganimate)
library(maps)
library(mapproj)
library(evaluate)

map <- map_data("world")
colnames(map)[1] <- "x"
colnames(map)[2] <- "y"

A1 <- data.frame(OrganisationID=c(1:10),x=rnorm(10) * 2 + 13,y= rnorm(10) + 48,`01 Jan 2020`=runif(10),"02 Jan 2020"=runif(10),
                 "03 Jan 2020"=runif(10),"04 Jan 2020"=runif(10),"05 Jan 2020"=runif(10),
                 "06 Jan 2020"=runif(10),"07 Jan 2020"=runif(10))
A1 <- gather(A1, Date, Performance, `X01.Jan.2020`:`X07.Jan.2020`, factor_key=TRUE)
A1$Date <- as.Date(A1$Date, format = "X%d.%b.%Y")
A1$Size <- sample(500:1000,70,T)


longitude_lim = sort(c(max(A1$x), min(A1$x)))
latitude_lim = sort(c(max(A1$y), min(A1$y)))

base_map <- ggplot() +
  geom_polygon(data = map, aes(x=x, y = y, group = group), fill="grey", alpha=0.3) +
  coord_fixed(xlim = longitude_lim, ylim = latitude_lim) +
  labs(title = "Performance Regional") +
  theme_bw() +
  geom_point(aes(x=A1$x, y=A1$y, color=A1$Performance), alpha=0.9) +
  scale_colour_gradient2(
    low = "red",
    mid = "#ffffcc",
    high = "#33cc33",
    midpoint = 0.85 
  )

# combine previous map lists, add points to map and animate
base_map
anim <- base_map +
  transition_states(A1$Date, transition_length = 2,
                    state_length = 1)
anim 

正如你所看到的,基本情节渲染得很好,但是当我尝试对其进行动画处理时,我得到了

Error in order(ind) : argument 1 is not a vector

我不知道从哪里开始解决这个问题,因为我没有一个名为“ind”的对象,所以我认为这是gganimate 中的某个过程。任何提示和技巧都值得赞赏(还有一种更有效的方式来创建我的 RepRoX)!

【问题讨论】:

    标签: r ggplot2 maps gganimate


    【解决方案1】:

    问题在于您使用了两个数据集,mapA1。为了使您的代码正常工作,您必须通过例如“告诉” gganimate 用于动画的数据集。通过将A1 传递给ggplot() 来制作全局数据集。

    顺便说一句:作为一般规则,使用 ggplot2 时不要通过A1$... 将变量分配给美学:

    base_map <- ggplot(data = A1) +
      geom_polygon(aes(x=x, y = y, group = group), data = map, fill="grey", alpha=0.3) +
      coord_fixed(xlim = longitude_lim, ylim = latitude_lim) +
      labs(title = "Performance Regional") +
      theme_bw() +
      geom_point(aes(x=x, y=y, color=Performance), alpha=0.9) +
      scale_colour_gradient2(
        low = "red",
        mid = "#ffffcc",
        high = "#33cc33",
        midpoint = 0.85 
      )
    
    # combine previous map lists, add points to map and animate
    base_map
    anim <- base_map +
      transition_states(Date, transition_length = 2,
                        state_length = 1)
    anim
    

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      相关资源
      最近更新 更多