gganimate "Error in order(ind) : argument 1 is not a vector"
gganimate "Error in order(ind) : argument 1 is not a vector"
我正在尝试使用 gganimate 包制作随时间变化的性能图的动画。我可以为使用 geom_point()
的其他 ggplot
设置动画,但这个会抛出一个我无法弄清楚的错误。
我在下面做了一个可行的例子,它抛出了同样的错误:
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 的更有效方法)!
问题是您使用了两个数据集,map
和 A1
。为了使您的代码正常工作,您必须“告诉”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
我正在尝试使用 gganimate 包制作随时间变化的性能图的动画。我可以为使用 geom_point()
的其他 ggplot
设置动画,但这个会抛出一个我无法弄清楚的错误。
我在下面做了一个可行的例子,它抛出了同样的错误:
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 的更有效方法)!
问题是您使用了两个数据集,map
和 A1
。为了使您的代码正常工作,您必须“告诉”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